一文搞懂 HTTPS 证书:从原理到自动续签
Understanding HTTPS Certificates: From Principles to Auto-Renewal
| David | 2026-08-31T17:57:28
很多开发同学知道要用 HTTPS 但不清楚证书具体是怎么回事。这篇文章从证书链原理讲起,到用 Let's Encrypt 实现自动续签。
Many developers use HTTPS without understanding certificates. This guide covers certificate chain fundamentals through Let's Encrypt auto-renewal setup.
前几天线上证书过期了,导致网站挂了半小时。事后复盘发现是因为没配自动续签,证书到期了也没人注意到。趁这个机会把证书相关的知识整理一下。 证书链是什么 浏览器信任一个网站的 HTTPS 证书,靠的是证书链: 根证书(Root CA):预装在操作系统和浏览器里,比如 DigiCert、ISRG Root X1 中间证书(Intermediate CA):根 CA 签发的,用于签发终端证书 终端证书(End Entity):你的域名证书 浏览器会沿着证书链往上验证,直到找到一个它信任的根证书。如果链断了(比如缺少中间证书),浏览器就会报不安全。 Let's Encrypt 免费证书 现在基本没有理由不用 HTTPS 了,因为 Let's Encrypt 提供免费的 DV 证书。用 certbot 获取证书: # 安装 certbot apt install certbot python3-certbot-nginx # 获取证书(Nginx 自动配置) certbot --nginx -d example.com -d www.example.com # 测试自动续签 certbot renew --dry-run 自动续签配置 Let's Encrypt 的证书有效期只有 90 天,必须配自动续签。certbot 安装时通常会自动配好 systemd timer 或 cron job: # 检查 systemd timer systemctl list-timers | grep certbot # 如果没有,手动加 cron 0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx" 常见坑 证书链不完整 有些服务器只配了终端证书没配中间证书,桌面浏览器可能正常(它会自己下载中间证书),但移动端和一些 API 客户端会报错。解决方案:用 fullchain.pem 而不是 cert.pem。 证书和密钥不匹配 续签后如果 Nginx 引用的还是旧证书文件,会出现证书和密钥不匹配的错误。确保 Nginx 配置引用的是 certbot 的符号链接路径(/etc/letsencrypt/live/),它会自动指向最新的证书。 HSTS 的坑 启用了 HSTS 之后如果再想切回 HTTP 就很麻烦了,因为浏览器会缓存 HSTS 策略。建议先用短的 max-age 测试,确认没问题再改大。
After a 30-minute outage from an expired certificate, here's a comprehensive guide on HTTPS certificates. Certificate Chain Root CA (pre-installed in browsers) → Intermediate CA → Your domain certificate. Missing intermediate certificates cause mobile/API client failures. Let's Encrypt Setup Free DV certificates via certbot with automatic Nginx configuration. 90-day validity requires auto-renewal via systemd timer or cron. Common Pitfalls Incomplete certificate chains (use fullchain.pem), certificate/key mismatch after renewal, and HSTS commitment (start with short max-age).