Let's Encrypt 自动续期后 nginx 不重载的解决办法
这个问题踩过不止一次:certbot 明明续期成功了,证书文件也是新的,但浏览器里看到的还是旧证书,到期时间没变。
原因很简单 —— certbot 只负责把证书写到磁盘,不会通知 nginx 重新读取。nginx 在启动时把证书加载进内存,之后文件变了它也不知道。
两种解决办法
第一种是加 --deploy-hook:
certbot renew --deploy-hook "systemctl reload nginx"
但这只在手动跑 renew 的时候生效。放在 cron 里的续期任务不会带上这个参数。
第二种更省事 —— 用 renewal-hooks 目录,certbot 每次续期成功后都会执行里面的脚本:
mkdir -p /etc/letsencrypt/renewal-hooks/deploy
printf '#!/bin/sh
systemctl reload nginx
' \
> /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
放进去之后不用重启任何东西,下次续期自动生效。
怎么确认真的生效了
别只看 certbot certificates,要看 nginx 实际吐出来的证书:
echo | openssl s_client -connect example.com:443 \
-servername example.com 2>/dev/null \
| openssl x509 -noout -dates
对比一下磁盘上的证书到期时间,一致才算真的生效。