修复 SSL 证书不匹配
SSL 证书不匹配:域名不符
浏览器会显示 ERR_CERT_COMMON_NAME_INVALID、"Your connection is not private" 或 "doesn't match" 之类的提示。证书本身有效,但不适用于这个主机名。
看看证书覆盖了哪些域名:
openssl s_client -connect example.com:443 -servername example.com < /dev/null 2>/dev/null | openssl x509 -noout -ext subjectAltName
也可以用 SSL Checker。把列出的域名和你访问的域名对一下。
| 你访问的 | 证书覆盖的 | 结果 |
|---|---|---|
| www.example.com | 只有 example.com | 不匹配 |
| example.com | 只有 www.example.com | 不匹配 |
| shop.example.com | example.com, www.example.com | 不匹配 |
| shop.example.com | *.example.com | 匹配,通配符覆盖一级 |
| a.b.example.com | *.example.com | 不匹配,通配符只覆盖一级 |
常见原因
- 签发时没带上
www域名或子域名。 - 这个域名对应的虚拟主机没有证书,或者 SNI 没匹配上,服务器就返回了默认证书。
- CDN 或共享主机返回了别的客户的证书。
- DNS 指向了错误的服务器。
修法
- 用你实际用到的所有域名重新签发。在 TryFreeSSL.com 上可以免费做。
- 确认对应的 HTTPS 虚拟主机里写的是正确的
server_name或ServerName。 - 用 DNS Lookup 核对 DNS。
SSL 证书不匹配:私钥与证书
服务器起不来或重载失败,日志里有 "key values mismatch" 之类的内容。比对一下公钥:
openssl x509 -in certificate.crt -noout -pubkey | openssl sha256
openssl pkey -in private.key -pubout | openssl sha256
两个哈希不一样,说明文件来自两对不同的密钥。常见原因是续期时生成了新私钥但旧的还留在原处,或者文件是从两个不同目录拷来的。装上与这张证书的 CSR 配套的私钥,或者用新 CSR 重新签发并保留那把私钥。详见how to verify a key pair。
检查跳转和规范主机名
选一个规范主机名,把另一个跳过来。两个主机名仍然都要在证书里,否则先触发警告的反而是跳转本身。用 HTTPS Redirect tool 测一下。
快速判断
| 现象 | 怎么办 |
|---|---|
| 浏览器报警,服务器正常 | 检查证书里的域名 |
| 服务器起不来 | 比对证书和私钥 |
| 只有一个子域名报警 | 加进证书或用通配符 |
| 出现别的站点的证书 | 修虚拟主机或 SNI 配置 |
相关问题
证书链不全会是另一个错误,见chain checker guide。如果修完之后锁还是没出现,读why a site says Not Secure after installing SSL。
相关阅读:certificate not trusted errors、installation failed errors 和the wildcard certificate。
常见问题
通配符能覆盖不带 www 的域名吗?
单靠它不行。申请时把 example.com 和 *.example.com 都写上。
能忽略这个警告吗?
别让访客养成点过去的习惯。把证书修好。
为什么只在部分网络出现?
有些网络会拦截流量或解析到别的服务器。从两个网络各取一次证书对比。
Common Name 还用吗?
现代浏览器看的是 Subject Alternative Name 列表,所以域名必须写在那里。
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.