http { # ... include /etc/nginx/sites-enabled/*; }
1 2 3 4 5 6
# file: /etc/nginx/sites-available/web-ssl.conf
server { server_name mydomain.com; # ... }
Create link to /etc/nginx/sites-available/web-ssl.conf:
1 2
cd /etc/nginx/sites-enabled sudo ln -s ../sites-available/web-ssl.conf .
Reload Nginx:
1
sudo nginx -s reload
申請 Let′s encrypt SSL 憑證
以上前置作業都完成後,就可以來申請憑證:
1
sudo certbot --nginx -d mydomain.com
接著可以選擇要不要自動轉址:
1 2 3 4 5 6 7 8
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: 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. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/mydomain.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/mydomain.com/privkey.pem Your cert will expire on 2021-06-30. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
檢查 config 是否有被自動添加內容
檢查 Nginx config 是否有被自動增加 listen 443 及憑證:
1 2 3 4 5
listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/mydomain.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
如果申請憑證時有選擇自動轉址,再檢查以下內容是否有被設定:
1 2 3 4 5 6
if ($host = mydomain.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name mydomain.com; return 404; # managed by Certbot