跳到主要内容
错误排查

证书已过期或尚未生效怎么排查?Nginx/Apache 时间、续期与 OCSP 修复指南

证书已过期或尚未生效 — 证书已过期或尚未生效怎么排查?Nginx/Apache 时间、续期与 OCSP 修复指南
核心结论

证书已过期或尚未生效的直接原因是当前客户端 UTC 时间不在证书 notBefore/notAfter 区间内;先用 `openssl x509 -noout -dates` 与 `date -u` 对比,时间偏差超过 5 分钟就要先校时。 线上证书日期用 `echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates -serial` 读取,和服务器文件 `/etc/letsencrypt/live/example.com/fullchain.pem` 的 serial 对比,不一致通常是未 reload。 Let's Encrypt 默认 90 天有效期,公共 TLS 证书最长 398 天;Certbot 2.x 用 `certbot renew --dry-run` 验证续期,部署钩子执行 `nginx -t && systemctl reload nginx`。 未生效错误常见于新证书刚签发但客户端时钟慢、CDN 边缘节点未同步或证书链缺失;OpenSSL 报 `certificate is not yet valid` 时先查 `notBefore` 和 NTP/chrony。 监控建议用 blackbox_exporter 的 `probe_ssl_earliest_cert_expiry` 或 `certbot certificates`,剩余天数低于 14 天告警,低于 7 天按 P1 处理。

问题背景与根本原因:浏览器为什么提示证书已过期或尚未生效?

用户搜“证书已过期或尚未生效”时,真正想知道的是:明明刚续了证书,为什么浏览器还报错?答案藏在 TLS 握手的证书校验环节。据 RFC 5280 第 4.1.2.5 节,客户端会取证书 Validity 字段里的 notBefore 与 notAfter,与本地当前时间比较;这两个时间都是 UTC,不带时区偏移。当前时间早于 notBefore,浏览器报“尚未生效”;晚于 notAfter,报“已过期”。也就是说,同一个 notAfter 值,在 UTC+8 的运维人员眼里可能“还有 1 天”,在 UTC 判断标准下已经失效。

好消息是错误码本身就能把排查范围缩小一半,不必先怀疑 CA:

客户端 典型报错文本 含义 优先排查方向
Chrome / Edge NET::ERR_CERT_DATE_INVALID 已过期或尚未生效(两者共用) 服务端实际下发证书的 notBefore/notAfter
Firefox SEC_ERROR_EXPIRED_CERTIFICATE notAfter 已过 续期任务、reload 是否生效
Firefox MOZILLA_PKIX_ERROR_NOT_YET_VALID_CERTIFICATE notBefore 在未来 客户端时钟、新证书生效时间差
curl / OpenSSL SSL certificate problem: certificate has expired 已过期 用 -v 看实际链与时间
curl / OpenSSL certificate is not yet valid 尚未生效 本机 date -u 与服务端证书

拿到报错后,按下面三步把“服务端证书时间”“UTC 当前时间”“客户端时间”三者对齐,这一步就能区分是证书真过期,还是判断标准被时区或时钟带偏:

# 1) 抓取线上实际下发的叶子证书有效期(注意 -servername,SNI 多站点必需)
openssl s_client -connect example.com:443 -servername example.com /dev/null 
  | openssl x509 -noout -subject -issuer -dates -serial
# 输出示例(时间一律为 GMT):
# notBefore=Mar  1 00:00:00 2025 GMT
# notAfter=May 30 23:59:59 2025 GMT

# 2) 用 UTC 当前时间做基准,而不是本地日历日期(OpenSSL 1.0.2+ 支持 -checkend)
date -u +"%Y-%m-%dT%H:%M:%SZ"
openssl x509 -noout -checkend 0 -in /etc/letsencrypt/live/example.com/cert.pem
# 退出码 0 = 未过期;1 = 已过期(0 表示“0 秒后是否过期”,即当前状态)

# 3) 检查本机证书文件与线上是否一致,排除“已续期但未 reload”
openssl x509 -noout -enddate -in /etc/letsencrypt/live/example.com/cert.pem
nginx -t && nginx -s reload      # Apache 对应:apachectl configtest && systemctl reload apache2

有效期规则决定了过期窗口有多窄。据 CA/Browser Forum 基线要求(Ballot 193,2020-09-01 生效),公开信任 TLS 证书最长 398 天;据 Let’s Encrypt 文档,其默认只签发 90 天证书,并建议每 60 天续期一次。这意味着自动续期连续失败一两次,就会直接掉进过期区间——所以生产事故的根因几乎从不是 CA 宕机,而是续期链路断了:HTTP-01 验证的 80 端口被安全组拦截、DNS-01 的 API Token 过期或权限被回收、certbot renew 的 cron 被注释掉。更隐蔽的是“证书已更新但服务未 reload”,Nginx/Apache 只在启动或 reload 时读取证书文件,磁盘上的新证书对运行中的 worker 不可见;在负载均衡场景下,还常见“多节点只更新了一台”,导致请求随机命中旧节点。另外注意,CA/B Forum 已通过 SC-081,计划从 2026-03-15 起把上限压到 200 天、2027 年 100 天、2029 年 47 天,续期自动化的容错余量会越来越小。

客户端侧的“尚未生效”多半不是证书问题:系统时间漂移、虚拟机快照恢复后时钟回退、IoT 或老设备 RTC 电池失效,都会让本地时间落到 notBefore 之前。服务端也可能制造同样的假象——新证书的 notBefore 恰好比当前时间晚几分钟(CA 签发时间与本地时钟的微小偏差),或 CDN 边缘节点仍缓存着旧证书。判断方法始终是同一条:把 date -u 的结果与 openssl x509 -dates 的 GMT 值直接比较,而不是看本地日历上“还剩几天”。

完整实操步骤:从报错到修复的 6 步排查流程

浏览器报 net::ERR_CERT_DATE_INVALID、Nginx 日志里出现 SSL_CTX_use_certificate 相关告警、或客户端提示”证书已过期或尚未生效”,这三处措辞不同,但排查路径是同一条:先确认**是哪一层的时间判断出问题**——磁盘上的证书文件、线上实际下发的证书、还是服务器/客户端系统时钟。据 RFC 5280 §4.1.2.5,有效期由 notBefore 与 notAfter 两个字段定义,校验方任一侧时钟越界都会报同一类错误。下面 6 步按”证据→文件→线上→时钟→续期→部署”顺序执行,每步都留下可比对的字段。

  1. 固定证据:记下浏览器完整错误码与发生时间(精确到分钟,便于和日志对齐)。命令行同时抓一次握手摘要:
    # curl 7.81.0(2022-01)起,-v 输出会打印 start date / expire date 两行
    curl -vI https://www.example.com 2>&1 | grep -Ei 'start date|expire date|subject:|issuer:'
    # 低于 7.81 的版本没有这两行,直接跳到第 3 步用 openssl s_client 取
    
  2. 查文件:确认磁盘上那份证书本身有没有过期。OpenSSL 1.1.1 与 3.x 均支持以下参数:
    # serial 记后 8 位,用于和第 3 步线上证书肉眼比对
    openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem 
      -noout -subject -issuer -dates -serial
    

    注意 fullchain.pem 首段才是站点证书,若只看到中间 CA 的日期说明证书链顺序被改过。

  3. 查线上:文件对不代表客户端拿到的对。-servername 必须保留,否则 SNI 虚拟主机可能返回默认站点证书:
    echo | timeout 10 openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
      | openssl x509 -noout -dates -serial -subject
    
    # 多节点(LB 后各后端)逐台执行,SNI 一律保留
    for ip in 10.0.0.11 10.0.0.12; do
      echo "== $ip"
      echo | timeout 10 openssl s_client -connect $ip:443 -servername example.com 2>/dev/null 
        | openssl x509 -noout -dates -serial
    done
    
  4. 查时间:偏差超过 5 分钟就足以触发”证书已过期或尚未生效”,先修时钟再复测:
    date -u
    timedatectl status | grep -E 'System time|NTP|synchronized'
    chronyc tracking    # 看 System time 一行,单位秒
    
    # 修正:二选一
    sudo timedatectl set-ntp true   # 容器/精简镜像默认常为 no,需手动打开
    sudo chronyc makestep           # 立即跳变;Debian/Ubuntu 的 chrony.conf 默认 makestep 1.0 3,仅前 3 次校正允许跳变
    
  5. 查续期:确认任务真的跑过、且下一次触发时间存在。据 Let’s Encrypt 文档,证书有效期 90 天、官方建议每 60 天续期:
    certbot renew --dry-run --cert-name example.com   # Certbot 2.x,--dry-run 走 staging,不消耗速率限制
    acme.sh --cron --home /root/.acme.sh              # acme.sh 3.0.x
    systemctl list-timers | grep -E 'certbot|acme'    # 无输出说明只有 cron:查 /etc/cron.d/certbot
    
  6. 部署与 reload:确认 ssl_certificate 指向 fullchain.pem(不是 cert.pem)后重载,reload 不等于重启,不会断连接:
    sudo nginx -t && sudo systemctl reload nginx
    sudo apachectl configtest && sudo systemctl reload apache2
    # 重载后重跑第 3 步,serial 未变即说明新证书没被加载(路径错/软链未更新)
    

把第 2、3 步的 serial 一对照,故障层基本就定了:

本地文件 serial 线上 s_client serial 结论与下一步
与线上一致,但 notAfter 已过 同左 续期从未成功 → 回到第 5 步查 dry-run 报错
新证书 serial 旧证书 serial 未部署或未 reload,多节点常见于只更新了一台 → 第 6 步
与线上一致且未过期 同左 服务端无问题,是客户端或中间设备时钟偏差 → 第 4 步

OCSP 相关补充:Mozilla Server Side TLS 建议开启 stapling(Nginx ssl_stapling on; 默认 off)。但 Let’s Encrypt 已停止提供 OCSP 响应、仅保留 CRL,对 LE 证书开 stapling 会拉取失败——它只影响握手体积,不会造成”证书已过期或尚未生效”的报错,别把它当成本次故障的根因。

配置/命令示例:OpenSSL 3.x、Certbot 2.x、Nginx 1.24 可复制片段

排查「证书已过期或尚未生效」时,最耗时的往往不是判断,而是把检查、续期、重载、校时四步串成可复制的命令。下面所有片段均基于 OpenSSL 3.x、Certbot 2.x、Nginx 1.24/1.26 与 acme.sh 3.0.7 实测,可直接粘贴使用。

第一步:判断是”已过期”还是”即将过期”。据 OpenSSL 官方 x509 手册,-checkend 在证书于指定秒数内仍有效时返回 0,将过期时返回 1,配合 shell 的 || 即可做前置告警:

# 返回 0 = 24 小时内不会过期;返回 1 = 即将过期或已过期
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -checkend 86400

# 改成 604800(7 天)可提前一周告警,适合放进 cron 每日巡检
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -checkend 604800 
  || echo "WARN: 证书 7 天内过期,检查 certbot renew 日志" >&2

# 同时看绝对时间,确认 notBefore/notAfter 与服务器时钟是否矛盾
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -dates

第二步:Nginx 1.24/1.26 最小可用配置。关键点是 ssl_certificate 必须指向 fullchain.pem,指向 cert.pem 会导致 Android、部分 Java 客户端报 unable to get local issuer certificate,因为只下发了叶子证书。自 1.23.4 起 ssl_protocols 默认值已为 TLSv1.2 TLSv1.3,ssl_session_timeout 默认为 5m,显式写出便于审计:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;  # 必须 fullchain,不是 cert.pem
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;   # 1.23.4+ 的默认值
    ssl_session_timeout 1d;
}

第三步:让续期后自动重载。Certbot 2.x 会执行 /etc/letsencrypt/renewal-hooks/deploy/ 下的所有可执行脚本,仅当证书真的更新时才触发:

sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh >/dev/null <<'EOF'
#!/bin/sh
nginx -t && systemctl reload nginx
EOF
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

# 临时单次重载,可写在 renew 命令上
sudo certbot renew --deploy-hook 'systemctl reload nginx'

用 acme.sh 3.0.7 时,--install-cert 自带 --reloadcmd,续期后同样会执行:

acme.sh --install-cert -d example.com 
  --key-file       /etc/nginx/ssl/example.com.key 
  --fullchain-file /etc/nginx/ssl/example.com.pem 
  --reloadcmd      'systemctl reload nginx'
方式 触发时机 配置位置 适用场景
Certbot deploy 钩子脚本 证书实际更新后 /etc/letsencrypt/renewal-hooks/deploy/ 多服务共用一张证书,需统一重载
certbot renew --deploy-hook 仅本次 renew 命令行参数 临时验证、CI 流水线
acme.sh --reloadcmd 每次 install/renew 后 acme.sh 账户配置内 不使用 Certbot 的环境

第四步:校时。「尚未生效」(notBefore 在未来)几乎都是时钟漂移。chrony 1.37+ 在 /etc/chrony.conf 中写 server ntp.aliyun.com iburst 与 makestep 1.0 3(偏移超过 1 秒且处于前 3 次更新时直接跳变而非缓慢调整);使用 systemd-timesyncd 则执行 timedatectl set-ntp true,再用 timedatectl show-timesync --all 查看 ServerName 与 PollIntervalMaxUSec 是否正常。

第五步:验证证书链完整性。据 Mozilla SSL Configuration Generator 的建议,链不全时应优先修 Nginx 配置而非客户端:

# 期望输出:fullchain.pem: OK
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt 
  /etc/letsencrypt/live/example.com/fullchain.pem

# 若失败,看服务器实际下发了几个证书(应 ≥2:叶子 + 中间证书)
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 
  | grep -c 'BEGIN CERTIFICATE'

参数对比与方案选型:Certbot、acme.sh、Caddy、云托管证书怎么选?

单机跑 Nginx/Apache,到底该选 Certbot 还是 acme.sh?先看一张对比表——很多“证书已过期或尚未生效”的告警,根因并不是续期失败,而是一开始就选错了工具链(比如在无 80 端口的机器上硬上 HTTP-01)。

方案 适用场景 关键命令/配置 自动续期 备注
Certbot 2.11 Nginx/Apache 单机 certbot renew --dry-run systemd timer 每日 2 次 需配 deploy-hook 才会 reload
acme.sh 3.0.7 无 80 端口 / 多 DNS API acme.sh --cron cron 每日 默认剩余约 60 天触发续期
Caddy 2.8 反向代理自动 HTTPS caddy reload 内置 ACME 适合新部署,无需外部定时器
云厂商托管证书 负载均衡 / CDN 控制台部署 平台自动 多节点需确认同步

Certbot 2.x 的续期参数怎么确认?据 Let’s Encrypt 文档,证书有效期为 90 天,官方建议在剩余 30 天前完成续期;Certbot 的 renew_before_expiry 默认值即为 30 天(写在 /etc/letsencrypt/renewal/*.conf),而 certbot.timer 每天运行两次并带随机延迟,避免整点集中冲击 ACME 服务端。

# Certbot 2.11+:staging 环境演练,验证 ACME 链路与 hook 是否可用
certbot renew --dry-run --deploy-hook "systemctl reload nginx"

# 列出全部证书的到期时间与续期配置来源
certbot certificates

# 查看定时器是否真的在跑(默认 certbot.timer,每天 2 次 + 随机延迟)
systemctl list-timers certbot.timer

acme.sh 3.x 默认什么时候续?默认 CA 已从 Let’s Encrypt 切换为 ZeroSSL,续期阈值由 --days 控制,默认 60 天(即剩余约 60 天时触发),比 Certbot 更保守。DNS-01 是它相对 Certbot 的核心优势:无需 80 端口,群晖、OpenWrt、内网机器都能签。

# 用 DNS API 签证书(Cloudflare 示例),ec-256 更省握手开销
acme.sh --issue -d example.com --dns dns_cf --keylength ec-256

# 关键:安装证书并绑定 reload 命令,否则新证书不会被 Nginx 读取
acme.sh --install-cert -d example.com 
  --key-file /etc/nginx/ssl/example.com.key 
  --fullchain-file /etc/nginx/ssl/example.com.pem 
  --reloadcmd "systemctl reload nginx"

# 手动触发一次 cron 逻辑,观察输出
acme.sh --cron

Caddy 2.8 为什么能省掉 reload?Caddy 内置 ACME 客户端,默认在证书剩余约 1/3 生命周期时自动续期,并在内存中热加载,不存在“续期成功但进程未 reload”这一常见坑。配置只需两行:

example.com {
    reverse_proxy 127.0.0.1:8080
}
# 改完配置后:caddy validate --config /etc/caddy/Caddyfile && caddy reload --config /etc/caddy/Caddyfile

多节点部署怎么确认证书真的同步了?CDN 或负载均衡场景,后端每一台机器的证书 serial 必须一致,否则部分请求会命中旧证书,表现为间歇性的“证书已过期或尚未生效”。用 openssl 批量抽查:

# 逐个后端节点取 serial 与有效期,对比是否一致
for ip in 10.0.0.11 10.0.0.12; do
  echo | openssl s_client -connect $ip:443 -servername example.com 2>/dev/null 
    | openssl x509 -noout -serial -dates
done

成本区间参考:Let’s Encrypt 与 ZeroSSL 免费;DigiCert、GlobalSign 单域名 OV 约 1000–3000 元/年,EV 更高;云厂商托管证书按域名与套餐计费,常见 0–2000 元/年。选型时把“自动部署 + 到期告警”能力折算进成本,比只看证书单价更接近真实 TCO。

结论性建议:单机 Nginx/Apache 优先 Certbot 2.x,文档最全、排错资料最多;多 DNS 服务商、无 80 端口或群晖/OpenWrt 场景选 acme.sh 3.x 的 DNS-01;新项目直接上 Caddy 2.8,从架构上消除 reload 遗漏;负载均衡/CDN 场景用云托管证书,但必须把 serial 一致性抽查写进巡检脚本。

常见错误与排查:这些真实报错分别怎么处理?

浏览器报 NET::ERR_CERT_DATE_INVALID,清缓存有用吗?没用。这是 Chromium(Chrome/Edge)对”证书有效期与系统时间不匹配”的专用错误码,含义只有一种。先点地址栏锁图标 →「连接不安全」→「证书无效」,看「有效期」字段;再用下面的命令读线上真实证书。-servername 必须写,否则 SNI 虚拟主机可能返回默认站点的另一张证书,据 OpenSSL 文档,缺省不发 SNI 扩展:

# OpenSSL 1.1.1 / 3.x 通用;443 为默认端口,2>/dev/null 过滤握手噪声
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates -serial
# 输出示例
# notBefore=Mar  1 00:00:00 2025 GMT
# notAfter=May 30 23:59:59 2025 GMT   ← 早于当前 UTC 时间即已过期

若 notAfter 已过,动作是续期后 systemctl reload nginx;Let’s Encrypt 文档明确要求部署新证书后必须 reload,certbot 可用 --deploy-hook "systemctl reload nginx" 自动化。清缓存不会改变服务端证书。

Firefox 的 SEC_ERROR_EXPIRED_CERTIFICATE 为什么连”继续访问”都不给?Mozilla 的 CA 证书策略对过期证书直接硬阻断,没有绕过按钮。确认命令:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -enddate
# notAfter=May 30 23:59:59 2025 GMT

反过来报 certificate is not yet valid / MOZILLA_PKIX_ERROR_NOT_YET_VALID_CERTIFICATE 呢?这是”证书已过期或尚未生效”的另一半:要么客户端时钟慢,要么新证书的 notBefore 在未来(常见于跨时区服务器签发后立即上线)。对比两端:

date -u                                   # 客户端 UTC 时间
openssl x509 -noout -startdate -in /etc/nginx/ssl/fullchain.pem
# 偏差 > 5 分钟先校时,别急着换证书
sudo chronyc tracking                     # chrony 默认;看 System time 偏移
sudo timedatectl set-ntp true             # systemd 系发行版

curl: (60) SSL certificate problem: unable to get local issuer certificate 是证书过期吗?不是,这是链不完整——服务器只发了叶证书、没发中间 CA。修法是把 Nginx 的 ssl_certificate 从 cert.pem 改为 fullchain.pem(Apache 对应 SSLCertificateChainFile),再验证:

openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt fullchain.pem
# fullchain.pem: OK

文件明明换了,线上还是旧证书?比 serial,别比文件名:

openssl x509 -noout -serial -in /etc/nginx/ssl/fullchain.pem          # 本地
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -serial                                       # 线上
# 不一致 → 进程未 reload / 容器挂载旧卷 / 负载均衡仍指向旧后端
sudo nginx -t && sudo systemctl reload nginx

Java / Python 客户端报 CertificateExpiredException 或 SSLCertVerificationError: certificate has expired 怎么查?先排客户端侧:容器基础镜像常冻结时间,JVM 也可能用独立 truststore 而非系统 CA。JDK 8u301+ / 11+ 可直接查看信任库(默认口令 changeit):

keytool -list -v -keystore "$JAVA_HOME/lib/security/cacerts" -storepass changeit 
  | grep -E "Alias name|Valid from"
python3 -c "import ssl; print(ssl.get_default_verify_paths())"   # 确认 Python 用哪套 CA
报错文本 真实根因 首条命令 修复动作
NET::ERR_CERT_DATE_INVALID notAfter 已过或时钟偏差 s_client | x509 -dates 续期 + reload
SEC_ERROR_EXPIRED_CERTIFICATE Firefox 硬阻断过期证书 x509 -noout -enddate 更新证书
certificate is not yet valid notBefore 在未来 / 客户端慢 date -u 对比 -startdate 校时或等待生效
curl (60) unable to get local issuer 中间 CA 缺失 openssl verify 换 fullchain.pem
文件新、线上旧 未 reload / 挂载旧文件 对比 -serial reload 或修挂载
CertificateExpiredException 客户端时钟或旧 truststore keytool -list -v 校时、升级 CA 包

RFC 5280 §4.1.2.5 规定 notBefore/notAfter 一律为 UTCTime/GeneralizedTime 的 GMT 值,所以任何本地时区解读都不该出现在判断里——统一用 date -u 对齐。

效果验证方法:如何确认证书日期、链和 reload 都已生效?

改完证书文件、也执行过 systemctl reload nginx,为什么浏览器还报”证书已过期或尚未生效”?答案通常藏在三个地方:进程是否真的重新读取了文件、链是否完整、以及新旧证书的 serial 是否一致。下面这套验证顺序建议从外到内执行,先看线上实际吐出的证书,再回头看本地文件。

第一步:核对线上证书的有效期与序列号。这是最直接的日期验证,注意必须带 -servername,否则 SNI 站点会拿到默认 server 块的证书:

# 取线上证书的 notBefore / notAfter / serial / subject
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -dates -serial -subject
# 对照本地文件,serial 必须一致,否则说明 reload 未生效或 reload 到了错误实例
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -serial -dates
date -u   # notBefore 不得晚于当前 UTC,notAfter 必须晚于当前 UTC

据 OpenSSL 文档,s_client 默认使用系统信任库校验证书链,但不会因校验失败而改变退出码,所以要用 -verify_return_error 让它”校验失败即报错”:

openssl s_client -connect example.com:443 -servername example.com 
  -verify_return_error </dev/null
# 末尾必须出现:Verify return code: 0 (ok)
Verify return code 含义 优先排查
0 (ok) 链完整、有效期正常 无需处理
10 证书链不完整(缺中间证书) 是否误用 cert.pem 而非 fullchain.pem
20 无法获取本地签发者证书 服务器未下发中间证书
21 证书尚未生效 / 已过期 notBefore、notAfter、系统时钟
62 主机名不匹配 SNI、server_name、证书 SAN

第二步:HTTP 层与协议版本验证。证书通过 OpenSSL 校验后,再确认 curl 层面无告警,并覆盖 Nginx ssl_protocols 中开启的各版本(默认 TLSv1.2 TLSv1.3):

curl -vI https://example.com --max-time 10        # 不应出现 SSL certificate problem
curl --tlsv1.2 --tls-max 1.2 -I https://example.com   # 单独测 TLS 1.2
curl --tlsv1.3 -I https://example.com                 # 单独测 TLS 1.3

第三步:OCSP stapling 是否真的生效。配置开启不等于装订成功,Nginx 需要能访问 OCSP 响应器:

# nginx.conf 的 http 或 server 块
ssl_stapling on;                 # 默认 off
ssl_stapling_verify on;          # 默认 off,会顺带校验证书链
resolver 223.5.5.5 valid=300s;   # 必须有 resolver,否则 stapling 静默失败
resolver_timeout 5s;

# 验证:应看到 "OCSP Response Status: successful"
openssl s_client -connect example.com:443 -status /dev/null 
  | grep -A3 'OCSP response'

第四步:续期结果验证。据 Let’s Encrypt 文档,Certbot 在到期前 30 天自动续期;确认剩余天数与下次续期时间:

certbot certificates                    # 关注 VALID: 89 days 一类的剩余天数
acme.sh --list                          # 关注 NextRenewTime 列
certbot renew --dry-run                 # 纳入变更窗口,确认 80/443 或 DNS API 可用

第五步:监控落地。Prometheus blackbox_exporter 0.25+ 的 ssl 探针会暴露 probe_ssl_earliest_cert_expiry(Unix 时间戳):

# 告警规则:剩余不足 14 天
- alert: SSLCertExpiringSoon
  expr: probe_ssl_earliest_cert_expiry - time() < 14*24*3600
  for: 1h

没有 Prometheus 时,用 -checkend 做每日兜底,参数单位是秒(1209600 = 14 天),退出码 1 表示即将过期:

# crontab: 0 8 * * * /usr/local/bin/cert-check.sh
openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -checkend 1209600 
  || echo "证书将在 14 天内过期" | mail -s "TLS 告警" ops@example.com

优先怀疑线上进程或节点仍在用旧证书。用 `openssl s_client -connect example.com:443 -servername example.com | openssl x509 -noout -serial -dates` 读取线上 serial,再与 `fullchain.pem` 的 serial 对比;不一致时检查 Nginx/Apache 是否 reload、容器是否挂载旧文件、CDN 边缘是否未同步。

多数不是 CA 问题,而是客户端时间慢、notBefore 比当前时间晚几分钟,或把 GMT 当成北京时间看错日期。执行 `date -u` 和 `openssl x509 -noout -startdate` 对比,偏差超过 5 分钟先启用 NTP/chrony 校时,再复测浏览器。

公开信任 TLS 证书最长 398 天,Let's Encrypt 默认 90 天,商业 CA 常见 398 天;换长有效期不能替代自动续期。单机场景可用 Certbot 2.x 的 systemd timer 加 deploy-hook,多 DNS 场景可用 acme.sh 3.x 的 DNS-01,把续期和 reload 做成可验证流程更关键。

单机 Nginx/Apache 优先 Certbot 2.x,`certbot renew –dry-run` 和 `certbot certificates` 排查直观;无 80 端口、多 DNS 服务商或群晖/OpenWrt 场景优先 acme.sh 3.x,用 `–install-cert` 配 `–reloadcmd` 自动部署。两者都免费,差别主要在 DNS API 覆盖和部署钩子写法。

先执行 `nginx -T | grep ssl_certificate` 确认当前配置指向的证书路径,再比对线上 serial 与文件 serial。若不一致,检查是否有容器挂载旧文件、负载均衡仍指向旧后端、SNI 默认 server 返回了错误证书;多节点要对每个 IP 执行 `openssl s_client -connect IP:443 -servername 域名`。

通常没用,该错误来自证书有效期校验,不是页面缓存。先查服务端 notAfter 和客户端系统时间;只有确认客户端时间错误时才校时,只有确认服务端证书已更新时才 reload 或同步 CDN。

有 Prometheus 时用 blackbox_exporter 0.25+ 的 `probe_ssl_earliest_cert_expiry`,规则设为剩余小于 14 天告警;没有监控系统时用 `openssl x509 -checkend 1209600` 每日 cron 检查,返回 1 即 14 天内将过期。剩余小于 7 天建议按 P1 处理并人工介入续期。


本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。