Nginx核心要領十三:Nginx讓網站啓用免費HTTPS,子域名也開啓https

Let’s Encrypt 是一個於2015年推出的數字證書認證機構,將通過旨在消除當前手動創建和安裝證書的複雜過程的自動化流程,爲安全網站提供免費的SSL/TLS證書,它有個贊助商 電子前哨基金會,讓網站安裝證書十分簡單,只需要使用電子前哨基金會EFF的 Certbot

PS:阿里雲服務器,需要先在服務器網絡安全規則中開放 443 端口

讓我們來安裝證書吧:

  • 1.打開 https://certbot.eff.org 網頁
  • 2.選擇web服務軟件和操作系統版本,我這裏是 nginx 、centos7.4
    在這裏插入圖片描述
  • 3.在該頁面下方會有 Nginx 在 Centos7下安裝 HTTPS 的操作步驟
    a:用ssh連接服務器
    b:啓用epel依賴,可用 yum install -y epel epel-devel
    c:yum -y install yum-utils
    d:yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
    e:sudo yum install certbot python2-certbot-nginx
  • 4.執行上面的命令後,再運行:sudo certbot --nginx ,certbot 會自動檢查到你的 nginx.conf 下的配置,把你所有的虛擬站點都列出來,然後讓你選擇需要開啓 https 的站點。你就簡單的輸入列表編號(用空格分開),然後,certbot 就幫你下載證書並更新 nginx.conf 了
  • 5.打開 nginx.conf 文件,可在 server 模塊下看到下面內容,(其中 http2 可以提升性能,但是需要在編譯nginx階段通過 --with-http_v2_module啓用,如果你沒有則可去掉,編譯代碼:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module(http2要求 Nginx 版本要大於 1.9.5))
#在location下添加重定向,訪問 www.zypcy.cn時會自動跳轉到 https://www.zypcy.cn
location / {
    # Redirect non-https traffic to https
    if ($scheme != "https") {
       return 301 https://$host$request_uri;
    }
}

listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.zypcy.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.zypcy.cn/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
  • 6.但是,Let’s Encrypt 的證書 90 天就過期了,所以,你還要設置上自動化的更新腳本,最容易的莫過於使用 crontab 了。使用 crontab -e 命令加入如下的定時作業(每個月都強制更新一下)
    在shell中輸入 crontab -e 回車,輸入下面的腳本,一月檢查一次證書,並重啓 nginx
0 0 1 * * /usr/bin/certbot renew --force-renewal
5 0 1 * * /usr/local/nginx/sbin/nginx -s reload

注:crontab 中有六個字段,其含義如下:
第1個字段:分鐘 (0-59分鐘)
第2個字段:小時 (0-23小時)
第3個字段:日期 (1-31號)
第4個字段:月份 (1-12月)
第5個字段:一週當中的某天 (0-7 [7 或 0 代表星期天])

現在訪問 www.zypcy.cn,瀏覽器自動轉換爲 https://www.zypcy.cn 了

之前我不太清楚 crontab -e 命令
因此有一個疑惑,怎麼驗證 crontab -e 週期性執行任務是否會運行,下面進行驗證,目標:每分鐘打印一段話

  1. crontab -e 回車
  2. 在裏面輸入:*/1 * * * * echo “$(date) zhuyu” >> /home/clog.log 2>&1
  3. 查看clog.log文件裏面是否有輸出
  4. cat /home/clog.log,可看到確實是 1分鐘執行了一次,因此上面一月檢查一次ssl證書的命令也會執行
    在這裏插入圖片描述
子域名開啓HTTPS,使用certbot --nginx命令無法生成ssl證書

使用:certbot certonly --webroot -w /home/fast/res/ -d your.domain.com
安裝成功後,默認會在 /etc/letsencrypt/live/your.domain.com/ 會生成CA證書。

    server {
       listen 80;
       server_name res.csxiuneng.com;
       location / {
         alias /home/fast/res/;
         autoindex on;
       }

       listen 443 ssl http2; # managed by Certbot
       ssl_certificate /etc/letsencrypt/live/res.csxiuneng.com/fullchain.pem; # managed by Certbot
       ssl_certificate_key /etc/letsencrypt/live/res.csxiuneng.com/privkey.pem; # managed by Certbot
    }

參考了一些博客:
https://coolshell.cn/articles/18094.html
https://zning.me/15424636102403.html
https://github.com/zning1994/certbot-letencrypt-wildcardcertificates-alydns-au
https://blog.csdn.net/liaoyanyunde/article/details/86572370

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章