跳到主要内容
证书选购指南

子域名证书单独申请怎么做?从 CSR 到 Nginx 配置、报错排查与生效验证的完整步骤

子域名证书单独申请 — 子域名证书单独申请怎么做?从 CSR 到 Nginx 配置、报错排查与生效验证的完整步骤
核心结论

子域名证书单独申请就是为 sub.example.com 单独签发一张仅含该 DNS 名(可加 www)的证书,推荐用 certbot 2.11 执行 certbot certonly --webroot -w /var/www/html -d sub.example.com --cert-name sub.example.com,Let's Encrypt 证书有效期 90 天并在到期前 30 天自动续期。 若 CA 要求 CSR,可用 OpenSSL 3.0.13 执行 openssl req -new -newkey rsa:2048 -sha256 -nodes -keyout sub.example.com.key -out sub.example.com.csr -subj '/CN=sub.example.com' -addext 'subjectAltName=DNS:sub.example.com,DNS:www.sub.example.com'。 Nginx 1.24/1.26 部署时把 ssl_certificate 指向 /etc/letsencrypt/live/sub.example.com/fullchain.pem,ssl_certificate_key 指向 privkey.pem,并启用 ssl_protocols TLSv1.2 TLSv1.3。 据 Let's Encrypt Rate Limits 文档,同一注册域名每周最多 50 张证书、同一 exact set 每周最多 5 张重复证书;测试请用 --server https://acme-staging-v02.api.letsencrypt.org/directory。 验证用 openssl s_client -connect sub.example.com:443 -servername sub.example.com -showcerts /dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName,并执行 certbot renew --dry-run。

一、为什么子域名要单独申请证书?问题背景与根本原因

问:主域已经有一张 example.com 的证书,甚至带了 *.example.com 通配符,为什么 blog.example.com 还要再签一张?先明确概念:子域名证书单独申请,指的是证书的 SAN(Subject Alternative Name)里只放 sub.example.com(必要时追加 www.sub.example.com 等少量别名),它在 Let’s Encrypt 体系里对应独立的 cert-name、独立的私钥文件和独立的续期任务,与主域 example.com 的 lineage 完全隔离。典型触发场景是博客、开放 API、运维管理后台被拆到不同主机、不同云账号,甚至不同外包团队维护。

根本原因一:通配符只匹配一级标签。据 RFC 6125 §6.4.3(2024 年 3 月起由 RFC 9525 取代)与 CA/B Forum Baseline Requirements §3.2.2.4,证书匹配只按 SAN 中的精确名或”最左单层通配符”执行,*.example.com 不覆盖 a.b.example.com。先查清现有证书到底覆盖了什么,再决定要不要签:

# 查看现有证书的 SAN 列表(判断子域是否已被覆盖)
openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -text 
  | grep -A1 "Subject Alternative Name"
# 典型输出:DNS:example.com, DNS:www.example.com, DNS:*.example.com
# 注意:该输出不包含 DNS:a.b.example.com,二级子域必然不匹配

匹配失败时的真实报错长这样,可据此快速定位是证书问题而非 DNS 问题:

$ curl -vI https://a.b.example.com
curl: (60) SSL: no alternative certificate subject name matches target host name 'a.b.example.com'
维度 主域证书 example.com + *.example.com 子域单独证书 sub.example.com
覆盖范围 example.com 及其一级子域 仅 SAN 列出的精确名
私钥持有方 常放在 CDN / WAF / 负载均衡,多方可见 只落在该子域对应主机
续期配置 /etc/letsencrypt/renewal/example.com.conf /etc/letsencrypt/renewal/sub.example.com.conf
吊销影响 全站一级子域同时失效 只影响该子域
DV 价格区间(2024 国内代理商) 0–300 元/年(Let’s Encrypt 为 0) 0–300 元/年;OV 约 800–3000 元/年

根本原因二:爆炸半径。主域私钥一旦交给 CDN、WAF 或外包团队,泄露面就是整站;单独证书可把 sub.example.com 的私钥限定在对应主机,出事时只吊销这一张,用序列号留证并走 OCSP/CRL:

# 记录序列号,便于吊销与审计追溯
openssl x509 -in /etc/letsencrypt/live/sub.example.com/cert.pem -noout -serial
# serial=03A1B2C3...
sudo certbot revoke --cert-path /etc/letsencrypt/live/sub.example.com/cert.pem 
  --reason keycompromise

根本原因三:部署与续期解耦。主域证书往往由负载均衡统一续期,子域却由 Kubernetes Ingress 或独立 Nginx 管理。据 Let’s Encrypt 文档,--cert-name 决定 lineage 目录名;不显式指定时 certbot 取第一个 -d 作为名字,后续误用 --expand 会把子域合并进主证书,导致”改一个域名重载全站”:

# 单独签发,生成独立的 renewal 配置
sudo certbot certonly --webroot -w /var/www/sub.example.com 
  -d sub.example.com -d www.sub.example.com 
  --cert-name sub.example.com 
  --deploy-hook "systemctl reload nginx"
# 结果:/etc/letsencrypt/renewal/sub.example.com.conf
# certbot 默认在到期前 30 天续期(renew_before_expiry),LE 证书有效期 90 天

根本原因四:资产边界。按系统边界拆证书,才能做资产清单、负责人和到期告警。最常见的误区是以为签了 sub.example.com 就自动带上 www.sub.example.com——不会。必须显式追加 -d www.sub.example.com,或在 CSR 的 SAN 中写入 DNS:www.sub.example.com,否则浏览器报 NET::ERR_CERT_COMMON_NAME_INVALID。这也是子域名证书单独申请时最容易漏的一步。

二、申请前准备:DNS、CAA、端口与账户权限

在提交 CSR 之前,先把“签发链路”上的五个前置条件验证一遍,能省掉 80% 的失败重试。本节命令默认在 Ubuntu 22.04 / Debian 12 上以 sudo 执行,适用于 certbot 2.10+、acme.sh 3.0.7+、OpenSSL 3.0+。

1)DNS 是否已指向正确目标?HTTP-01 与 DNS-01 的验证路径完全不同,先看清楚子域名解析到哪:

# A / AAAA 记录必须直接返回公网 IP,不能是 CNAME 到内网或返回空
dig +short A    sub.example.com
dig +short AAAA sub.example.com

# 若用 DNS-01,确认该 TXT 名称当前是否已有记录(避免冲突)
dig +short TXT _acme-challenge.sub.example.com

HTTP-01 要求 80/tcp 从公网可达且 /.well-known/acme-challenge/ 不被重定向;DNS-01 则要求你对 _acme-challenge.sub.example.com 具备写入权限(自建 BIND 需 allow-update,云 DNS 需对应 API Key)。据 Let’s Encrypt 文档,DNS-01 是唯一能签发通配符证书的挑战类型,这也是很多子域名场景改用 DNS-01 的原因。

2)CAA 记录是否放行?CAA 沿父域继承,子域没记录就查父域,父域有 0 issue "letsencrypt.org" 才允许签发;存在但没列 Let’s Encrypt,就会返回 CAA record prevents issuance。

dig CAA example.com +short
dig CAA sub.example.com +short
# 期望形如:0 issue "letsencrypt.org"

3)工具版本与 ACME 账户。OpenSSL 需 1.1.1+ 才支持 -addext(3.0.13+ 更稳妥),否则 CSR 的 SAN 要手写配置文件:

certbot --version        # 建议 ≥ 2.10
acme.sh --version        # 建议 ≥ 3.0.7
openssl version          # 建议 ≥ 3.0.13

# 首次使用需注册账户(已有账户会提示已存在,可忽略)
certbot register --email ops@example.com --agree-tos --no-eff-email

4)端口与 Nginx 默认 server。放行 80/tcp(HTTP-01)和 443/tcp(HTTPS),云安全组与主机防火墙两层都要查:

sudo ufw status verbose            # 或:sudo iptables -L INPUT -n --line-numbers
sudo ss -lntp | grep -E ':(80|443)s'

常见问题:默认 server 把 /.well-known/acme-challenge 也 301 到 HTTPS,导致验证拿不到文件。应在其前插入放行规则:

location ^~ /.well-known/acme-challenge/ {
    root /var/www/html;          # 与 certbot --webroot -w 保持一致
    default_type text/plain;
    try_files $uri =404;
}

5)权限与回滚备份。/etc/letsencrypt/live 默认 0700 root:root,Nginx master 以 root 启动并加载私钥即可,切勿 chmod 777。用 namei -l 逐级检查:

namei -l /etc/letsencrypt/live/sub.example.com/privkey.pem
# 变更前留档,便于覆盖主域配置后回滚
openssl x509 -in /etc/nginx/ssl/example.com.pem -noout -serial -dates
sudo nginx -T > /root/nginx-backup-$(date +%F).conf
挑战类型 必需条件 通配符支持 典型失败报错
HTTP-01 80/tcp 公网可达,无 301 拦截 否 Timeout during connect
DNS-01 可写 _acme-challenge TXT 是 Incorrect TXT record

三、完整实操步骤(分步骤编号)

下面按真实执行顺序给出六个步骤。命令基于 OpenSSL 3.x(Ubuntu 22.04 默认 3.0.2、Debian 12 默认 3.0.11)与 certbot 2.x,路径按单域名独立证书设计,与主域名证书完全隔离。

步骤 1:生成私钥与 CSR(仅 CA 需要 CSR 时)

Let’s Encrypt 走 ACME 协议时不需要手工 CSR,但商业 CA(DigiCert、Sectigo、ZeroSSL 企业版)仍要求提交。一次命令同时生成私钥和带 SAN 的 CSR:

# -nodes 表示私钥不加密(便于 Nginx 自动加载,需靠文件权限保护)
# -addext 是 OpenSSL 1.1.1+ 才支持的参数,低版本必须写 openssl.cnf
openssl req -new -newkey rsa:2048 -sha256 -nodes 
  -keyout /etc/ssl/private/sub.example.com.key 
  -out /etc/ssl/certs/sub.example.com.csr 
  -subj '/CN=sub.example.com/O=Example Ltd/C=CN' 
  -addext 'subjectAltName=DNS:sub.example.com,DNS:www.sub.example.com'

chmod 600 /etc/ssl/private/sub.example.com.key   # 私钥必须 600,否则 Nginx 报权限错误

# 检查 SAN 是否真的写进 CSR(CA 只看 SAN,CN 已废弃)
openssl req -in /etc/ssl/certs/sub.example.com.csr -noout -text 
  | grep -A1 'Subject Alternative Name'

预期输出为 DNS:sub.example.com, DNS:www.sub.example.com。据 CA/Browser Forum Baseline Requirements(v2.x,2023 起生效),所有公开证书必须包含 SAN 扩展,且 CN 不再作为身份校验依据——这就是只填 CN 会被 CA 直接拒签的原因。

步骤 2:用 Let’s Encrypt webroot 方式自动签发(推荐)

webroot 不重启 Web 服务,最适合已有 80 端口站点的场景。--cert-name 是关键参数,它决定 /etc/letsencrypt/live/ 下的目录名,后续续期、部署都靠它定位:

certbot certonly --webroot -w /var/www/html 
  -d sub.example.com -d www.sub.example.com 
  --cert-name sub.example.com 
  --email ops@example.com --agree-tos --no-eff-email 
  --keep-until-expiring

成功时 certbot 2.x 输出:Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/sub.example.com/fullchain.pem,有效期 90 天(Let’s Encrypt 官方固定策略,不可延长)。--keep-until-expiring 保证重复执行不会无谓消耗每周 5 次「重复证书」速率限制。

步骤 3:80 端口不可用时改用 DNS-01 验证

内网子域、80 被防火墙封锁、或需要通配符(*.sub.example.com)时,只能走 DNS-01。acme.sh 配合 DNS 服务商 API 可全自动:

# --keylength ec-256 生成 ECDSA P-256 证书,体积更小、握手更快
acme.sh --issue --dns dns_ali -d sub.example.com 
  --server letsencrypt --keylength ec-256

# 无 API 时用 certbot 手动模式,按其提示添加 TXT 记录
certbot certonly --manual --preferred-challenges dns -d sub.example.com

手动模式下先加记录、再验证,TTL 建议 60–600 秒,并确认已生效:

dig TXT _acme-challenge.sub.example.com +short

必须看到返回的 43 字符 base64 摘要后才回车,否则报 Incorrect TXT record found at _acme-challenge.sub.example.com(Let’s Encrypt 从多个视角查询,DNS 传播不全即失败)。注意:手动 DNS 模式无法自动续期,90 天后必须重来。

步骤 4:部署到 Nginx(直接引用 live 目录,勿复制)

server {
    listen 443 ssl;
    http2 on;                                   # Nginx 1.25.1+ 写法,旧版用 listen 443 ssl http2;
    server_name sub.example.com www.sub.example.com;

    # 直接指向 live 目录的符号链接,续期后自动生效
    ssl_certificate     /etc/letsencrypt/live/sub.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sub.example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;        # 遵循 Mozilla SSL Configuration Generator 中级配置
    ssl_session_cache   shared:SSL:10m;
}
nginx -t && systemctl reload nginx   # 先测试再 reload,避免配置错误导致全站 502

不要 cp fullchain.pem 到别处:live 目录是 renewal 后自动更新的软链,复制副本会在 90 天后静默过期。若必须复制,需在 --deploy-hook 里重做拷贝。

步骤 5:配置并验证自动续期

certbot renew --dry-run          # 模拟续期,不消耗速率限制,必做
systemctl list-timers | grep certbot

certbot 2.x 由 certbot.timer 驱动,默认每天运行两次并附加随机延迟(官方设计用于避免整点冲击 ACME 服务器);实际续期发生在到期前 30 天内。acme.sh 用户需显式安装证书钩子:

acme.sh --install-cert -d sub.example.com 
  --key-file       /etc/nginx/ssl/sub.example.com.key 
  --fullchain-file /etc/nginx/ssl/sub.example.com.pem 
  --reloadcmd      'systemctl reload nginx'   # 续期后自动重载,缺此参数则新证书不生效

步骤 6:把证书指纹与到期日登记进资产台账

openssl x509 -in /etc/letsencrypt/live/sub.example.com/cert.pem -noout 
  -serial -fingerprint -sha256 -dates -ext subjectAltName

输出含 serial、SHA256 Fingerprint、notBefore/notAfter、X509v3 Subject Alternative Name。将序列号、到期日与 --cert-name 写入 CMDB,并设置到期前 21 天告警——比 Let’s Encrypt 的 30 天续期窗口留出 9 天人工兜底时间,可覆盖续期失败、DNS 变更、速率限制等常见故障。

四、配置/命令示例(含具体参数与版本)

OpenSSL 3.0.13 怎么一次性生成带 SAN 的 CSR 并当场验证?单域名证书早已不够用——现代 CA 与浏览器按 RFC 6125 只认 SAN 扩展,CN 仅作展示。OpenSSL 从 1.1.1 起提供 -addext,3.0.13(Ubuntu 24.04 LTS 默认版本)可直接在一条命令里写入 SAN,不必再手写 openssl.cnf:

# -nodes 不加密私钥(便于 Nginx 无密码启动);-sha256 为 CA/B Forum 强制摘要算法
openssl req -new -newkey rsa:2048 -sha256 -nodes 
  -keyout sub.example.com.key -out sub.example.com.csr 
  -subj '/CN=sub.example.com' 
  -addext 'subjectAltName=DNS:sub.example.com,DNS:api.sub.example.com'

# 验证:只抓 SAN 一行,应输出 DNS:sub.example.com, DNS:api.sub.example.com
openssl req -in sub.example.com.csr -noout -text | sed -n '/Subject Alternative Name/,+1p'

若输出为空或只有 DNS:sub.example.com,说明 -addext 未生效(OpenSSL < 1.1.1)或参数被 -config 覆盖,此时 CA 签出的证书会直接缺少 api 子域,后续访问报 NET::ERR_CERT_COMMON_NAME_INVALID。

用 certbot 单独签发时,怎么避免目录变成 sub.example.com-0001?关键在 --cert-name:据 Let’s Encrypt 官方文档,该参数决定 /etc/letsencrypt/live/ 与 renewal/ 下的 lineage 名称,缺省时 certbot 取首个 -d 域名并在冲突时追加 -0001,导致续期钩子与 Nginx 路径全部错位。certbot 2.11 命令如下:

certbot certonly --webroot -w /var/www/html 
  -d sub.example.com 
  --cert-name sub.example.com       # 固定 lineage 目录名,续期配置写入 renewal/sub.example.com.conf
  --key-type rsa --rsa-key-size 2048   # certbot ≥2.0 默认 key-type 为 ecdsa,不显式指定则 --rsa-key-size 被忽略
  --preferred-challenges http 
  --deploy-hook 'systemctl reload nginx'  # 仅在成功续期后触发,优于 --post-hook
参数 默认值 取值范围 / 说明
--key-type ecdsa(certbot ≥ 2.0) rsa / ecdsa,1.32 及更早默认 rsa
--rsa-key-size 2048 2048 / 3072 / 4096,仅 key-type=rsa 时生效
--preferred-challenges http http(80 端口)/ dns(通配符必需)
--cert-name 首个 -d 域名 决定 live/ 与 renewal/ 下的目录名

Nginx 1.24 / 1.26 的 443 片段怎么写?注意 1.25.1 起官方弃用 listen ... http2 写法,改用独立的 http2 on;,旧写法在 1.26 仍可用但会打印 deprecated 警告:

server {
    listen 443 ssl;                 # 1.25.1+ 推荐;旧版写 listen 443 ssl http2;
    http2 on;                       # 仅 Nginx ≥ 1.25.1 支持,1.24 请删除此行
    server_name sub.example.com;
    ssl_certificate     /etc/letsencrypt/live/sub.example.com/fullchain.pem;  # 必须是 fullchain,只给 cert.pem 会让 Android/Java 报链不完整
    ssl_certificate_key /etc/letsencrypt/live/sub.example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;   # 默认 TLSv1 TLSv1.1 TLSv1.2(1.23.4 之前)
    ssl_session_cache   shared:SSL:10m;    # 默认 none,10m ≈ 4 万个会话
    ssl_session_timeout 1d;                # 默认 5m
    add_header Strict-Transport-Security "max-age=31536000" always;  # 首次上线建议先用 max-age=300 观察
}

Apache 2.4.58 呢?在 /etc/apache2/sites-available/sub.example.com-ssl.conf 中:

<VirtualHost *:443>
    ServerName sub.example.com
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/sub.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/sub.example.com/privkey.pem
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1   # 显式禁用已废弃协议
</VirtualHost>
# 校验后重载:apachectl configtest && systemctl reload apache2

私钥权限与路径怎么确认?手动签发的证书常因某一级目录 755 之外的可读位泄漏私钥:

install -d -m 700 /etc/ssl/private
install -m 600 -o root -g root sub.example.com.key /etc/ssl/private/sub.example.com.key
namei -l /etc/letsencrypt/live/sub.example.com/privkey.pem   # 逐级打印权限与属主

典型输出中 letsencrypt 目录为 drwx------,privkey.pem 是符号链接指向 archive/;只要 Nginx master(以 root 启动)能读到即可,worker 进程无需权限。

私钥泄漏后如何吊销并重新签发?先取序列号留档,再按 RFC 5280 的 reason code 吊销,最后清理 lineage 重签,否则 certbot 会因同名证书存在而复用旧配置:

openssl x509 -in /etc/letsencrypt/live/sub.example.com/cert.pem -noout -serial
certbot revoke --cert-path /etc/letsencrypt/live/sub.example.com/cert.pem --reason keycompromise
certbot delete --cert-name sub.example.com        # 清理 live/archive/renewal
# 再执行前面的 certonly 命令完成子域名证书单独申请

注意 --reason 还支持 superseded、cessationOfOperation;keycompromise 会让 Let’s Encrypt 把该密钥记入吊销名单,后续若用同一私钥重签会被直接拒绝。

五、参数对比与方案选型表格

先回答最常被问到的那句:子域名证书单独申请到底值不值?答案取决于三件事——这个子域是否由独立团队/独立主机运维、是否需要单独吊销、以及未来会不会出现二级子域(如 a.b.example.com)。下面这张表是选型的核心依据,价格区间为 2024 年国内主流渠道的常见报价,会随 CA 与代理商促销波动。

方案 覆盖范围 典型命令/参数 私钥隔离 续期 参考价格(2024 年常见区间)
单域名 DV 证书(Let’s Encrypt) 仅 sub.example.com,可把 www.sub.example.com 加入同一 SAN certbot certonly --webroot -w /var/www/html -d sub.example.com --cert-name sub.example.com 独立 lineage 与私钥(/etc/letsencrypt/live/sub.example.com/) 90 天有效,剩余 30 天自动续期 0 元
商业 DV 单域名证书 仅一个 FQDN,部分 CA 可加免费 www CSR 的 SAN 只写 sub.example.com,在 CA 控制台下单 独立 常见 1 年,需手动或 API 续期 约 0–300 元/年
通配符证书 *.example.com 只覆盖一级子域,不覆盖 a.b.example.com certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com 私钥需分发到多个子域,隔离较弱 90 天或 1 年 Let’s Encrypt 0 元,商业约 1000–5000 元/年
SAN 多域名证书 可把 sub.example.com、api.example.com、example.com 放同一张证书 -d sub.example.com -d api.example.com --cert-name shared-san 多系统共享私钥 统一续期,单点故障影响多域名 商业约 500–3000 元/年

差异的根源在 CSR 的 SAN 字段,而不在“证书类型”这个营销名词上。据 RFC 5280 与 CA/Browser Forum 基线要求,现代证书必须在 SAN 扩展中列出全部域名,CN 仅作兼容保留;因此你多写一个 www,证书就多覆盖一个名字,与价格无关。

# OpenSSL 1.1.1 及以上支持 -addext;更低版本需在 .cnf 中写 req_extensions
openssl req -new -newkey rsa:2048 -nodes 
  -keyout sub.example.com.key 
  -out sub.example.com.csr 
  -subj "/CN=sub.example.com" 
  -addext "subjectAltName=DNS:sub.example.com,DNS:www.sub.example.com"

# 下单前自检 SAN 是否写全,避免签发后才发现漏域名
openssl req -in sub.example.com.csr -noout -text | grep -A1 "Subject Alternative Name"
# 输出应形如:DNS:sub.example.com, DNS:www.sub.example.com

如果 SAN 漏了 www,浏览器会直接报 NET::ERR_CERT_COMMON_NAME_INVALID,而 curl 侧对应 SSL: no alternative certificate subject name matches target host name——这是“证书明明签下来了却报错”的第一大原因。

选型建议:

  • 子域由独立团队或独立主机管理、需要单独吊销(例如外包系统下线)、或存在二级子域 → 选单独证书,这也正是子域名证书单独申请最典型的场景。
  • 5 个以内子域、同团队统一运维 → 可用 SAN 多域名证书,续期一次搞定,但任一域名私钥泄露就要整体重签。
  • 大量一级子域(几十上百)且能严格管控私钥 → 通配符;注意 Let’s Encrypt 通配符强制 DNS-01,无法用 --webroot,且必须手动或借助 DNS 插件自动化。

落地前做一次隔离性验证:ls -l /etc/letsencrypt/live/sub.example.com/privkey.pem 与 /etc/letsencrypt/renewal/sub.example.com.conf 应各自独立存在;若两条命令指向同一目录,说明 --cert-name 复用了旧 lineage,私钥隔离实际并未达成。

六、常见错误与排查(列出真实报错信息)

这一节把六类高频报错按「报错原文 → 定位命令 → 根因」整理,命令可直接复制。先看速查表:

报错关键词 最可能根因 定位命令
acme:error:unauthorized + key authorization file ... did not match webroot 与 Nginx root 不一致,或 301 把校验路径跳走 curl -I http://sub.example.com/.well-known/acme-challenge/test
acme:error:dns + NXDOMAIN looking up A 公网无 A/AAAA 记录、CNAME 链中断、split-horizon DNS dig +trace sub.example.com
CAA record for ... prevents issuance 子域自写的 CAA 未授权 Let’s Encrypt dig CAA sub.example.com +short
X509_check_private_key:key values mismatch 证书与私钥不是同一对 openssl x509 -noout -modulus ...
too many certificates already issued 命中周配额(同一注册域名 50 张 / 同一 exact set 5 张) 看报错里的 exact set 域名列表
unrecognized arguments: --preferred-chain certbot < 1.12 certbot --version

Q:HTTP-01 报 key authorization file did not match,是 DNS 没生效吗?

多数不是 DNS,而是 ACME 拿到的文件和 certbot 写的文件内容不同——webroot 指错了目录。先造探针自测:

# 1) 确认 Nginx 里该 server 块的 root,必须与 certbot -w 完全一致
nginx -T | grep -B2 -A6 'server_name sub.example.com'
# 2) 写探针(路径与 -w 参数同源)
echo ok > /var/www/sub/.well-known/acme-challenge/test
chmod -R 755 /var/www/sub/.well-known
# 3) 公网侧验证:必须 200 且 body 为 ok
curl -I  http://sub.example.com/.well-known/acme-challenge/test
curl -s  http://sub.example.com/.well-known/acme-challenge/test

返回 301/302 说明跳转把校验吃掉了。据 Let’s Encrypt 文档,HTTP-01 仅接受 301 跳向 HTTP 或 HTTPS,其它跳转一律判失败;用 ^~ 前缀匹配压住全站 rewrite:

location ^~ /.well-known/acme-challenge/ {
    root /var/www/sub;          # 与 certbot -w 保持一致
    default_type text/plain;
    try_files $uri =404;
}

签发命令:certbot certonly --webroot -w /var/www/sub -d sub.example.com(--webroot 无默认值,必须显式给 -w)。

Q:报 DNS problem: NXDOMAIN 怎么定位?

dig +trace sub.example.com A        # 看权威层是否应答
dig @8.8.8.8 sub.example.com A +short   # 校验器视角(公网递归)
dig @1.1.1.1 sub.example.com CNAME +short

两条结果不一致,基本是 split-horizon(内网权威只对内应答)或 CNAME 链末端指向不存在的名字;ACME 校验器只走公网递归,内网能解析不算数。

Q:CAA 记录为什么会阻止签发?

dig CAA sub.example.com +short   # 最近的一级,命中即停止继承
dig CAA example.com +short       # 父域,仅当子域无 CAA 时才生效

按 RFC 8659,CAA 沿域名树自下而上查找,某一级存在记录即终止继承。因此父域写 0 issue "letsencrypt.org" 并不能覆盖子域自写的 0 issue "otherca.com"——需改子域 CAA 或换 CA;无 CAA 记录时默认不限制任何 CA。

Q:Nginx 起不来,报 key values mismatch?

# 原始报错
# SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/sub.key") failed
# (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)
openssl x509 -noout -modulus -in fullchain.pem | openssl sha256
openssl rsa  -noout -modulus -in privkey.pem  | openssl sha256
# 两行哈希必须完全一致

不一致即部署错文件:常见于把 cert.pem(叶子证书)当 fullchain.pem 用,或复用旧 CSR 时残留上一张私钥。ECDSA 私钥不能用 openssl rsa,改 openssl pkey -noout -modulus。

Q:too many certificates already issued 是封号了吗?

不是,是命中速率限制。据 Let’s Encrypt Rate Limits 文档,同一注册域名(含所有子域)每 7 天 50 张,同一 exact set of domains 每 7 天 5 张重复证书。调试期改打 staging(配额宽松且不计入生产):

certbot certonly --webroot -w /var/www/sub -d sub.example.com 
  --server https://acme-staging-v02.api.letsencrypt.org/directory 
  --dry-run    # 试运行不落盘;正式签发时去掉 --dry-run

Q:certbot: error: unrecognized arguments: –preferred-chain

该参数自 certbot 1.12 引入,低于此版本直接报错,升级到 2.11 后可用;默认值空,即跟随 CA 返回的默认链。acme.sh 3.0.7+ 的等价写法:

certbot --preferred-chain "ISRG Root X1"   # certbot >= 1.12
acme.sh --issue -d sub.example.com --preferred-chain 'ISRG Root X1'  # acme.sh >= 3.0.7

七、效果验证方法(如何确认配置生效)

问:Nginx nginx -s reload 返回 0,是不是就说明子域名证书单独申请已经生效了?

不是。reload 只证明配置语法可解析,客户端实际拿到什么证书、走什么协议,必须从外部再验一遍。按”证书链 → 信任链 → HTTP 层 → 协议套件 → OCSP/续期 → 监控”六层依次查,任何一层失败都能定位到具体环节。

第一层:线上实际下发的是哪张证书、覆盖哪些名字(据 OpenSSL 文档,多域名共用 IP 时必须带 -servername 触发 SNI,否则可能拿到默认站点的证书):

# 抓取服务端真实返回的证书,-showcerts 会打印完整链
openssl s_client -connect sub.example.com:443 -servername sub.example.com -showcerts </dev/null 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
# 期望输出:
# subject=CN = sub.example.com
# issuer=C = US, O = Let's Encrypt, CN = R11
# notAfter=Mar 12 08:00:00 2026 GMT        ← 距今应 ≤ 90 天
# X509v3 Subject Alternative Name:
#     DNS:sub.example.com

若 subject 显示的是别的域名,说明 server_name 没匹配上、落到了 default_server;若 SAN 里没有 sub.example.com,说明子域名证书单独申请时 CSR 的 SAN 字段就写错了。

第二层:信任链是否闭合

# 用系统 CA 库校验叶子证书,-untrusted 提供中间证书
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt 
  -untrusted /etc/letsencrypt/live/sub.example.com/chain.pem 
  /etc/letsencrypt/live/sub.example.com/cert.pem
# 通过:/etc/letsencrypt/live/sub.example.com/cert.pem: OK
# 失败:error 20 at 0 depth lookup: unable to get local issuer certificate

报 missing chain 类错误时,检查 Nginx 是否误配了 ssl_certificate .../cert.pem——应改为 fullchain.pem(叶子 + 中间证书),否则 Android 与部分 Java 客户端会直接握手失败(RFC 5280 §6 要求提供到可信锚点的完整路径)。

第三层与第四层:HTTP 状态与协议套件

# --resolve 绕过 DNS,直连源站 IP,避免被 CDN 掩盖问题
curl -sSI https://sub.example.com --resolve sub.example.com:443:203.0.113.10 
  | grep -Ei 'HTTP/|strict-transport'
# HTTP/2 200
# strict-transport-security: max-age=31536000

# 看协商结果
curl -sv https://sub.example.com --resolve sub.example.com:443:203.0.113.10 -o /dev/null 2>&1 | grep 'SSL connection'
# SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384

# 枚举协议与套件;强制旧协议应失败
nmap --script ssl-enum-ciphers -p 443 sub.example.com
openssl s_client -connect sub.example.com:443 -servername sub.example.com -tls1_1 </dev/null
# 40A7...:error:0A000102:SSL routines::unsupported protocol

Nginx 1.24 中 ssl_protocols 默认值为 TLSv1 TLSv1.1 TLSv1.2,不含 TLSv1.3,必须显式写成 ssl_protocols TLSv1.2 TLSv1.3;(Mozilla Server Side TLS 推荐的 intermediate 配置即为此组合),否则上面的 curl 只会显示 TLSv1.2。

层次 关键命令 通过标准 失败最常见原因
证书链/SAN openssl s_client -showcerts CN 与 SAN 均为 sub.example.com SNI 未匹配、CSR 的 SAN 写错
信任链 openssl verify -untrusted chain.pem 返回 OK 用了 cert.pem 而非 fullchain.pem
HTTP 层 curl -I --resolve HTTP/2 200 + HSTS 头 server_name 落默认站、后端 502
协议套件 nmap ssl-enum-ciphers 仅 TLSv1.2/1.3,无 RC4/3DES ssl_protocols 未收紧
OCSP openssl ocsp -resp_text Cert Status: good OCSP 地址取错或中间证书不匹配
续期 certbot renew --dry-run all simulated renewals succeeded webroot 权限、80 端口被占

第五层:吊销状态与自动续期(Let’s Encrypt 证书有效期 90 天,官方建议每 60 天续期一次)

openssl x509 -in /etc/letsencrypt/live/sub.example.com/cert.pem -noout -ocsp_uri
# http://r3.o.lencr.org
openssl ocsp -issuer /etc/letsencrypt/live/sub.example.com/chain.pem 
  -cert /etc/letsencrypt/live/sub.example.com/cert.pem 
  -url http://r3.o.lencr.org -resp_text | grep -E 'Cert Status|This Update'
# Cert Status: good
certbot renew --dry-run   # 演练不写盘、不消耗速率限制
# Congratulations, all simulated renewals succeeded

第六层:把”剩余天数”变成告警

# 剩余天数一行流,可直接放进 cron 脚本
echo $(( ( $(date -d "$(openssl s_client -connect sub.example.com:443 -servername sub.example.com </dev/null 2>/dev/null 
  | openssl x509 -noout -enddate | cut -d= -f2)" +%s) - $(date +%s) ) / 86400 ))

规模化场景用 blackbox_exporter 0.25 的 probe_ssl_earliest_cert_expiry(Unix 时间戳,单位秒):probe_ssl_earliest_cert_expiry - time() < 21 * 86400 即触发告警,留出 21 天排查窗口,远早于 90 天到期。最后打开浏览器开发者工具 → Security → View certificate,确认 SAN 一栏只有你申请的那个子域名——多域名混签时,这里能最快发现”证书贴错站点”。

八、续期与权限治理:避免子域证书变成孤儿

给子域单独申请证书只要十分钟,但三个月后它可能已经悄悄续期失败——没有告警、没有负责人,这就是”孤儿证书”。根因通常不是 ACME 协议本身,而是 lineage 命名随意、renewal 配置随主机迁移丢失、私钥权限被改宽。以下五个动作把这条链路管住。

一、先固定 lineage 名,别让 certbot 自己编号。同一域名重复申请时,certbot 会自动派生 sub.example.com-0001 目录,之后脚本里写的路径与真实目录对不上,续期钩子全部失效。首次签发就锁定:

# 首次签发即锁定 lineage 名,避免出现 sub.example.com-0001
certbot certonly --webroot -w /var/www/acme 
  --cert-name sub.example.com 
  -d sub.example.com 
  --deploy-hook 'systemctl reload nginx'
# 结果目录:/etc/letsencrypt/live/sub.example.com/
# 续期配置:/etc/letsencrypt/renewal/sub.example.com.conf

据 certbot 文档,renewal/sub.example.com.conf 里的 webroot_map、authenticator、deploy_hook 是”这个证书怎么续”的唯一真相;迁移主机时必须把 live/、archive/、renewal/ 三个目录一起搬,只拷 live/ 下的软链接必然续期失败。

二、续期节奏要看得见。Let’s Encrypt 证书有效期 90 天,官方建议剩余 30 天时续期,以留出失败重试窗口。certbot 由 systemd timer 驱动,默认每天运行两次并叠加随机延迟,可用 systemctl list-timers 'certbot*' 确认下次触发时间;acme.sh 默认约 60 天续期,续期后需自行重载服务。

方案 默认续期周期 触发方式 重载 Nginx 的做法
certbot(≥1.10) 每天 2 次 + 随机延迟,剩余 30 天内才真正续 certbot.timer --deploy-hook 写入 renewal conf
acme.sh(3.x) 约 60 天 crontab 每 60 分钟检查 --renew-hook 'systemctl reload nginx'
cert-manager 1.15 到期前 2/3 时长(90 天证书约 30 天) Certificate 控制器 无需 reload,Secret 更新后 Nginx 重读
# acme.sh 续期后自动重载,注意 hook 写在签发时而非续期时
acme.sh --issue -d sub.example.com -w /var/www/acme 
  --renew-hook 'systemctl reload nginx'

三、私钥 600 root:root 是硬底线。Nginx 由 root 启动的 master 进程加载私钥,worker 进程降权到 nginx 用户后不再读文件,所以把私钥 chown 给 worker 用户既没必要也不安全。检查:

stat -c '%a %U:%G %n' /etc/letsencrypt/live/sub.example.com/privkey.pem
# 期望输出:600 root:root /etc/letsencrypt/live/sub.example.com/privkey.pem

权限过宽或属主错误时,Nginx 启动会直接报:BIO_new_file() failed (SSL: error:8000000D:system library::Permission denied)。

四、先 staging 再生产,避免打爆速率限制。Let’s Encrypt 对同一注册域名有每周 50 张的重复证书上限,调试期务必用 staging 端点(--dry-run 本身已默认走 staging):

certbot certonly --webroot -w /var/www/acme 
  --cert-name sub.example.com -d sub.example.com 
  --server https://acme-staging-v02.api.letsencrypt.org/directory

Kubernetes 场景用 cert-manager 1.15 的 Certificate 资源,secretName 按子域命名(如 tls-sub-example-com),不要让多个 Ingress 共享同一个 TLS Secret——共享会导致某个子域下线时把别人的证书一起换掉。

五、台账、告警与吊销。CMDB 至少记录 cert-name、SAN 列表、到期日、负责人,并在到期前 21 天告警(早于 Let’s Encrypt 建议的 30 天续期点,留出人工介入时间)。吊销前先留证据再动手:

# 先记录序列号与主题,便于事后审计
openssl x509 -noout -serial -subject -dates 
  -in /etc/letsencrypt/live/sub.example.com/cert.pem
# 再吊销指定 lineage,切勿指向主域 cert.pem
certbot revoke --cert-path /etc/letsencrypt/live/sub.example.com/cert.pem 
  --reason keycompromise

keyCompromise 对应 RFC 5280 定义的 reasonCode 1,是最常用的吊销理由。孤儿证书的判据很简单:certbot certificates 列出的每个 cert-name,都应能在 CMDB 里找到负责人和告警规则,找不到的就是孤儿。

单独证书的 SAN 只含 sub.example.com,私钥和续期任务独立;通配符 *.example.com 覆盖所有一级子域但不覆盖 a.b.example.com,且私钥需分发到多台机器。若子域由不同团队管理或需要单独吊销,单独证书更便于隔离。

不会。需要在 certbot 命令追加 -d www.sub.example.com,或在 CSR 的 SAN 写入 DNS:www.sub.example.com;否则浏览器会报证书名称不匹配。

检查 http://sub.example.com/.well-known/acme-challenge/ 是否被 301 到 HTTPS、是否被 WAF 拦截、Nginx root 是否与 certbot -w 一致;用 curl -I 查看状态码,放行该路径后重试。

有。据 Let's Encrypt Rate Limits 文档,同一注册域名每周最多 50 张证书、同一 exact set 每周最多 5 张重复证书;测试请用 staging 环境,生产失败后不要连续重试。

Let's Encrypt 证书有效期 90 天,需要自动续期;商业 DV 常见 1 年,OV 常见 1-2 年。无论哪种,建议到期前 21-30 天告警,并用 certbot renew –dry-run 或 acme.sh –cron 验证。

改用 DNS-01,在 _acme-challenge.sub.example.com 添加 TXT 记录,等待 dig TXT 生效后签发;或让 CDN 回源放行 /.well-known/acme-challenge 路径。Cloudflare 代理开启时 HTTP-01 可能被拦截,DNS-01 更可控。

如果子域与主域同一团队、同一入口、数量少,SAN 多域名证书可减少配置;如果子域独立主机、独立负责人、需要单独吊销或存在二级子域,选单独证书。价格上 Let's Encrypt 单独证书 0 元,商业 SAN 约 500-3000 元/年。


本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。