centos nginx下配置免费https

准备

记录下部署免费https的过程 ,使用Let’s Encrypt的免费证书

下载自动安装脚本`wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto`

安装

  1. 执行脚本./certbot-auto --nginx

这里会下载一些东西,让后让你选择你需要加https的域名,这个域名是在
nginx.conf中配置的
server中读取到的。注意选择的域名只能是备案过的那个域名。因为这个脚本会到
DNS服务器去查这个域名对应的
ip服务器。对不上也是不会给颁发证书的。

《centos nginx下配置免费https》

  1. 执行成功后,会让你选择是否把http的请求重定向到https。直接选择2就行

《centos nginx下配置免费https》

到这里已经配置成功了,访问下网站就可以看效果了。

点击那个锁还可以看到关于证书的详细信息

《centos nginx下配置免费https》

《centos nginx下配置免费https》

总结

其实这脚本就是相当于一键安装包,帮你申请https证书,然后下载到服务器存放,然后在把证书配置到nginx.conf里边。打开nginx.conf就能看到新增的配置信息。

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

     # Redirect non-https traffic to https
     if ($scheme != "https") {
        return 301 https://$host$request_uri;
        #该状态代码301告诉浏览器(和搜索引擎)这是永久重定向。这使浏览器记住重定向,以便下次访问时,浏览器将在内部进行重定向
     }# managed by Certbot
}

由于Let’s Encrypt这个证书90天后就过期了,可以使用cron做一个定时任务,因为我这个证书是18号申请的,所以每个月的19号就执行一次,执行crontab -e后会进入个文件输入0 0 19 * * ./path/to/certbot-auto renew
crontab -e的五个参数分别代表,分钟、小时、天、月、周。

参考

https://certbot.eff.org/lets-encrypt/centos6-nginx
https://bjornjohansen.no/redirect-to-https-with-nginx
http://nginx.org/en/docs/http/ngx_http_core_module.html

    原文作者:玫翠慧
    原文地址: https://segmentfault.com/a/1190000017436129
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞