nginx配置https,支持http和https访问

it2022-05-05  152

前言

移动产品,升级https,由于用了nginx反向代理,所以就把https锁定在了nginx

前期准备

申请阿里云SSL证书

1、找到阿里云SSL证书

2、购买证书

3、根据自己的需求选择证书类型

4、进入SSL证书控制台

5、下载nginx类型证书

上传证书到服务器

1、在nginx的conf文件中创建cert文件夹

2、上传nginx证书到cert文件夹

nginx安装http_ssl_module模块

1、进入nginx源码包

2、执行命令,重新编译nginx

命令:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

3、重新安装nginx启动文件

命令:

make

不要使用 make install,否则会被覆盖安装

4、备份原先的nginx运行文件(以防万一)

命令:

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

5、查看新生成的nginx运行文件

6、替换刚编译后nginx运行文件(此处需要先关闭nginx服务)

命令:

cp ./objs/nginx /usr/local/nginx/sbin/

7、查看安装情况

命令:

/usr/local/nginx/sbin/nginx -V

出现--with-http_ssl_module说明已安装ok

nginx配置https

server { listen 80; listen 443; server_name test.baidu.com; ssl_certificate cert/2031870__pre-read.com.pem; ssl_certificate_key cert/2031870__pre-read.com.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击 server_tokens off; #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密, #并且强制用 HTTPS 访问 fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; access_log /usr/local/nginx/logs/httpsaccess.log; location ~* (?:favicon\.ico|crossdomain\.xml)$ { expires 10d; } location / { root /; proxy_set_header Host $host; proxy_pass http://manage-read-server; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; } }

 

1、检测配置是否正确

命令:

/usr/local/nginx/sbin/nginx -t

2、启动nginx

启动命令:

/usr/local/nginx/sbin/nginx

重启命令:

/usr/local/nginx/sbin/nginx -s reload

3、访问页面

同时也支持http请求


最新回复(0)