HTTPS 和 SSL/TLS 管理
自动证书申请、续期和 TLS 配置。
边缘进阶
为什么
HTTPS 是现代 Web 必需 — 浏览器拒绝 HTTP,SEO/HTTP2 需要 TLS。Cloudflare 边缘网络覆盖 330+ 城市,全球 p95 延迟低于 50ms。
边缘函数相比源服务器将 TTFB 降低 60-80%,Akamai 报告 2024 年 40% 的企业使用边缘计算。
何时使用
所有生产部署 — HTTPS 不可妥协。 需要数据库写入的有状态逻辑避免边缘函数 — 边缘运行时无持久连接池。需要长时间运行任务(>30s)或重 CPU 计算时跳过。
如何做
# Let's Encrypt with certbot (for self-hosted)
sudo certbot certonly --standalone -d example.com
sudo certbot renew --dry-run
# Auto-续期 (cron)
0 12 * * * /usr/bin/certbot renew --quiet
# nginx TLS 配置
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
}
4. 验证部署 — curl -I https://your-app.example.com
5. 监控日志 — 前 5 分钟检查错误
6. 需要时回滚 — 回退到上一版本
示例
# Vercel — 自动 HTTPS, no 配置 needed
# For 自定义 certs (企业):
{
"ssl": {
"certificate": "@cert.pem",
"key": "@key.pem"
}
}
# 测试 SSL grade
npx ssllabs example.com
常见问题
- ⚠证书续期失败 — 检查 80 端口对 HTTP-01 challenge 开放
- ⚠混合内容 — 确保所有资源用 https:// URL
- ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致
最佳实践
- ✓仅用 TLS 1.2+ — 禁用旧协议
- ✓用 HSTS — 强制未来所有访问用 HTTPS
- ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求