一、SSL证书申请
现在主流的ssl证书有openssl和startssl,这里我们使用openssl证书来开发,毕竟跟微信小程序保持一致比较安全些,我是通过阿里云申请。
二、密钥生成
在nginx的安装目录下创建 cert文件夹,将阿里云生成的密钥复制到你的服务器 1. [root@hah ~]# cd ../usr/local/nginx/ 2. [root@hah nginx]# mkdir cert 3. 上传密钥文件件
三、nginx重新编译添加SSL模块
1. 检查是否安装ssl模块 [root@hah nginx]# /usr/local/nginx/sbin/nginx -V 2. 若吗没有ssl模块安装ssl(关闭nginx) ①进入nginx安装目录 进入的是我们解压nginx的目录 [root@hah ~]# cd nginx-1.13.4 [root@hah ~]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module ②make编译 ,不需要make install,否则会覆盖安装了 [root@hah ~]# make ③备份原有的nginx文件(备份是一个良好的习惯,再修改配置文件的时候最好都要备份一下) [root@hah ~]# cp /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak ④将新生成的nginx程序覆盖原有的nginx(这个时候nginx要停止状态) [root@hah ~]# cp objs/nginx /usr/local/nginx/sbin/nginx ⑤测试新的nginx程序是否正确 [root@hah ~]# /usr/local/nginx/sbin/nginx -t 如果显示如下说明成功,否则失败 nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test issuccessful ⑥启动nginx,可以通过命令查看是否已经加入成功(不报错,就是成功了) [root@hah ~]# cd /usr/local/nginx/sbin [root@hah ~]# ./nginx
四、nginx配置SSL加密
①监听80接口 server { listen 80; server_name www.xxxx.com; rewrite ^(.*) https://$server_name$1 permanent; } ②监听443端口 server { listen 443 ssl; server_name www.xxxx.com; ssl on; ssl_certificate /usr/local/nginx/key/server.crt; ssl_certificate_key /usr/local/nginx/key/server.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #指定SSL服务器端支持的协议版本 ssl_ciphers HIGH:!aNULL:!MD5; #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #指定加密算法 ssl_prefer_server_ciphers on; #在使用SSLv3和TLS协议时指定服务器的加密算法要优先于客户端的加密算法 }
参见 : https://blog.csdn.net/w410589502/article/details/72833283
欢迎关注我的公众号