前提 该文主要记录如何在没有购买域名的情况下使用SSL/TLS协议,即地址前面的http 变成了https 。但是这样的SSL协议是会被浏览器认为是不安全的。在开发或者测试环境可以这样搞,生产环境下还是乖乖的买个域名吧。
SSL证书 第一步 首先到https://csr.chinassl.net/generator-csr.html 这里生成SSL秘钥(私钥)和等会拿去生成SSL证书的CSR文件。里面内容可以随便填,域名啥的随便填都没关系。保存好这两个文件。
第二步 拿刚才的CSR文件到https://csr.chinassl.net/free-ssl.html 这里生成SSL证书。
到这里为止,我们只需要记住秘钥 和SSL证书 的存储路径,在nginx配置文件当中需要使用到。 假设存到这里吧。
1 2 /etc/ssl/my_domain/my_domain.ssl /etc/ssl/my_domain/my_domain.private
我这里只是改了文件的后缀而已,并不影响使用。文件的后缀名你们自行决定也可以。
Nginx添加SSL模块 先查看Nginx以前安装过的模块,避免编译后覆盖了之前添加的模块。进入到你的nginx安装包目录。执行以下命令
1 2 3 4 5 6 nginx version: nginx/1.16.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local /nginx --with-http_realip_module
主要看configure arguments
这一行,那么我之前的预编译命令就是如下,而如果没有自定义添加过任何模块那么这里应该为空的
1 ./configure --prefix=/usr/local /nginx --with-http_realip_module
现在需要添加SSL模块,那么命令如下:
1 2 ./configure --prefix=/usr/local /nginx --with-http_realip_module \ --with-http_ssl_module
然后执行make
命令,已经安装过安装过nginx的(即执行过make install
),就不要执行 make install
,不然把你之前安装好的nginx文件覆盖掉。 当然,未安装Nginx的就可以执行make install
命令了。
更新Nginx启动文件 方式一:停止Nginx服务更新 1 2 3 4 cd /usr/local /nginx/sbin/./nginx -s stop mv ./nginx ./nginx.old cp nginx安装包目录/objs/nginx ./nginx
方式二:热部署更新 可以参考我公众号的文章:https://mp.weixin.qq.com/s/o7rkczakPNiys1KM7Z87EA
配置文件 1 vim /usr/local /nginx/conf/nginx.conf
配置文件我只摘取了server模块,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 server { listen 80 ; server_name 127.0.0.1 ; location / { rewrite ^/(.*) https://$host $1 permanent ; } } server { listen 443 ssl; server_name 127.0.0.1 ; ssl_certificate /etc/ssl/my_domain/my_domain.ssl; ssl_certificate_key /etc/ssl/my_domain/my_domain.private; ssl_session_cache shared:SSL:1m ; ssl_session_timeout 5m ; ssl_prefer_server_ciphers on ; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 ; location / { root html; index index.html index.htm; } }
此时输入ip地址,你就能看到https
了。
扩展知识 多个SSL模块 当nginx的多个模块都需要使用SSL协议时,如PC端的前端项目使用了80端口转发,手机端使用了81端口转发。那么可以改成如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 server { listen 80 ; server_name 127.0.0.1 ; location / { rewrite ^/(.*) https://$host $1 permanent ; } } server { listen 81 ; server_name 127.0.0.1 ; location / { rewrite ^/(.*) https://$host :8443$1 permanent ; } } server { listen 443 ssl; server_name 127.0.0.1 ; ssl_certificate /etc/ssl/my_domain/my_domain.ssl; ssl_certificate_key /etc/ssl/my_domain/my_domain.private; ssl_session_cache shared:SSL:1m ; ssl_session_timeout 5m ; ssl_prefer_server_ciphers on ; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 ; location / { root html; index index.html index.htm; } } server { listen 8443 ssl; server_name 127.0.0.1 ; ssl_certificate /etc/ssl/my_domain/my_domain.ssl; ssl_certificate_key /etc/ssl/my_domain/my_domain.private; ssl_session_cache shared:SSL:1m ; ssl_session_timeout 5m ; ssl_prefer_server_ciphers on ; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 ; location / { root html; index index.html index.htm; } }
443端口转发 https的默认端口是443,而没有root权限的用户启动时,nginx会提示没有权限使用443端口,此时则需要使用端口转发规则,把443转发到其它端口,如8443。那么需要root用户执行以下命令
1 2 3 iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443 iptables -t nat -nL --line service iptables save
然后把nginx配置文件的监听端口改成8443 ssl
1 2 3 4 5 6 7 8 9 10 11 12 13 server { listen 80 ; server_name 127.0.0.1 ; location / { rewrite ^/(.*) https://$host $1 permanent ; } } server { listen 8443 ssl; server_name 127.0.0.1 ; ... }
总结 OK,这就是最近工作上需要完成的一个功能,还是自己太菜了。总结以下,希望也能帮到别人。~Thanks♪(・ω・)ノ
个人博客网址: https://colablog.cn/
如果我的文章帮助到您,可以关注我的微信公众号,第一时间分享文章给您