Nginx SSL 证书过期排查与 Certbot 自动续期
一、问题背景
某业务网站可以正常访问,但浏览器提示 SSL 证书已经过期。
登录 Ubuntu 服务器排查时,发现以下异常:
/etc/nginx/下找不到之前配置的业务域名。nginx -T只显示默认配置,没有 HTTPS 和反向代理配置。systemctl status nginx显示启动失败。网站依然可以正常访问。
本文统一使用 example.com 作为示例域名。
核心排查思路:先定位真正提供服务的 Nginx,再检查证书管理方式,最后解决证书申请和自动续期问题。
二、定位实际运行的 Nginx
2.1 查看 Nginx 完整配置
nginx -T该命令会检查配置语法,并输出主配置文件及所有通过 include 引入的配置文件。
发现只加载了以下配置:
/etc/nginx/nginx.conf
/etc/nginx/sites-enabled/default没有找到业务域名、SSL 证书及反向代理配置。
思考:为什么没有业务配置,网站却仍然可以访问?
存在以下几种可能:
Nginx 配置文件被删除,但运行中的进程仍使用内存中的旧配置。
服务器安装了多套 Nginx,当前查看的不是实际运行的实例。
网站由其他服务器或代理提供服务。
注意:nginx -T 读取的是磁盘上的配置文件,而不是运行中 Nginx 内存中的配置。
2.2 查看 Nginx 服务状态
systemctl status nginx.service发现以下错误:
Active: failed (Result: exit-code)
bind() to 0.0.0.0:80 failed (98: Unknown error)
still could not bind()Linux 错误码 98 对应 EADDRINUSE,表示地址或端口已经被占用。
思考:当前 Nginx 启动失败,但是网站仍能访问,说明很可能存在另一个正在运行的 Web 服务。
查看端口占用:
ss -lntp | grep -E ':(80|443)\s'此时不能盲目杀掉占用端口的进程,因为它可能就是正在提供业务服务的进程。
2.3 查看实际运行的 Nginx 进程
ps -ef | grep '[n]ginx: master'返回结果:
nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf至此确认,服务器存在两套 Nginx。
问题原因:之前执行的 nginx 命令指向 Ubuntu 安装的 Nginx,而实际业务系统使用的是宝塔安装的 Nginx。
2.4 查找真正的业务配置
查看完整配置:
/www/server/nginx/sbin/nginx -T \
-c /www/server/nginx/conf/nginx.conf搜索域名、证书及反向代理配置:
grep -RnE 'server_name|ssl_certificate|proxy_pass' \
/www/server/panel/vhost/nginx/最终定位到业务配置文件:
/www/server/panel/vhost/nginx/example.com.conf三、排查 SSL 证书管理方式
3.1 查看原有证书配置
cat /www/server/panel/vhost/nginx/example.com.conf发现原有证书路径:
ssl_certificate /www/server/nginx/ssl/example.com/fullchain.pem;
ssl_certificate_key /www/server/nginx/ssl/example.com/privkey.pem;查看证书有效期及签发机构:
openssl x509 \
-in /www/server/nginx/ssl/example.com/fullchain.pem \
-noout -dates -issuer其中:
notBefore:证书生效时间。
notAfter:证书过期时间。
issuer:证书签发机构。
3.2 检查 Certbot 是否管理证书
certbot certificates返回:
No certificates found.检查证书存储目录:
ls -lah /etc/letsencrypt/live/返回:
No such file or directory排查结论:
当前 Certbot 默认目录下没有任何证书管理记录,因此无法直接通过 certbot renew 完成续期。
之前的证书可能由宝塔、其他 ACME 工具申请,或者通过手动方式导入,具体来源尚未确认。
最终决定使用 Certbot 重新申请证书,并建立自动续期机制。
四、Certbot 证书申请方式
本次选择 Webroot 模式,原因如下:
不需要停止正在运行的 Nginx。
不影响正常业务访问。
不依赖 Certbot 自动识别宝塔 Nginx。
方便后续实现自动续期。
Webroot 工作原理
Certbot 创建临时验证文件
↓
存入指定 Webroot 目录
↓
Nginx 通过 HTTP 80 端口提供文件访问
↓
Let's Encrypt 通过公网访问验证文件
↓
确认域名控制权
↓
签发 SSL 证书五、证书申请失败与原因分析
5.1 创建验证目录
mkdir -p /var/www/certbot/.well-known/acme-challenge申请证书:
certbot certonly \
--webroot \
-w /var/www/certbot \
--cert-name example.com \
-d example.com \
-d www.example.com参数说明:
5.2 申请失败
报错信息:
Certbot failed to authenticate some domains
Type: tls
Fetching https://example.com/.well-known/acme-challenge/...
remote error: tls: handshake failure思考:Webroot 使用 HTTP-01 验证,为什么错误信息中出现了 HTTPS?
检查原有 Nginx 配置:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}发现所有 HTTP 请求都会被重定向到 HTTPS,包括 Certbot 的验证请求。
实际失败流程:
Let's Encrypt 发起 HTTP 验证请求
↓
Nginx 返回 301 重定向
↓
Let's Encrypt 访问 HTTPS 443
↓
HTTPS 握手失败
↓
无法获取验证文件
↓
证书申请失败注意:Let's Encrypt 支持一定条件下的 HTTP 到 HTTPS 重定向,并且不会检查重定向目标的证书有效期。因此,证书过期本身不一定导致验证失败。
本次日志明确显示的是 TLS 握手失败,具体握手失败原因没有进一步确认。
最简单可靠的解决方案是让 ACME 验证请求直接通过 HTTP 返回验证文件。
六、修复 Nginx 验证配置
6.1 备份原有配置
cp /www/server/panel/vhost/nginx/example.com.conf \
/root/example.com.conf.bak6.2 修改80端口配置
vim /www/server/panel/vhost/nginx/example.com.conf修改为:
server {
listen 80;
server_name example.com www.example.com;
# Certbot 域名验证
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type text/plain;
try_files $uri =404;
}
# 其他请求正常跳转 HTTPS
location / {
return 301 https://$host$request_uri;
}
}此时443端口的证书配置保持不变。
6.3 为什么必须将 return 301 放到 location 中?
Nginx 的 server 级别 rewrite 指令会在 location 匹配之前执行。
如果直接在 server 中配置 return 301,即使增加了 ACME 验证 location,请求也会提前被重定向。
修复后的请求流程:
HTTP 80 请求
|
├── /.well-known/acme-challenge/
| ↓
| 直接读取 Webroot 验证文件
| ↓
| 返回 HTTP 200
|
└── 其他请求
↓
301 重定向
↓
HTTPS 443这样既不影响正常 HTTPS 跳转,也能够完成证书申请和续期验证。
6.4 检查并重载 Nginx
/www/server/nginx/sbin/nginx -t \
-c /www/server/nginx/conf/nginx.conf && \
/www/server/nginx/sbin/nginx -s reload \
-c /www/server/nginx/conf/nginx.conf必须使用实际运行的 Nginx 程序,避免误操作另一套 Nginx。
6.5 手动验证 Webroot 路径
创建测试文件:
echo "success" > /var/www/certbot/.well-known/acme-challenge/test测试本机:
curl -i -H "Host: example.com" \
http://127.0.0.1/.well-known/acme-challenge/test测试域名:
curl -i http://example.com/.well-known/acme-challenge/test
curl -i http://www.example.com/.well-known/acme-challenge/test预期结果:
HTTP/1.1 200 OK
success如果本机访问正常,而公网访问异常,则进一步检查 DNS 解析、CDN、安全组及防火墙。
测试完成后删除测试文件:
rm /var/www/certbot/.well-known/acme-challenge/test6.6 重新申请证书
certbot certonly \
--webroot \
-w /var/www/certbot \
--cert-name example.com \
-d example.com \
-d www.example.com最终成功申请 SSL 证书。
七、配置新证书
7.1 Certbot 证书目录
/etc/letsencrypt/live/example.com/
├── cert.pem
├── chain.pem
├── fullchain.pem
└── privkey.pemNginx 通常使用 fullchain.pem 和 privkey.pem。
7.2 最终推荐的 Nginx 完整配置
server {
listen 80;
server_name example.com www.example.com;
# Certbot 证书申请和续期验证
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type text/plain;
try_files $uri =404;
}
# HTTP 跳转 HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com www.example.com;
# 直接引用 Certbot 管理的证书
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
client_max_body_size 100M;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
access_log /www/wwwlogs/example.com.log;
error_log /www/wwwlogs/example.com.error.log;
}推荐直接引用 Certbot 管理的证书,而不是手动复制到其他目录,避免续期成功后 Nginx 仍然引用旧证书。
检查并重载:
/www/server/nginx/sbin/nginx -t \
-c /www/server/nginx/conf/nginx.conf && \
/www/server/nginx/sbin/nginx -s reload \
-c /www/server/nginx/conf/nginx.conf7.3 验证网站实际使用的证书
echo | openssl s_client \
-connect example.com:443 \
-servername example.com 2>/dev/null \
| openssl x509 -noout -dates -subject -issuer通过检查网站实际对外提供的证书,确认新证书是否已经生效,而不是仅检查磁盘上的证书文件。
两个域名都应进行验证,并使用浏览器或 curl 确认 HTTPS 访问正常。
八、配置 Certbot 自动续期
8.1 手动续期命令
certbot renewCertbot 会自动检查所有已管理的证书,仅续期满足续期条件的证书。
模拟续期:
certbot renew --dry-run模拟续期用于测试域名验证流程,不会替换正式证书。
强制续期:
certbot renew --force-renewal生产环境不要频繁强制续期,避免触发 Let's Encrypt 签发频率限制。
8.2 为什么续期后必须重载 Nginx?
Nginx 在启动或重载配置时,会将静态配置的 SSL 证书和私钥加载到内存中。
Certbot 更新磁盘上的证书文件后,正在运行的 Nginx 不会自动重新加载证书。
因此需要配置 Deploy Hook,在证书续期成功后自动重载 Nginx。
8.3 创建自动重载脚本
直接执行以下命令:
mkdir -p /etc/letsencrypt/renewal-hooks/deploy/
cat > /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh <<'EOF'
#!/bin/bash
set -e
NGINX=/www/server/nginx/sbin/nginx
CONF=/www/server/nginx/conf/nginx.conf
# 检查 Nginx 配置
"$NGINX" -t -c "$CONF"
# 平滑重载 Nginx,使新证书生效
"$NGINX" -s reload -c "$CONF"
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shCertbot 成功续期后会自动执行 Deploy Hook。
使用 set -e 可以避免配置检查失败后继续执行重载命令。
8.4 配置自动续期定时任务
检查是否已有定时任务:
systemctl list-timers --all | grep certbot如果存在 certbot.timer:
systemctl enable --now certbot.timer查看运行状态:
systemctl status certbot.timer显示 active (waiting) 说明定时任务正在等待下一次执行。
如果系统没有现成的 Certbot 自动续期机制,可以使用 Crontab。
crontab -e添加以下配置,每天凌晨3点检查证书:
0 3 * * * /usr/bin/certbot renew --quiet实际程序路径通过以下命令确认:
which certbot注意:Systemd Timer 和 Crontab 选择一种即可,避免重复配置。
8.5 测试自动续期
certbot renew --dry-run成功提示类似:
Congratulations, all simulated renewals succeeded默认情况下模拟续期不会执行 Deploy Hook,因此需要单独测试重载脚本:
/etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh8.6 完整自动续期流程
Systemd Timer / Crontab
↓
定期执行 certbot renew
↓
检查证书是否需要续期
↓
通过 HTTP-01 Webroot 验证域名
↓
Let's Encrypt 签发新证书
↓
更新 /etc/letsencrypt/live/ 下的证书链接
↓
执行 Deploy Hook
↓
检查 Nginx 配置
↓
平滑重载 Nginx
↓
新证书生效整个过程无需重启后端业务系统。
九、生产环境常用命令
查看 Certbot 日志:
tail -n 100 /var/log/letsencrypt/letsencrypt.log查看定时续期执行日志:
journalctl -u certbot.service检查证书续期配置:
cat /etc/letsencrypt/renewal/example.com.conf查看实际运行的 Nginx:
ps -ef | grep '[n]ginx: master'查看进程实际使用的可执行文件:
readlink -f /proc/进程PID/exe查看进程启动参数:
tr '\0' ' ' < /proc/进程PID/cmdline十、生产环境踩坑总结
十一、重点总结
1. 先定位实际运行的服务
不能仅根据默认安装目录判断服务运行情况。
应该按照以下顺序进行排查:
故障现象
↓
服务状态
↓
端口占用
↓
实际运行进程
↓
程序启动参数
↓
配置文件
↓
日志及相关依赖
↓
修复并验证2. 不要盲目重启生产服务
当磁盘配置与实际运行状态不一致时,直接重启服务可能导致原本正常运行的业务中断。
应先定位原因,再修改配置,检查语法,最后平滑重载。
3. 证书申请与业务访问应当解耦
ACME 验证请求直接通过 HTTP 返回验证文件,其他业务请求正常重定向到 HTTPS。
既能保证证书申请和续期,也不会改变正常业务访问方式。
4. 证书自动续期不等于自动生效
完整的证书自动化管理包含三个核心组成部分:
定时任务:负责触发续期。
Certbot:负责验证域名及更新证书。
Deploy Hook:负责重载 Nginx,使新证书生效。
5. 故障处理必须形成闭环
证书申请成功只是其中一个环节,还需要确认:
Nginx 已引用新证书。
网站实际提供的证书有效。
自动续期定时任务正常运行。
模拟续期测试成功。
自动重载脚本执行正常。
本次实际已确认成功申请证书,其余自动化配置需要执行并验证后,才能确认整个流程闭环。
最重要的生产排查经验:先根据现象提出假设,再通过命令逐步验证,定位真正的故障原因,最后实施修复,而不是根据经验盲目修改配置或重启服务。Nginx SSL 证书过期排查与 Certbot 自动续期实战
一、问题背景
某业务网站可以正常访问,但浏览器提示 SSL 证书已经过期。
登录 Ubuntu 服务器排查时,发现以下异常:
/etc/nginx/下找不到之前配置的业务域名。nginx -T只显示默认配置,没有 HTTPS 和反向代理配置。systemctl status nginx显示启动失败。网站依然可以正常访问。
本文统一使用 example.com 作为示例域名。
核心排查思路:先定位真正提供服务的 Nginx,再检查证书管理方式,最后解决证书申请和自动续期问题。
二、定位实际运行的 Nginx
2.1 查看 Nginx 完整配置
nginx -T该命令会检查配置语法,并输出主配置文件及所有通过 include 引入的配置文件。
发现只加载了以下配置:
/etc/nginx/nginx.conf
/etc/nginx/sites-enabled/default没有找到业务域名、SSL 证书及反向代理配置。
思考:为什么没有业务配置,网站却仍然可以访问?
存在以下几种可能:
Nginx 配置文件被删除,但运行中的进程仍使用内存中的旧配置。
服务器安装了多套 Nginx,当前查看的不是实际运行的实例。
网站由其他服务器或代理提供服务。
注意:nginx -T 读取的是磁盘上的配置文件,而不是运行中 Nginx 内存中的配置。
2.2 查看 Nginx 服务状态
systemctl status nginx.service发现以下错误:
Active: failed (Result: exit-code)
bind() to 0.0.0.0:80 failed (98: Unknown error)
still could not bind()Linux 错误码 98 对应 EADDRINUSE,表示地址或端口已经被占用。
思考:当前 Nginx 启动失败,但是网站仍能访问,说明很可能存在另一个正在运行的 Web 服务。
查看端口占用:
ss -lntp | grep -E ':(80|443)\s'此时不能盲目杀掉占用端口的进程,因为它可能就是正在提供业务服务的进程。
2.3 查看实际运行的 Nginx 进程
ps -ef | grep '[n]ginx: master'返回结果:
nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf至此确认,服务器存在两套 Nginx。
问题原因:之前执行的 nginx 命令指向 Ubuntu 安装的 Nginx,而实际业务系统使用的是宝塔安装的 Nginx。
2.4 查找真正的业务配置
查看完整配置:
/www/server/nginx/sbin/nginx -T \
-c /www/server/nginx/conf/nginx.conf搜索域名、证书及反向代理配置:
grep -RnE 'server_name|ssl_certificate|proxy_pass' \
/www/server/panel/vhost/nginx/最终定位到业务配置文件:
/www/server/panel/vhost/nginx/example.com.conf三、排查 SSL 证书管理方式
3.1 查看原有证书配置
cat /www/server/panel/vhost/nginx/example.com.conf发现原有证书路径:
ssl_certificate /www/server/nginx/ssl/example.com/fullchain.pem;
ssl_certificate_key /www/server/nginx/ssl/example.com/privkey.pem;查看证书有效期及签发机构:
openssl x509 \
-in /www/server/nginx/ssl/example.com/fullchain.pem \
-noout -dates -issuer其中:
notBefore:证书生效时间。
notAfter:证书过期时间。
issuer:证书签发机构。
3.2 检查 Certbot 是否管理证书
certbot certificates返回:
No certificates found.检查证书存储目录:
ls -lah /etc/letsencrypt/live/返回:
No such file or directory排查结论:
当前 Certbot 默认目录下没有任何证书管理记录,因此无法直接通过 certbot renew 完成续期。
之前的证书可能由宝塔、其他 ACME 工具申请,或者通过手动方式导入,具体来源尚未确认。
最终决定使用 Certbot 重新申请证书,并建立自动续期机制。
四、Certbot 证书申请方式
本次选择 Webroot 模式,原因如下:
不需要停止正在运行的 Nginx。
不影响正常业务访问。
不依赖 Certbot 自动识别宝塔 Nginx。
方便后续实现自动续期。
Webroot 工作原理
Certbot 创建临时验证文件
↓
存入指定 Webroot 目录
↓
Nginx 通过 HTTP 80 端口提供文件访问
↓
Let's Encrypt 通过公网访问验证文件
↓
确认域名控制权
↓
签发 SSL 证书五、证书申请失败与原因分析
5.1 创建验证目录
mkdir -p /var/www/certbot/.well-known/acme-challenge申请证书:
certbot certonly \
--webroot \
-w /var/www/certbot \
--cert-name example.com \
-d example.com \
-d www.example.com参数说明:
5.2 申请失败
报错信息:
Certbot failed to authenticate some domains
Type: tls
Fetching https://example.com/.well-known/acme-challenge/...
remote error: tls: handshake failure思考:Webroot 使用 HTTP-01 验证,为什么错误信息中出现了 HTTPS?
检查原有 Nginx 配置:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}发现所有 HTTP 请求都会被重定向到 HTTPS,包括 Certbot 的验证请求。
实际失败流程:
Let's Encrypt 发起 HTTP 验证请求
↓
Nginx 返回 301 重定向
↓
Let's Encrypt 访问 HTTPS 443
↓
HTTPS 握手失败
↓
无法获取验证文件
↓
证书申请失败注意:Let's Encrypt 支持一定条件下的 HTTP 到 HTTPS 重定向,并且不会检查重定向目标的证书有效期。因此,证书过期本身不一定导致验证失败。
本次日志明确显示的是 TLS 握手失败,具体握手失败原因没有进一步确认。
最简单可靠的解决方案是让 ACME 验证请求直接通过 HTTP 返回验证文件。
六、修复 Nginx 验证配置
6.1 备份原有配置
cp /www/server/panel/vhost/nginx/example.com.conf \
/root/example.com.conf.bak6.2 修改80端口配置
vim /www/server/panel/vhost/nginx/example.com.conf修改为:
server {
listen 80;
server_name example.com www.example.com;
# Certbot 域名验证
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type text/plain;
try_files $uri =404;
}
# 其他请求正常跳转 HTTPS
location / {
return 301 https://$host$request_uri;
}
}此时443端口的证书配置保持不变。
6.3 为什么必须将 return 301 放到 location 中?
Nginx 的 server 级别 rewrite 指令会在 location 匹配之前执行。
如果直接在 server 中配置 return 301,即使增加了 ACME 验证 location,请求也会提前被重定向。
修复后的请求流程:
HTTP 80 请求
|
├── /.well-known/acme-challenge/
| ↓
| 直接读取 Webroot 验证文件
| ↓
| 返回 HTTP 200
|
└── 其他请求
↓
301 重定向
↓
HTTPS 443这样既不影响正常 HTTPS 跳转,也能够完成证书申请和续期验证。
6.4 检查并重载 Nginx
/www/server/nginx/sbin/nginx -t \
-c /www/server/nginx/conf/nginx.conf && \
/www/server/nginx/sbin/nginx -s reload \
-c /www/server/nginx/conf/nginx.conf必须使用实际运行的 Nginx 程序,避免误操作另一套 Nginx。
6.5 手动验证 Webroot 路径
创建测试文件:
echo "success" > /var/www/certbot/.well-known/acme-challenge/test测试本机:
curl -i -H "Host: example.com" \
http://127.0.0.1/.well-known/acme-challenge/test测试域名:
curl -i http://example.com/.well-known/acme-challenge/test
curl -i http://www.example.com/.well-known/acme-challenge/test预期结果:
HTTP/1.1 200 OK
success如果本机访问正常,而公网访问异常,则进一步检查 DNS 解析、CDN、安全组及防火墙。
测试完成后删除测试文件:
rm /var/www/certbot/.well-known/acme-challenge/test6.6 重新申请证书
certbot certonly \
--webroot \
-w /var/www/certbot \
--cert-name example.com \
-d example.com \
-d www.example.com最终成功申请 SSL 证书。
七、配置新证书
7.1 Certbot 证书目录
/etc/letsencrypt/live/example.com/
├── cert.pem
├── chain.pem
├── fullchain.pem
└── privkey.pemNginx 通常使用 fullchain.pem 和 privkey.pem。
7.2 最终推荐的 Nginx 完整配置
server {
listen 80;
server_name example.com www.example.com;
# Certbot 证书申请和续期验证
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type text/plain;
try_files $uri =404;
}
# HTTP 跳转 HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com www.example.com;
# 直接引用 Certbot 管理的证书
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
client_max_body_size 100M;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
access_log /www/wwwlogs/example.com.log;
error_log /www/wwwlogs/example.com.error.log;
}推荐直接引用 Certbot 管理的证书,而不是手动复制到其他目录,避免续期成功后 Nginx 仍然引用旧证书。
检查并重载:
/www/server/nginx/sbin/nginx -t \
-c /www/server/nginx/conf/nginx.conf && \
/www/server/nginx/sbin/nginx -s reload \
-c /www/server/nginx/conf/nginx.conf7.3 验证网站实际使用的证书
echo | openssl s_client \
-connect example.com:443 \
-servername example.com 2>/dev/null \
| openssl x509 -noout -dates -subject -issuer通过检查网站实际对外提供的证书,确认新证书是否已经生效,而不是仅检查磁盘上的证书文件。
两个域名都应进行验证,并使用浏览器或 curl 确认 HTTPS 访问正常。
八、配置 Certbot 自动续期
8.1 手动续期命令
certbot renewCertbot 会自动检查所有已管理的证书,仅续期满足续期条件的证书。
模拟续期:
certbot renew --dry-run模拟续期用于测试域名验证流程,不会替换正式证书。
强制续期:
certbot renew --force-renewal生产环境不要频繁强制续期,避免触发 Let's Encrypt 签发频率限制。
8.2 为什么续期后必须重载 Nginx?
Nginx 在启动或重载配置时,会将静态配置的 SSL 证书和私钥加载到内存中。
Certbot 更新磁盘上的证书文件后,正在运行的 Nginx 不会自动重新加载证书。
因此需要配置 Deploy Hook,在证书续期成功后自动重载 Nginx。
8.3 创建自动重载脚本
直接执行以下命令:
mkdir -p /etc/letsencrypt/renewal-hooks/deploy/
cat > /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh <<'EOF'
#!/bin/bash
set -e
NGINX=/www/server/nginx/sbin/nginx
CONF=/www/server/nginx/conf/nginx.conf
# 检查 Nginx 配置
"$NGINX" -t -c "$CONF"
# 平滑重载 Nginx,使新证书生效
"$NGINX" -s reload -c "$CONF"
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.shCertbot 成功续期后会自动执行 Deploy Hook。
使用 set -e 可以避免配置检查失败后继续执行重载命令。
8.4 配置自动续期定时任务
检查是否已有定时任务:
systemctl list-timers --all | grep certbot如果存在 certbot.timer:
systemctl enable --now certbot.timer查看运行状态:
systemctl status certbot.timer显示 active (waiting) 说明定时任务正在等待下一次执行。
如果系统没有现成的 Certbot 自动续期机制,可以使用 Crontab。
crontab -e添加以下配置,每天凌晨3点检查证书:
0 3 * * * /usr/bin/certbot renew --quiet实际程序路径通过以下命令确认:
which certbot注意:Systemd Timer 和 Crontab 选择一种即可,避免重复配置。
8.5 测试自动续期
certbot renew --dry-run成功提示类似:
Congratulations, all simulated renewals succeeded默认情况下模拟续期不会执行 Deploy Hook,因此需要单独测试重载脚本:
/etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh8.6 完整自动续期流程
Systemd Timer / Crontab
↓
定期执行 certbot renew
↓
检查证书是否需要续期
↓
通过 HTTP-01 Webroot 验证域名
↓
Let's Encrypt 签发新证书
↓
更新 /etc/letsencrypt/live/ 下的证书链接
↓
执行 Deploy Hook
↓
检查 Nginx 配置
↓
平滑重载 Nginx
↓
新证书生效整个过程无需重启后端业务系统。
九、生产环境常用命令
查看 Certbot 日志:
tail -n 100 /var/log/letsencrypt/letsencrypt.log查看定时续期执行日志:
journalctl -u certbot.service检查证书续期配置:
cat /etc/letsencrypt/renewal/example.com.conf查看实际运行的 Nginx:
ps -ef | grep '[n]ginx: master'查看进程实际使用的可执行文件:
readlink -f /proc/进程PID/exe查看进程启动参数:
tr '\0' ' ' < /proc/进程PID/cmdline十、生产环境踩坑总结
十一、重点总结
1. 先定位实际运行的服务
不能仅根据默认安装目录判断服务运行情况。
应该按照以下顺序进行排查:
故障现象
↓
服务状态
↓
端口占用
↓
实际运行进程
↓
程序启动参数
↓
配置文件
↓
日志及相关依赖
↓
修复并验证2. 不要盲目重启生产服务
当磁盘配置与实际运行状态不一致时,直接重启服务可能导致原本正常运行的业务中断。
应先定位原因,再修改配置,检查语法,最后平滑重载。
3. 证书申请与业务访问应当解耦
ACME 验证请求直接通过 HTTP 返回验证文件,其他业务请求正常重定向到 HTTPS。
既能保证证书申请和续期,也不会改变正常业务访问方式。
4. 证书自动续期不等于自动生效
完整的证书自动化管理包含三个核心组成部分:
定时任务:负责触发续期。
Certbot:负责验证域名及更新证书。
Deploy Hook:负责重载 Nginx,使新证书生效。
5. 故障处理必须形成闭环
证书申请成功只是其中一个环节,还需要确认:
Nginx 已引用新证书。
网站实际提供的证书有效。
自动续期定时任务正常运行。
模拟续期测试成功。
自动重载脚本执行正常。
本次实际已确认成功申请证书,其余自动化配置需要执行并验证后,才能确认整个流程闭环。
最重要的生产排查经验:先根据现象提出假设,再通过命令逐步验证,定位真正的故障原因,最后实施修复,而不是根据经验盲目修改配置或重启服务。
