证书 47 天就过期?我在 EC2 上用 Certbot 搞定了自动续期

上周收到运维群里转发的消息:CA/Browser Forum 通过决议,SSL/TLS 证书有效期要逐步缩短到 47 天。一年续 8 次以上,手动?想都别想。

我正好有几台亚马逊云科技的 EC2 跑 Nginx,证书是 Let's Encrypt 的。趁这个机会把自动续期整明白了,顺便试了三种 Certbot 模式,踩了两个坑,记录一下。

有效期缩短时间表

时间 有效期
现在 398 天
2026.3 200 天
2027.3 100 天
2029.3 47 天

ACM 用户可以不看这篇

如果你用 ALB / CloudFront / API Gateway,直接用 ACM 申请证书,自动续期不需要操心。本文是给那些在 EC2 上自建 Nginx、用第三方证书的同学看的。

三种 Certbot 模式

Standalone:简单粗暴但有停机

bash 复制代码
sudo systemctl stop nginx
sudo certbot certonly --standalone -d yourdomain.com
sudo systemctl start nginx

每次续期 Nginx 要停 10-30 秒。开发环境凑合用,生产别碰。

Webroot:零停机但需要改配置

核心是给 ACME 验证路径开个口子:

nginx 复制代码
server {
    listen 80;
    server_name yourdomain.com;

    location /.well-known/acme-challenge/ {
        root /usr/share/nginx/html;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

踩坑:我第一次忘了加这个 location,80 端口全部 301 到 HTTPS,Let's Encrypt 验证请求也被重定向了,签发失败。排查半小时。

bash 复制代码
sudo certbot certonly --webroot -w /usr/share/nginx/html -d yourdomain.com

Nginx 插件:零停机 + 全自动(推荐)

bash 复制代码
sudo certbot --nginx -d yourdomain.com

一条命令搞定验证、下载、配置、reload。后续 certbot renew 自动搞定一切。

自动续期

bash 复制代码
# 检查定时任务是否存在
sudo systemctl list-timers | grep certbot

# 没有就手动加
echo "0 3,15 * * * root certbot renew --quiet" | sudo tee /etc/cron.d/certbot

DigiCert 也支持 ACME

商业证书同样能自动化:

bash 复制代码
sudo certbot register \
  --server "https://acme.digicert.com/v2/acme/directory/" \
  --eab-kid "YOUR_KID" --eab-hmac-key "YOUR_KEY" \
  --email admin@yourdomain.com --agree-tos

sudo certbot --nginx \
  --server "https://acme.digicert.com/v2/acme/directory/" \
  -d yourdomain.com

总结

模式 停机 复杂度 适合
Standalone 10-30s 测试
Webroot 需要掌控配置
Nginx 插件 大部分生产

能用 ACM 就用 ACM,自建就 Nginx 插件模式一把梭。别等 47 天有效期落地了才慌。


参考:亚马逊云科技官博 - 使用ACME协议实现证书自动续期

相关推荐
Avan_菜菜5 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
ping某9 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
難釋懷11 天前
Nginx反向代理中的容错机制
运维·nginx
bloglin9999911 天前
Nginx高危漏洞CVE-2021-23017及配置样例
运维·nginx
进阶的小名11 天前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
難釋懷11 天前
Nginx获取客户端真实IP
服务器·前端·nginx
qq_谁赞成_谁反对11 天前
甲方IT的成长之路--nginx实战--2604
服务器·数据库·nginx
图灵追慕者11 天前
Nginx安裝以及配置顯示本地服務器文件夾
运维·nginx
rabbit_pro11 天前
Nginx配置维护模式
运维·nginx
楠目12 天前
Nginx 解析漏洞利用总结
nginx·网络安全