如何检查SSL证书有效期(不再被打个措手不及)
证书过期会让整个网站瞬间瘫痪,而原因永远是"我们忘了续期"。下面介绍如何查看到期日期,以及如何避免被它突袭。
在命令行中
# 打印在线服务器的 notBefore / notAfter
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -dates
# 仅到期时间(便于脚本)
openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -enddate
在 Windows PowerShell 中:
$c = [Net.Sockets.TcpClient]::new('example.com',443)
$s = [Net.Security.SslStream]::new($c.GetStream())
$s.AuthenticateAsClient('example.com')
$s.RemoteCertificate.GetExpirationDateString()
30天法则
公开证书正变得越来越短命——CA/Browser Forum已同意到2029年将最长有效期缩短至47天。手动续期跟不上节奏,因此:
| 有效期 | 续期时机 | 方式 |
|---|---|---|
| 90天(Let's Encrypt) | 第60天 | ACME自动化 |
| 1年 | 到期前30天 | 自动化+告警 |
将监控设置为在剩余30天时告警,这与本站 SSL检测 标为琥珀色的阈值一致。长久之计是自动化——参见 ACME与Let's Encrypt。
提示: 请检查边缘(CDN/负载均衡)实际提供的证书,而不仅仅是源站——两者可能不同。