Nginx从搭建到配置支持HTTPS的方法

2019-10-17 17:33:35王振洲

配置 https

# /usr/local/nginx/conf/nginx.conf
#
# HTTPS server configuration
#
server {
  listen    443 ssl; # ssl 端口
  server_name www.xingkongbj.com xingkongbj.com; # 域名
  ssl         on; # 开启 ssl
  ssl_certificate   ssl/server.crt;
  ssl_certificate_key ssl/server.key;
  ssl_session_timeout 5m;
#  ssl_protocols SSLv2 SSLv3 TLSv1;
#  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#  ssl_prefer_server_ciphers  on;
  location / {
   proxy_redirect off; # 禁止跳转
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_pass http://98.142.138.177/;
  }
}
# nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
# 原因是nginx缺少http_ssl_module模块,编译安装时带上--with-http_ssl_module配置就可以了
# 切换到nginx源码包
cd cd /usr/local/src/nginx-1.14.0/
# 查看 ngixn 原有的模块
/usr/local/nginx/sbin/nginx -V
# 重新配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 重新编译,不需要 make install 安装。否则会覆盖
make
# 备份原有已经安装好的 nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx.conf
# 将刚刚编译好的 nginx 覆盖掉原来的 nginx(ngixn必须停止)
cp ./objs/nginx /usr/local/nginx/sbin/ 
# 这时,会提示是否覆盖,请输入yes,直接回车默认不覆盖
# 启动 nginx,查看 nginx 模块,发现已经添加
/usr/local/nginx/sbin/nginx -V
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx

总结

以上所述是小编给大家介绍的Nginx从搭建到配置支持HTTPS的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易采站长站网站的支持!