用 Let's Encrypt 免费证书的站,某天凌晨证书过期,全站 HTTPS 报错,早上才发现。
为什么会失败
certbot 的自动续期任务一直在跑,但失败了没告警。手动执行才看到错误:
Certbot failed to authenticate some domains (authenticator: webroot).
The server could not connect to the client for validation.
原因是 80 端口的验证路径被一条新加的 Nginx 规则拦了:
location ~ /\.well-known { deny all; }
这条规则本意是隐藏敏感目录,但把 ACME 验证也挡了。
处理
- 修正规则,放行验证路径:
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/www/certbot;
}
- 手动续期验证
- 加监控:证书剩余天数 < 15 天告警
监控脚本
end=$(echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null \
| openssl x509 -noout -enddate | cut -d= -f2)
days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
[ "$days" -lt 15 ] && echo "证书剩余 ${days} 天" | mail -s "SSL 告警" ops@example.com
这个脚本放 crontab 每天跑一次,比等证书过期靠谱。
其他常见原因
- DNS 解析指向了旧服务器,验证文件访问不到
- 用了 CDN,验证请求被 CDN 拦截或缓存
- 续期频率过高被限流(Let's Encrypt 有配额)
- 证书链不完整,部分客户端不信任
建议
- 续期任务加
--deploy-hook,成功后自动 reload Nginx
- 保留一份手动续期文档,出事能照做
- 关键站点可以买付费证书,省心