Apache AH01909 证书名称与服务器名不匹配:根因、修复、配置与验证
问题背景与根因:AH01909 到底在比较什么
AH01909 是 Apache mod_ssl 在启动、重载或首次处理 TLS 请求时输出的警告,典型形式为 AH01909: RSA certificate configured for example.com:443 does NOT include an ID which matches the server name。它表示当前 443 vhost 加载的证书中,CN/SAN 没有覆盖 Apache 认为的服务器名。警告本身不一定会让进程退出,但客户端会校验证书域名,浏览器常见 NET::ERR_CERT_COMMON_NAME_INVALID、SSL_ERROR_BAD_CERT_DOMAIN,curl 常见 (60) SSL: no alternative certificate subject name matches target host name。
根因通常不是证书过期,而是名称不匹配:vhost 复用了其他站点证书;ServerName/ServerAlias 与证书 SAN 不一致;ServerName 误写 https://example.com;多站点共用 IP 时第一个 443 vhost 成为默认服务器;通配符层级不够;IDN 域名未使用 punycode。Apache 按 IP:port 选择默认 vhost,再按 SNI 选择匹配 vhost;非 SNI 客户端只能拿到默认证书,所以默认 vhost 的证书必须覆盖其 ServerName。
sudo grep -R 'AH01909' /var/log/apache2/error.log /var/log/httpd/ssl_error_log
如果日志里同时出现 AH02032,说明 SNI 主机名与 HTTP Host 头也不一致,要一并检查反向代理、重定向和 ServerAlias。
快速定位:apachectl -S、证书 SAN 与线上证书三方核对
先不要改配置,先拿到三份证据:vhost 绑定、证书文件内容、线上实际返回的证书。执行 sudo apachectl -S(RHEL 用 sudo httpd -S),输出会标出每个 IP:port 的 default server 和 namevhost。示例:
VirtualHost configuration:
*:443 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2)
port 443 namevhost example.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2)
port 443 namevhost shop.example.com (/etc/apache2/sites-enabled/shop-ssl.conf:2)
重点看 default server 是否是证书覆盖的域名。然后列出所有 443 vhost 的 ServerName、ServerAlias 和证书路径:sudo grep -RInE 'ServerName|ServerAlias|SSLCertificateFile|SSLCertificateKeyFile|SSLCertificateChainFile' /etc/apache2/sites-enabled /etc/httpd/conf.d。
检查证书 SAN:sudo openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -subject -issuer -dates -ext subjectAltName。用 -checkhost 逐项验证:sudo openssl x509 -checkhost example.com -noout -in /etc/letsencrypt/live/example.com/fullchain.pem,再把 example.com 换成 www.example.com、shop.example.com。线上证书用:openssl s_client -connect 127.0.0.1:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName。如果线上证书与文件证书不是同一张,说明请求落到了别的 vhost 或默认 vhost。
完整修复步骤:从 ServerName 到证书绑定
按以下顺序修复,命令可直接执行。1)备份并列出 vhost:sudo cp -a /etc/apache2 /etc/apache2.bak.$(date +%F),RHEL 用 sudo cp -a /etc/httpd /etc/httpd.bak.$(date +%F);再执行 sudo apachectl -S 保存输出。
2)修正 ServerName:在 443 vhost 中写 ServerName example.com,需要带端口时写 ServerName example.com:443,不要写协议前缀;把所有对外域名放进 ServerAlias www.example.com shop.example.com。3)核对证书覆盖:sudo openssl x509 -checkhost www.example.com -noout -in /etc/letsencrypt/live/example.com/fullchain.pem,每个 ServerName/ServerAlias 都要返回 www.example.com does match。
4)缺域名就重签:sudo certbot certificates 查看现有证书;用 sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --expand --deploy-hook 'systemctl reload apache2' 重新签发,RHEL 把 reload 目标改为 httpd。5)把 vhost 指向本站证书:SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem、SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem,不要复用其他站点证书。
6)处理默认 443 vhost:用 apachectl -S 找到 default server,调整 sites-enabled 中文件名/加载顺序,或单独建 000-default-ssl.conf,确保其证书覆盖该默认 ServerName。7)重载并验证:sudo apachectl configtest && sudo systemctl reload apache2,RHEL 用 sudo systemctl reload httpd。若仍有告警,回到第 2 步重新核对 SAN 与 vhost 绑定。
配置示例:单站点、多站点与默认 443 vhost
单站点 vhost 示例,Apache 2.4.8+ 直接用 fullchain.pem,不要再单独指定 ChainFile:
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
DocumentRoot /var/www/example
</VirtualHost>
多站点时每个 443 vhost 使用自己的证书,ServerName 必须出现在对应证书 SAN 中:
<VirtualHost *:443>
ServerName shop.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/shop.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/shop.example.com/privkey.pem
DocumentRoot /var/www/shop
</VirtualHost>
默认 443 vhost 建议单独放在最先加载的文件,例如 /etc/apache2/sites-enabled/000-default-ssl.conf,其 ServerName 和证书必须一致。非 SNI 客户端只会访问这个默认 vhost,不能靠 SNI 分流。旧版 Apache 若没有 fullchain.pem,可用 SSLCertificateFile cert.pem 加 SSLCertificateChainFile chain.pem;2.4.8+ 优先 fullchain,避免链文件重复。
对比表格:名称匹配、证书有效期与常见旧认知
下表把 AH01909 排查中最容易混淆的项目放在一起。注意:截至 2026 年 5 月,公共可信 TLS 证书单张最长有效期已不是 398 天,而是 200 天;2027-03-15 起降至 100 天,2029-03-15 起降至 47 天。Let’s Encrypt 默认仍为 90 天,必须用自动续期;更短周期证书以官方公告和账户能力为准。
| 项目 | 旧认知/常见写法 | 当前建议 |
|---|---|---|
| 公共 TLS 证书最长有效期 | 398 天 | 200 天(2026-03-15 起;2027-03-15 起 100 天,2029-03-15 起 47 天) |
| Let’s Encrypt 有效期 | 90 天,可手动续 | 默认 90 天,必须自动续期;短周期选项以官方为准 |
| 域名匹配字段 | 只看 CN | 现代客户端以 SAN 为准,CN 仅兼容 |
| ServerName 写法 | https://example.com |
example.com 或 example.com:443,禁止协议前缀 |
| 通配符覆盖 | *.example.com 匹配所有子域 |
只匹配一级子域,a.b.example.com 需单独 SAN |
| 证书链配置 | 总是单独写 ChainFile | Apache 2.4.8+ 优先 fullchain.pem |
表格里的每一项都可能单独触发 AH01909 或客户端域名校验失败,排查时不要只盯着证书到期时间。
常见错误排查:真实报错与处理
AH01909: RSA certificate configured for example.com:443 does NOT include an ID which matches the server name:先看该 vhost 的 ServerName/ServerAlias 是否都在证书 SAN 中;再确认SSLCertificateFile指向的是本站证书,而不是其他站点或默认证书。AH02032: Hostname example.com:443 provided via SNI and hostname www.example.com provided via HTTP are different:SNI 主机名与 HTTP Host 头不一致,常见于反向代理、CDN 回源或重定向。检查ProxyPreserveHost、后端 Host 头,并让 ServerName/ServerAlias 覆盖两个名称。- 浏览器报
NET::ERR_CERT_COMMON_NAME_INVALID或SSL_ERROR_BAD_CERT_DOMAIN:用openssl s_client -connect example.com:443 -servername www.example.com -showcerts </dev/null看实际返回证书;若 SAN 不对,重新签发而不是只改 Apache。 curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com':证书缺少 www 的 SAN。执行sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --expand后重载 Apache。apachectl -S显示 default server 是别的站点:把默认 443 vhost 改成证书覆盖的域名,或调整加载顺序;不要假设所有客户端都会发送 SNI。AH02565: Certificate and private key ... do not match:证书与私钥不配对,虽然不属于名称不匹配,但常与复制错文件同时出现。用openssl x509 -noout -modulus和openssl rsa -noout -modulus比对指纹。
效果验证:配置、线上握手与日志三关
第一关,配置语法与 vhost 绑定:sudo apachectl configtest 应返回 Syntax OK;sudo apachectl -S 中 default server 和每个 namevhost 的证书路径要对应正确。第二关,线上握手:openssl s_client -connect example.com:443 -servername example.com -verify_hostname example.com -verify_return_error </dev/null,看到 Verify return code: 0 (ok) 才算通过;再对 www.example.com、shop.example.com 逐个执行。
第三关,HTTP 客户端与日志:curl -sS -o /dev/null -w '%{http_code} %{ssl_verify_result}' https://example.com/,正常应输出 200 0,其中 0 表示证书校验通过;若输出非 0,继续看 openssl s_client 的 SAN。重载后检查日志:sudo grep -R 'AH01909' /var/log/apache2/error.log /var/log/httpd/ssl_error_log | tail -n 20,新日志中不应再出现该告警。
最后做续期演练:sudo certbot renew --dry-run,确认续期后 deploy hook 会重载 Apache。对外站点可再用 SSL Labs 或 openssl s_client -connect 域名:443 -servername 域名 复核证书链、SAN 和有效期。
常见问题
它通常是 mod_ssl 的启动或重载警告,不一定立即中断服务;但证书域名不匹配会让浏览器报 NET::ERR_CERT_COMMON_NAME_INVALID、curl 报 error 60,实际访问会失败。应视为必须修复的配置错误,不要只把日志级别调低。
现代浏览器和 TLS 客户端主要校验 SAN,不再只看 CN。若 SAN 缺少 www.example.com、shop.example.com 或 ServerAlias 中的域名,即使 CN 正确也会不匹配。用 openssl x509 -ext subjectAltName 检查,并重新签发包含全部域名的证书。
Apache 对每个 IP:port 把第一个加载的 443 vhost 作为默认服务器。用 apachectl -S 查看 default server,调整 sites-enabled 中文件名或加载顺序,或单独建 000-default-ssl.conf,并确保其证书覆盖该默认 ServerName。非 SNI 客户端只能拿到默认证书。
不能。ServerName 只接受主机名,可带端口,不能带协议前缀。写成 https://example.com 会被当作错误主机名参与匹配,直接触发 AH01909。应改为 ServerName example.com 或 ServerName example.com:443,并把协议跳转放在 Redirect 或 Rewrite 规则里。
不能。通配符证书只匹配同一级域名,*.example.com 可匹配 www.example.com、shop.example.com,但不匹配 a.b.example.com。后者必须单独加入 SAN,或改用覆盖该名称的证书;否则 Apache 和浏览器都会报名称不匹配。