跳到主要内容

证书过期应急处理

问题

监控告警显示 SSL 证书即将过期(或已过期),用户看到浏览器安全警告,如何紧急处理?

答案

应急流程

Step 1:确认证书状态

# 查看远程服务器证书信息
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | \
openssl x509 -noout -dates -subject
# notBefore=Jan 15 00:00:00 2024 GMT
# notAfter=Apr 15 23:59:59 2024 GMT ← 过期时间
# subject=CN = example.com

# 查看本地证书文件
openssl x509 -in /etc/ssl/certs/example.crt -noout -dates

# 批量检查多个域名
for domain in example.com api.example.com admin.example.com; do
EXPIRY=$(echo | openssl s_client -connect "$domain:443" -servername "$domain" 2>/dev/null | \
openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
echo "$domain$EXPIRY"
done

Step 2:紧急续期

# Let's Encrypt 续期
certbot renew --force-renewal

# 如果自动续期失败,手动签发
certbot certonly --nginx -d example.com -d www.example.com

# 续期后重载 Nginx
nginx -t && nginx -s reload

# 验证新证书
curl -vI https://example.com 2>&1 | grep -E "expire|subject"

Step 3:商业证书续期

# 1. 生成新的 CSR
openssl req -new -newkey rsa:2048 -nodes \
-keyout /etc/ssl/private/example.key \
-out /etc/ssl/certs/example.csr \
-subj "/CN=example.com/O=MyCompany"

# 2. 提交 CSR 给 CA(DigiCert / GeoTrust 等)
# 3. CA 签发后下载证书
# 4. 部署新证书
cat example.crt intermediate.crt > fullchain.crt
cp fullchain.crt /etc/nginx/ssl/
cp example.key /etc/nginx/ssl/

# 5. 重载
nginx -t && nginx -s reload

预防措施

# 1. 设置证书过期监控(30 天、7 天、1 天告警)
# Prometheus Blackbox Exporter 配置
# probe_ssl_earliest_cert_expiry - time() < 30*86400

# 2. Let's Encrypt 自动续期定时任务
# /etc/cron.d/certbot
0 3 * * * root certbot renew --quiet --post-hook "nginx -s reload"

# 3. 证书到期日历提醒
# 将所有证书到期日期记录到共享日历

常见面试问题

Q1: 如何建立证书管理体系,避免过期?

答案

  1. 统一管理:用证书管理平台(如 HashiCorp Vault、cert-manager)统一签发和续期
  2. 自动续期:Let's Encrypt + certbot/cert-manager 自动化
  3. 监控告警:Blackbox Exporter 或脚本每天检查证书有效期,30/7/1 天三级告警
  4. CMDB 记录:所有证书信息(域名、到期时间、CA、负责人)录入 CMDB
  5. 流程规范:证书续期 SOP,有主备负责人

相关链接