1.获取安装包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
2.解压后并进入
tar xvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
3.配置安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install
4.启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5.假设没有安装编译工具,请先执行以下命令,然后执行步骤3(配置安装)
yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl-devel yum -y install gcc
6.配置nginx.conf
vim /usr/local/nginx/conf/nginx.conf
error_log /var/log/nginx/error.log; #有修改
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#有修改
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $http_host $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;#有修改
resolver 223.5.5.5 223.6.6.6 8.8.8.8 1.2.4.8 114.114.114.114 valid=3600s;#有修改
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;#有修改
#gzip on;
server {7.重启或者关闭nginx
/usr/local/nginx/sbin/nginx -s reload /usr/local/nginx/sbin/nginx -s stop
8.配置ssl
vim /etc/nginx/conf.d/test.baidu.com.conf
server{
listen 80;
server_name test.baidu.com;
return 301 https://test.baidu.com$request_uri;
}
server{
listen 443 ssl;
server_name test.baidu.com;
location / {
proxy_pass http://127.0.0.1:9090/baidu/;
proxy_redirect http://$host:9090/baidu/ /;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 = /404.html;
#error_page 404 /404.html;
#location = /40x.html {
#}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# ssl on;
ssl_certificate /root/.acme.sh/test.baidu.com/fullchain.cer; # 证书文件
ssl_certificate_key /root/.acme.sh/test.baidu.com/test.baidu.com.key; # 私钥文件
ssl_session_timeout 5m; # 会话缓存过期时间
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 开启 SSL 支持
ssl_prefer_server_ciphers on; # 设置协商加密算法时,优先使用服务端的加密套件
}