Nginx 實現 HTTPS(基於 Let's Encrypt 的免費證書)


SSL / TLS加密會為您的用戶帶來更高的搜索排名和更好的安全性。
Let’s Encrypt 是一個認證機構(CA)。它可以提供免費證書,並且已經被大多數瀏覽器所信任。另外,通過工具 Certbot 可以讓我們完全自動化證書的安裝和更新。
安裝證書的前提條件:

安裝服務器(這里用 NGINX)。
注冊域名。
創建一個DNS記錄,將域名和服務器的 IP 地址相關聯。
記得安裝完成后,防火牆需要打開 443 端口,否則無法訪問!!!

1. 安裝 Let’s Encrypt 客戶端
所有的證書相關的操作,都可以通過 Certbot 軟件實現。
注意:HTTPS 作為重要的基礎服務,一旦出問題就需要立刻把軟件更新到官網提供的最新版本,所以不推薦用各個 Linux 發行版的默認倉庫進行安裝,而是通過官方工具 certbot-auto 安裝。

wget https://dl.eff.org/certbot-auto # 下載到本地
chmod a+x ./certbot-auto # 添加可執行權限
./certbot-auto --help all # 查看幫助

2. 驗證域名所有權
配置 Nginx,使要獲取證書的域名對應的 80 端口可以正常訪問。在 /etc/nginx/conf.d/ 目錄下為域名創建新文件 www.example.com.conf,並添加相關配置信息:

server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;

location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

重啟 Nginx

systemctl restart nginx

 

3. 生成證書
certbot-auto --nginx -d kikakika.com -d www.kikakika.com

出現錯誤:

Error while running nginx -c /etc/nginx/nginx.conf -t.

nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

解決方法:

/certbot-auto --nginx --nginx-server-root=/usr/local/nginx/conf

正常情況下,進入交互式界面,提示你輸入郵箱(在證書失效前收到通知郵件),並同意官方協議。

[root@VM_120_242_centos tmp]# ./certbot-auto --nginx -d scall2.szhuizhong.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for scall2.szhuizhong.cn
Waiting for verification...
Cleaning up challenges
Deployed Certificate to VirtualHost /etc/nginx/nginx.conf for scall2.szhuizhong.cn

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
No redirect - Make no further changes to the webserver configuration.
Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.

證書生成成功后,會讓你選擇是否將所有的 HTTP 請求重定向到 HTTPS(輸入 1 或者 2)。如果選 1,則通過 HTTP 和 HTTPS 都可以訪問。如果選 2,則所有通過 HTTP 來的請求,都會被 301 重定向到 HTTPS。參考下面的 5. 配置 Nginx。
輸完 1 或者 2 回車后,會有成功提示,並說明證書放在 /etc/letsencrypt/live/證書的域名 這個位置:

4. 開啟 443 端口
開啟443端口:firewall-cmd --permanent --add-port=443/tcp
查看是否成功:firewall-cmd --permanent --query-port=443/tcp
端口添加成功后,需要reload重新載入:firewall-cmd --reload

5. 配置 Nginx
certbot-auto 會自動調用 Nginx 的插件,改寫配置文件。可以通過參數 certonly 關閉自動改寫配置文件的功能,只獲取證書。
./certbot-auto certonly -d kikakika.com

下面是 certbot-auto 自動改寫過的配置文件,所有改寫過的行都在后面加了注釋:# managed by Certbot。

# 生成證書時選 1: No redirect,不把 HTTP 請求重定向到 HTTPS,一個 server 同時監聽 80 和 443 端口
server {
listen 80;
server_name scall2.szhuizhong.cn;
root /home/szhuizhong/trunk;

location / {
if (-f $request_filename) {
expires max;
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
index index.html index.htm index.php l.php;
autoindex off;
}

error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param CI_ENV 'testing';
include fastcgi_params;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/scall2.szhuizhong.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/scall2.szhuizhong.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
}

# 生成證書時選 2: redirect,HTTP 請求重定向到 HTTPS,兩個 server 分別監聽 80 和 443 端口
server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;

location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/kikakika.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/kikakika.com/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
}
server {
server_name kikakika.com www.kikakika.com;
listen 80;
return 301 https://$host$request_uri; # managed by Certbot
}

6. 設置定時任務
出於安全策略, Let’s Encrypt 簽發的證書有效期只有 90 天。通過 ./certbot-auto renew 命令可以續簽。
- 編輯 crontab 文件:

$ crontab -e

添加 certbot 命令:
在每天凌晨3點運行。該命令將檢查服務器上的證書是否將在未來30天內過期,如果是,則進行更新。--quiet 指令告訴 certbot 不要生成輸出。
0 3 * * * /root/letsencrypt/certbot-auto renew --quiet

寫入日志

0 0 1 * * /home/soft/ssl/letsencrypt/certbot-auto renew>>/home/soft/ssl/letsencrypt/log.txt



保存並關閉文件。所有安裝的證書將自動更新並重新加載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM