Aaron Lau(韩栖)

Nginx 学习笔记 (3) —— Let's Encrypt 证书

334 字

倘源站只支持 HTTP,也许做到上一篇就够了。但都 9102 年,还不支持 HTTPS 的网站很少了。即使后端不支持 HTTPS,经过反代服务器的中转,也能实现 HTTPS 加密访问。

1) 启用 EPEL 源。官方源因版权顾虑,移除了有争议的软件包,而 Extra Packages for Enterprise Linux 的尺度更宽一些,算是个有益补充; 2) 安装并配置 Certbot,应指定 Nginx 的安装目录。Certbot 拥有傻瓜式的交互,键入邮箱、接受条款、选取主机名、决定 HTTP 是否重定向至 HTTPS,视情况操作即可;

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install certbot python2-certbot-nginx
certbot --nginx --nginx-server-root=/usr/local/nginx/conf

3) 创建一个 cron 定时任务,以实现自动续期;

echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

4) 完事了。就这么简单。来看看 Certbot 都动了哪些配置:

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

options-ssl-nginx.conf 里默认是 ssl_protocols TLSv1.2 TLSv1.3。如需开启 HTTP/2、TLS 1.3 和 HSTS,见下一篇。

<small>原载 App-scope,有删改。</small>

App-scope · 教程 · Nginx