参考资料

  1. 实现HTTPS自动跳转的详细说明
  2. nginx ssl配置步骤
  3. windows 配置ssl证书详细说明以及案例
  4. 哪些行业特别需要SSL证书
  5. Apache服务器SSL配置
  6. 免费SSL证书的获取途径
  7. SSL证书的费用与成本
  8. 如何管理多个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)