HTTPS 现在已经是网站标配。Let’s Encrypt 提供免费的 DV 证书,有效期 90 天,配合 certbot 可以自动申请和续期。下面是 Nginx 上配置 Let’s Encrypt 的完整步骤。
Let’s Encrypt 证书类型对比
| 工具 | 适用场景 | 特点 |
|---|---|---|
| certbot | Nginx/Apache 标准部署 | 自动修改配置,最常用 |
| acme.sh | DNS 验证/泛域名 | 纯 shell,依赖少 |
| Cloudflare | CDN 接管 SSL | 源站可不装证书 |
续期流程
→
→
安装 certbot
Debian/Ubuntu:
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
CentOS/RHEL:
sudo dnf install certbot python3-certbot-nginx -y
申请证书
certbot 会自动读取 Nginx 配置里的 server_name,然后验证域名所有权。执行:
sudo certbot --nginx -d example.com -d www.example.com
按提示选择是否把 HTTP 流量重定向到 HTTPS。建议选 2(强制重定向)。
验证证书
申请完成后检查:
sudo certbot certificates
浏览器访问 https://example.com,应该能看到锁标志。
自动续期
certbot 安装时会创建一个 systemd timer 或 cron 任务。测试续期是否工作:
sudo certbot renew --dry-run
如果测试通过,证书会在到期前自动续期。
手动配置 Nginx
如果 certbot –nginx 自动修改配置失败,可以手动配置。certbot 会把证书放在 /etc/letsencrypt/live/example.com/:
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 现代 TLS 配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...;
ssl_prefer_server_ciphers off;
location / {
root /var/www/html;
index index.html;
}
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
常见问题处理
验证失败:确保 80 端口能从外网访问,Nginx 配置里有正确的 server_name,域名解析已经生效。
证书路径错误:检查 /etc/letsencrypt/live/ 下的域名目录是否正确。
自动续期没跑:检查 systemctl list-timers 里的 certbot.timer,或者 /etc/cron.d/certbot。
关于证书过期后的处理,参考 HTTPS 证书过期怎么办?确认与续期完整指南。
HTTPS 优化
# 开启 HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# OCSP 装订
ssl_stapling on;
ssl_stapling_verify on;
更多 Nginx 运维教程,请访问 fenij.com。
常见问题
问:一个证书可以覆盖多个域名吗?
答:可以。申请时加上多个 -d 参数,certbot 会生成 SAN 证书。最多 100 个域名。
问:通配符证书怎么申请?
答:需要 DNS 验证。执行 certbot -d *.example.com --manual --preferred-challenges dns certonly,按提示添加 TXT 记录。
问:Let’s Encrypt 证书支持 IP 吗?
答:不支持。必须有域名。如果只有 IP,可以考虑 ZeroSSL 或自签证书。