参考资料

  1. SSL证书的必要性
  2. SSL证书的费用与成本
  3. 如何管理多个SSL证书
  4. 实现HTTPS自动跳转的详细说明
  5. 如何配置服务器强制使用 HTTPS?
  6. 如何申请SSL证书
  7. 如何选择适合的SSL证书类型
  8. ssl证书不受信任问题详解

如何检查 SSL 证书是否过期

如何检查 SSL 证书是否过期

详细说明介绍

SSL 证书过期会导致网站无法访问或浏览器警告。检查证书过期时间可提前续期,避免服务中断。

适用操作系统

  • Linux/Unix

  • Windows

  • macOS

证书有效性检查方法

  1. OpenSSL 命令(Linux/macOS)

    openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

    输出示例:

    notBefore=Jan 1 00:00:00 2023 GMT  
    notAfter=Dec 31 23:59:59 2023 GMT
  2. PowerShell(Windows)

    $cert = [System.Net.Security.SslStream]::new([System.Net.Sockets.TcpClient]::new("example.com", 443).GetStream()).RemoteCertificate
    $cert.GetExpirationDateString()
  3. 浏览器检查

    • Chrome/Firefox: 点击地址栏锁图标 → "证书" → 查看有效期

配置示例(自动监控脚本)

#!/bin/bash
end_date=$(openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
expiry_epoch=$(date -d "$end_date" +%s)
current_epoch=$(date +%s)
days_left=$(( (expiry_epoch - current_epoch) / 86400 ))
echo "证书剩余天数: $days_left"

调试与验证

注意事项

  1. 时区问题:证书时间以 GMT 为准

  2. 证书链完整性:需检查中间证书是否过期

  3. 提前续期:建议在到期前 30 天处理

  4. 多域名检查:SAN 证书需验证所有域名

  5. 服务重启:更新证书后需重启 Web 服务(如 Nginx/Apache)