Let’s Encrypt 签发失败 Failed authorization / Timeout during connect:从 80 端口排查到 DNS-01 兜底
问题背景与根因:ACME 服务器为什么连不上你的 80 端口
HTTP-01 是 Let’s Encrypt 默认的域名验证方式:CA 会从多个境外出口 IP 发起请求,访问 http://你的域名/.well-known/acme-challenge/<token>,并在 5 秒内期望拿到状态码 200、内容精确等于 <token>.<账户密钥指纹> 的响应。这条链路任何一环不通,certbot 都会报 Failed authorization procedure,最常见的 Detail 是 Timeout during connect (likely firewall problem)。
典型报错如下。注意 Detail 里的 IP 是 CA 看到的源站地址,如果它和你的真实公网 IP 不一致,说明 DNS 解析到了 CDN 或其他机器:
Certbot failed to authenticate some domains (authenticator: webroot).
Domain: example.com
Type: connection
Detail: 203.0.113.10: Fetching
http://example.com/.well-known/acme-challenge/8f3aC...:
Timeout during connect (likely firewall problem)
Hint: The Certificate Authority failed to download the temporary
challenge file(s) created in the webroot directory.
按出现频率排序,根因主要有五类:
- 80 端口没有对公网开放:云安全组只放行了 443、iptables/nftables 默认 DROP、运营商封禁 80(国内未备案域名常见)。
- 80 端口没有进程监听:Nginx 只写了
listen 443 ssl,或者监听在 127.0.0.1:80。 - 强制 HTTPS 跳转抢在验证路径之前:server 级别的
return 301会在 location 匹配前执行,CA 收到 301 而不是 200。 - DNS 指错:A 记录指向旧服务器,或 AAAA 记录指向一台没有 IPv6 监听的机器,CA 优先走 IPv6 直接超时。
- 中间层拦截:CDN/WAF 拦截
/.well-known/、回源到多台后端而 webroot 内容不一致、境外 IP 被限流。
还要记住两个时间常数:Let’s Encrypt 证书有效期 90 天,官方建议剩余 30 天时续期;公开信任的 TLS 证书最长有效期受 CA/B Forum 限制为 398 天。失败验证另有严格限流:同一账户对同一主机名每小时最多 5 次失败,触发后必须等 1 小时,所以调试阶段务必先加 --dry-run 或使用 staging 环境。
第一步:确认 80 端口公网可达(监听、防火墙、安全组)
用三条命令把「本机监听 → 本机防火墙 → 云安全组」逐层排除,不要跳步。
# 1. 是否有进程监听 0.0.0.0:80
ss -lntp 'sport = :80'
# 期望:LISTEN 0 511 0.0.0.0:80 ... users:((nginx,pid=...))
# 若显示 127.0.0.1:80,说明只监听本地,需改成 listen 80;
# 2. 本机防火墙
firewall-cmd --list-all # firewalld,确认 services 含 http
firewall-cmd --permanent --add-service=http && firewall-cmd --reload
ufw status verbose # ufw,确认 80/tcp ALLOW
ufw allow 80/tcp
iptables -S INPUT | grep -E 'dpt:80|policy'
nft list ruleset | grep -A8 'chain input'
云厂商安全组要单独放行入方向 TCP:80,源地址至少不能只放行办公网 IP。阿里云、腾讯云、AWS 的安全组与实例内防火墙是两套体系,只改一边不生效。
接着从服务器外部验证。先放一个测试文件:
mkdir -p /var/www/html/.well-known/acme-challenge
echo ok > /var/www/html/.well-known/acme-challenge/probe
chmod -R 755 /var/www/html/.well-known
curl -sS -o /dev/null -w '%{http_code}n'
http://example.com/.well-known/acme-challenge/probe # 期望 200
本机 curl 通不代表 CA 能通,因为可能命中本地 hosts 或回环。要在另一台公网机器上执行,或用 --resolve 强制指定源站 IP:
curl -v --resolve example.com:80:203.0.113.10
http://example.com/.well-known/acme-challenge/probe
如果这里超时,在服务器上抓包确认请求是否到达:tcpdump -i any -n port 80。没有包说明被安全组或运营商拦截;有 SYN 无 ACK 说明本机防火墙 DROP。最后检查 DNS,尤其是 AAAA:
dig +short A example.com
dig +short AAAA example.com
dig +short CAA example.com
若 AAAA 返回地址但服务器没有 IPv6 监听,直接删除该记录或补齐 listen [::]:80,这是「浏览器能开、CA 超时」的高频原因。也可用 letsdebug.net 的 HTTP-01 检查,它会从境外多节点复现 CA 的访问路径。
第二步:处理强制 HTTPS 跳转与 Web 服务器路径拦截
Nginx 的 return 写在 server 块里会在 location 匹配之前执行,所以把跳转放在 server 级、把 ACME 路径放在 location 级是无效的。正确写法是把 301 收进 location /,并给验证路径单独的 root:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# 精确前缀匹配,优先级高于 location /
location ^~ /.well-known/acme-challenge/ {
root /var/www/html;
default_type text/plain;
try_files $uri =404;
}
location / {
return 301 https://$host$request_uri;
}
}
要点:root /var/www/html 意味着文件真实路径是 /var/www/html/.well-known/acme-challenge/<token>,必须与 certbot -w 的路径完全一致;try_files $uri =404 能让文件缺失时返回 404 而不是落到单页应用入口,便于排错。改完执行 nginx -t && systemctl reload nginx。
Apache 用 Alias 加 RewriteCond 排除验证路径:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
Alias /.well-known/acme-challenge/ /var/www/html/.well-known/acme-challenge/
<Directory /var/www/html/.well-known/acme-challenge>
Require all granted
Options -Indexes
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
权限与 SELinux 也常被忽略:
chown -R www-data:www-data /var/www/html/.well-known
chmod 755 /var/www/html/.well-known /var/www/html/.well-known/acme-challenge
# SELinux 开启时
chcon -R -t httpd_sys_content_t /var/www/html/.well-known
# 或永久规则
semanage fcontext -a -t httpd_sys_content_t '/var/www/html/.well-known(/.*)?'
restorecon -Rv /var/www/html/.well-known
如果站点前面有 CDN 或 WAF,需要把 /.well-known/acme-challenge/ 设为不缓存、不拦截、回源到源站。判断方法很直接:curl -I http://example.com/.well-known/acme-challenge/probe,看返回头里有没有 CDN 标识(如 cf-ray、x-cache),有就说明请求没到源站。
第三步:certbot 与 acme.sh 的完整签发命令
确认链路通后,按部署形态选择签发方式。webroot 最通用,不需要停服务:
apt install -y certbot
certbot certonly --webroot -w /var/www/html
-d example.com -d www.example.com
--email ops@example.com --agree-tos --no-eff-email
--keep-until-expiring
Nginx 插件会自动改配置并处理跳转,适合新手:
apt install -y python3-certbot-nginx
certbot --nginx -d example.com -d www.example.com --redirect
# 只签发不改配置
certbot certonly --nginx -d example.com
standalone 模式要求 80 端口空闲,适合没有 Web 服务的机器:
systemctl stop nginx
certbot certonly --standalone -d example.com --preferred-challenges http
systemctl start nginx
正式签发前一定先演练,staging 环境不计入正式限流:
certbot certonly --webroot -w /var/www/html -d example.com --dry-run
# 或显式使用测试 CA
certbot certonly --webroot -w /var/www/html -d example.com --staging
acme.sh 更轻量,且天然支持 DNS-01 与自动安装证书:
curl https://get.acme.sh | sh -s email=ops@example.com
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
# HTTP-01
~/.acme.sh/acme.sh --issue -d example.com -d www.example.com
-w /var/www/html --keylength ec-256
# 安装证书并配置续期后重载
~/.acme.sh/acme.sh --install-cert -d example.com
--key-file /etc/nginx/ssl/example.com.key
--fullchain-file /etc/nginx/ssl/example.com.fullchain.pem
--reloadcmd 'systemctl reload nginx'
需要自己生成 CSR 的场景(私钥必须留在本机,只把 CSR 交给工具):
openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key
-out example.com.csr -subj '/CN=example.com'
-addext 'subjectAltName=DNS:example.com,DNS:www.example.com'
certbot certonly --webroot -w /var/www/html --csr example.com.csr
排错时打开详细日志:certbot -vvv ... 2>&1 | tee /tmp/certbot.log,服务端日志在 /var/log/letsencrypt/letsencrypt.log,acme.sh 用 --debug 2。日志里会写明 CA 请求的完整 URL 和返回码,比控制台摘要有用得多。
第四步:80 端口不可用时切换到 DNS-01(acme.sh + DNS API)
当 80 端口被封、境外访问受限,或需要签发通配符证书时,DNS-01 是唯一稳妥方案:CA 只查询 _acme-challenge.example.com 的 TXT 记录,完全不碰你的 Web 服务。
# 阿里云 DNS
export Ali_Key='LTAI5t...'
export Ali_Secret='...'
~/.acme.sh/acme.sh --issue --dns dns_ali
-d example.com -d '*.example.com' --keylength ec-256
# Cloudflare
export CF_Token='...'
export CF_Account_ID='...'
~/.acme.sh/acme.sh --issue --dns dns_cf -d example.com -d '*.example.com'
# DNSPod
export DP_Id='123456'
export DP_Key='...'
~/.acme.sh/acme.sh --issue --dns dns_dp -d example.com -d '*.example.com'
凭据会被保存在 ~/.acme.sh/account.conf,建议给 API 密钥最小权限(只允许修改指定域名解析),并限制该文件权限为 600。手动模式适合无法给 API 权限的场景:
~/.acme.sh/acme.sh --issue --dns -d example.com
--yes-I-know-dns-manual-mode-enough-go-ahead-please
# 按提示在 DNS 服务商添加 TXT 记录后执行
~/.acme.sh/acme.sh --renew -d example.com
--yes-I-know-dns-manual-mode-enough-go-ahead-please
记录没生效是 DNS-01 失败的主因,用两个公共 DNS 交叉验证:
dig +short TXT _acme-challenge.example.com @8.8.8.8
dig +short TXT _acme-challenge.example.com @1.1.1.1
dig +short NS example.com
常见坑:TXT 记录名误写成 _acme-challenge.example.com.example.com(很多面板会自动补根域名);同时签根域名和泛域名时会有两条同名 TXT,必须都保留;TTL 建议 60~120 秒;如果域名配置了 CAA,必须包含 0 issue letsencrypt.org,否则报 CAA record prevents issuance。
DNS-01 的续期同样走 cron,acme.sh 安装时会自动写入,确认一下:crontab -l | grep acme.sh,正常应看到 17 0 * * * /root/.acme.sh/acme.sh --cron --home /root/.acme.sh > /dev/null。
验证方式对比:HTTP-01、DNS-01、TLS-ALPN-01 怎么选
三种验证方式的能力边界差别很大,选型时按端口可用性和是否需要泛域名来定:
| 验证方式 | 依赖端口 | 支持泛域名 | 需要公网 HTTP | 自动化难度 | 典型场景 |
|---|---|---|---|---|---|
| HTTP-01 | 80(可 301 到 443,首个请求仍打 80) | 否 | 是 | 低 | 普通单站、VPS 自建 |
| DNS-01 | 无入站要求,需能调用 DNS API | 是 | 否 | 中 | 80 被封、CDN 回源、内网、通配符 |
| TLS-ALPN-01 | 443 | 否 | 否(需 443) | 中 | 只开放 443 的边缘节点 |
再按「故障现象 → 首选方案」给一张速查表:
| 现象 | 判断依据 | 处理 |
|---|---|---|
| Timeout during connect | tcpdump 抓不到 SYN | 放行安全组 80,或改 DNS-01 |
| Invalid response … 404 | curl 验证路径返回 404 | 对齐 webroot 与 root 路径 |
| Invalid response … 301 | 返回头含 Location: https | 把 301 收进 location / |
| no TXT record found | dig 查不到记录 | 修正记录名、等待传播 |
| too many failed authorizations | 报错含 rate-limits 链接 | 等 1 小时,先用 –dry-run |
两点选型经验:其一,HTTP-01 的重定向上限是 10 次,最终地址可以是 http 或 https 的 80/443,但首个请求必须落在 80;其二,DNS-01 的 TXT 记录验证通过后可以删除,acme.sh 默认在续期时重新写入,因此不要把清理脚本做成定时任务,否则续期必然失败。对于同时有 CDN 和多台源站的架构,优先 DNS-01,可以彻底绕开回源路径不一致的问题。
常见错误排查:真实报错与对应处理
先看日志,再看报错原文,最后用外部工具复现,顺序不要颠倒:
tail -n 200 /var/log/letsencrypt/letsencrypt.log
grep -i 'error|detail|challenge' /var/log/letsencrypt/letsencrypt.log | tail -20
tail -f /var/log/nginx/access.log | grep acme-challenge
以下是高频报错与对应处理:
- Timeout during connect (likely firewall problem):CA 的 SYN 没到服务器。按第一节查安全组、iptables、运营商封禁;国内未备案域名 80 被封时只能改 DNS-01。
- Invalid response from http://…/.well-known/acme-challenge/xxx: 404:文件不在 CA 请求的路径下。核对
-w与 Nginxroot是否指向同一目录,注意root与alias的路径拼接差异。 - … : 301 / 302:跳转抢在验证前。按第二节把
return 301移进location /。 - … : 403:目录权限或 WAF 拦截。用
namei -l /var/www/html/.well-known/acme-challenge/probe检查每一级是否可读。 - Fetching https://…: Connection refused:重定向到 443 但 443 未监听,或证书未生效导致 TLS 握手失败。
- Problem binding to port 80: Could not bind to IPv4 or IPv6:standalone 模式下 Nginx 仍占用 80,先
systemctl stop nginx。 - urn:ietf:params:acme:error:dns :: no TXT record found:TXT 未生效或记录名错误,用
dig +short TXT _acme-challenge.example.com @8.8.8.8复核。 - The key authorization file from the server did not match this challenge:多台后端内容不一致或命中缓存,临时摘掉负载均衡的其他节点。
- CAA record for example.com prevents issuance:
dig CAA example.com后添加0 issue letsencrypt.org。 - too many failed authorizations recently:触发每小时 5 次的失败限流,等待 1 小时,期间用
--dry-run调配置。
外部诊断工具:letsdebug.net 的 HTTP-01 检查会给出多节点探测结果;curl -v 看完整握手与响应头;tcpdump -i any -n port 80 确认包是否到达。三者结合基本能在 10 分钟内定位到具体环节。
效果验证与自动续期检查
签发成功后不要只看 certbot 的 Congratulations,直接查线上证书:
openssl s_client -connect example.com:443 -servername example.com
</dev/null 2>/dev/null | openssl x509 -noout
-subject -issuer -dates -ext subjectAltName
# 只看到期时间
echo | openssl s_client -connect example.com:443 -servername example.com
2>/dev/null | openssl x509 -noout -enddate
# 剩余不足 14 天返回非 0
openssl x509 -checkend 1209600 -noout -in /etc/letsencrypt/live/example.com/fullchain.pem
确认 SAN 覆盖了所有域名(example.com 与 www.example.com 必须在同一张证书里,否则浏览器报名称不匹配),并确认链完整:
curl -sS -o /dev/null -w '%{http_code} %{ssl_verify_result}n' https://example.com
# 期望 200 0;非 0 表示证书链或主机名有问题
续期状态检查:
certbot certificates # 查看到期日与证书路径
certbot renew --dry-run # 演练续期,不消耗限额
systemctl list-timers | grep -i certbot
# 输出应含 certbot.timer,通常每天运行两次
~/.acme.sh/acme.sh --list # acme.sh 管理的证书
~/.acme.sh/acme.sh --cron --home ~/.acme.sh # 手动触发一次续期检查
crontab -l | grep acme.sh
生产环境建议加两层监控:一是到期前 30/14/7 天各告警一次,用 openssl x509 -checkend 写进巡检脚本;二是在证书更新后自动 reload 并校验,例如 --deploy-hook 'systemctl reload nginx',避免出现证书换了但 Nginx 仍加载旧文件的情况。最后用 SSL Labs 或 openssl s_client -tls1_3 做一次评级与协议检查,确认没有残留 TLS 1.0/1.1。
注意 Let’s Encrypt 证书有效期 90 天,续期窗口是到期前 30 天;公开信任证书最长有效期 398 天的规则只影响商业 CA 的签发策略,不改变 Let’s Encrypt 的 90 天节奏,因此自动续期必须始终处于可用状态。
常见问题
必须。CA 首个请求固定打到 http://域名:80/.well-known/acme-challenge/,虽然允许 301 跳到 443,但 80 不通就收不到跳转。只有 443 时改用 DNS-01 或 TLS-ALPN-01。
浏览器可能走 CDN、缓存,或你的网络可达,而 CA 是从境外多个出口直连源站。常见原因是安全组只放行办公 IP、AAAA 记录指向无 IPv6 监听的机器、WAF 拦截境外访问。用 letsdebug.net 复现最快。
在 80 端口的 server 块里为 /.well-known/acme-challenge/ 单独配置 root,并把 return 301 放进 location / 内,因为 server 级 return 会在 location 匹配前执行。改完 nginx -t 再 reload。
这是失败验证限流:同一账户对同一主机名每小时最多 5 次失败,触发后等 1 小时自动恢复。期间不要反复重试,先用 certbot –dry-run 或 –staging 把配置调通再正式签发。
先确认记录名是 _acme-challenge.example.com,而不是被面板补成 _acme-challenge.example.com.example.com;再用 dig +short TXT _acme-challenge.example.com @8.8.8.8 和 @1.1.1.1 对比,权威 NS 未同步时等 1~5 分钟,TTL 建议 60。
有效期 90 天,官方建议剩余 30 天时续期。certbot 通过 systemd timer 每天检查两次,acme.sh 通过 cron 每天检查。公开信任 TLS 证书最长有效期受 CA/B Forum 限制为 398 天,但不影响 90 天节奏。