如何利用Nginx和Certbot实现大规模网站证书自动化签发与定期轮转?

2026-04-29 02:022阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1168个文字,预计阅读时间需要5分钟。

如何利用Nginx和Certbot实现大规模网站证书自动化签发与定期轮转?

批量签发、自动续期、不修改Nginx配置、不间断服务——webroot+模式是当前管理几十上百个域名+HTTPS+证书最稳定的路径。 它不依赖certbot。

location ^~ /.well-known/acme-challenge/ 必须全局统一配置

这个 location 是整个 webroot 模式的命脉。它不能写在某个 server 块里,也不能每个域名单独配一套路径,否则批量申请时会反复失败。

  • 所有域名共用一个验证根目录,比如 /var/www/certbot,创建后确保 Nginx 进程可读:sudo chown nginx:nginx /var/www/certbot
  • 在全局配置(如 /etc/nginx/conf.d/common.confhttp 块内)加一段固定规则:

    location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; try_files $uri =404; }

  • 千万别用 alias——末尾斜杠处理不一致会导致 404;也别嵌套在 location / 里,优先级冲突会让验证文件压根不命中
  • 验证是否生效:手动放个文件 echo test > /var/www/certbot/.well-known/acme-challenge/test,然后 curl http://any-domain-you-have/.well-known/acme-challenge/test 应返回 test

certbot certonly --webroot 批量执行要按域名组拆开

certbot 不支持一次命令签发完全无关的域名(比如 a.comb.net),但可以对同一主域下的多个子域(site1.comwww.site1.comapi.site1.com)打成一张证书。所以批量不是“全塞进一个命令”,而是“分组循环执行”。

  • 把域名按归属分组,每行一组,写入 /opt/domains.txt

    site1.com www.site1.com api.site1.com site2.com docs.site2.com example.com

  • 用 while 循环调用,避免单点失败阻断全部:

    while read -r domains; do certbot certonly --webroot -w /var/www/certbot \ -d $domains \ --email admin@$(echo $domains | awk '{print $1}') \ --agree-tos --non-interactive --quiet done < /opt/domains.txt

  • 每组成功后,证书存进独立目录:/etc/letsencrypt/live/site1.com/,结构干净,后续 Nginx include 或脚本轮询都方便

自动续期不能只靠 certbot renew + nginx -s reload

直接在 crontab 里写 certbot renew && nginx -s reload 是危险操作:续期成功但 reload 失败(比如配置语法错误),Nginx 就会退出,HTTPS 服务瞬间中断。

  • 必须用 --deploy-hook 把 reload 封装进原子流程:

    certbot renew --quiet --no-self-upgrade \ --deploy-hook "nginx -t && systemctl reload nginx"

  • 检查 reload 是否真生效:hook 里加 nginx -t 验证语法,再 systemctl reload nginx,任何一步失败都不继续
  • 测试续期逻辑是否通:先跑 certbot renew --dry-run,看日志里有没有 Attempting to renew cert (site1.com)...Deploying certificate
  • 注意证书实际续期时间:Let’s Encrypt 默认只在到期前 30 天内触发续期,--dry-run 不受此限,但真实 cron 任务得等够天数才动作

Nginx HTTPS 配置必须模板化 + 独立引用证书路径

不要为每个域名复制粘贴一整段 server { listen 443 ssl; ... }。证书路径、SSL 参数、业务逻辑要解耦,否则新增域名或调整 cipher 套件就得改几十个文件。

  • /etc/nginx/conf.d/ssl/ 下为每个域名建单独 conf:site1.com.conf,内容只含关键段:

    server { listen 443 ssl http2; server_name site1.com www.site1.com; ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem; include /etc/nginx/conf.d/ssl/params.conf; # 公共 SSL 参数 location / { proxy_pass http://backend-site1; } }

  • 公共参数(TLS 版本、cipher、session 缓存)抽到 /etc/nginx/conf.d/ssl/params.conf,统一维护
  • HTTP server 块里仍保留 301 跳转,但不用再重复写 location ^~ /.well-known/acme-challenge/ ——它已在全局配置里,复用即可

最常被忽略的是权限和 SELinux:CentOS/RHEL 上若启用了 SELinux,/var/www/certbot 目录可能需要额外打标 chcon -R -t httpd_sys_content_t /var/www/certbot,否则 Nginx 读不到验证文件;另外,certbot 续期后生成的新私钥默认权限是 600,但 Nginx worker 进程若非 root 启动,可能无权读取——建议用 --deploy-hook 顺手 fix:chmod 644 /etc/letsencrypt/live/*/privkey.pem

标签:Nginx

本文共计1168个文字,预计阅读时间需要5分钟。

如何利用Nginx和Certbot实现大规模网站证书自动化签发与定期轮转?

批量签发、自动续期、不修改Nginx配置、不间断服务——webroot+模式是当前管理几十上百个域名+HTTPS+证书最稳定的路径。 它不依赖certbot。

location ^~ /.well-known/acme-challenge/ 必须全局统一配置

这个 location 是整个 webroot 模式的命脉。它不能写在某个 server 块里,也不能每个域名单独配一套路径,否则批量申请时会反复失败。

  • 所有域名共用一个验证根目录,比如 /var/www/certbot,创建后确保 Nginx 进程可读:sudo chown nginx:nginx /var/www/certbot
  • 在全局配置(如 /etc/nginx/conf.d/common.confhttp 块内)加一段固定规则:

    location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; try_files $uri =404; }

  • 千万别用 alias——末尾斜杠处理不一致会导致 404;也别嵌套在 location / 里,优先级冲突会让验证文件压根不命中
  • 验证是否生效:手动放个文件 echo test > /var/www/certbot/.well-known/acme-challenge/test,然后 curl http://any-domain-you-have/.well-known/acme-challenge/test 应返回 test

certbot certonly --webroot 批量执行要按域名组拆开

certbot 不支持一次命令签发完全无关的域名(比如 a.comb.net),但可以对同一主域下的多个子域(site1.comwww.site1.comapi.site1.com)打成一张证书。所以批量不是“全塞进一个命令”,而是“分组循环执行”。

  • 把域名按归属分组,每行一组,写入 /opt/domains.txt

    site1.com www.site1.com api.site1.com site2.com docs.site2.com example.com

  • 用 while 循环调用,避免单点失败阻断全部:

    while read -r domains; do certbot certonly --webroot -w /var/www/certbot \ -d $domains \ --email admin@$(echo $domains | awk '{print $1}') \ --agree-tos --non-interactive --quiet done < /opt/domains.txt

  • 每组成功后,证书存进独立目录:/etc/letsencrypt/live/site1.com/,结构干净,后续 Nginx include 或脚本轮询都方便

自动续期不能只靠 certbot renew + nginx -s reload

直接在 crontab 里写 certbot renew && nginx -s reload 是危险操作:续期成功但 reload 失败(比如配置语法错误),Nginx 就会退出,HTTPS 服务瞬间中断。

  • 必须用 --deploy-hook 把 reload 封装进原子流程:

    certbot renew --quiet --no-self-upgrade \ --deploy-hook "nginx -t && systemctl reload nginx"

  • 检查 reload 是否真生效:hook 里加 nginx -t 验证语法,再 systemctl reload nginx,任何一步失败都不继续
  • 测试续期逻辑是否通:先跑 certbot renew --dry-run,看日志里有没有 Attempting to renew cert (site1.com)...Deploying certificate
  • 注意证书实际续期时间:Let’s Encrypt 默认只在到期前 30 天内触发续期,--dry-run 不受此限,但真实 cron 任务得等够天数才动作

Nginx HTTPS 配置必须模板化 + 独立引用证书路径

不要为每个域名复制粘贴一整段 server { listen 443 ssl; ... }。证书路径、SSL 参数、业务逻辑要解耦,否则新增域名或调整 cipher 套件就得改几十个文件。

  • /etc/nginx/conf.d/ssl/ 下为每个域名建单独 conf:site1.com.conf,内容只含关键段:

    server { listen 443 ssl http2; server_name site1.com www.site1.com; ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem; include /etc/nginx/conf.d/ssl/params.conf; # 公共 SSL 参数 location / { proxy_pass http://backend-site1; } }

  • 公共参数(TLS 版本、cipher、session 缓存)抽到 /etc/nginx/conf.d/ssl/params.conf,统一维护
  • HTTP server 块里仍保留 301 跳转,但不用再重复写 location ^~ /.well-known/acme-challenge/ ——它已在全局配置里,复用即可

最常被忽略的是权限和 SELinux:CentOS/RHEL 上若启用了 SELinux,/var/www/certbot 目录可能需要额外打标 chcon -R -t httpd_sys_content_t /var/www/certbot,否则 Nginx 读不到验证文件;另外,certbot 续期后生成的新私钥默认权限是 600,但 Nginx worker 进程若非 root 启动,可能无权读取——建议用 --deploy-hook 顺手 fix:chmod 644 /etc/letsencrypt/live/*/privkey.pem

标签:Nginx