跳到主要内容
ssl证书按装

网站 HTTPS CA 证书配置实战:SSL/TLS、单域名与通配符证书、HTTPS 泛解析排错指南

网站 HTTPS CA 证书配置实战:SSL/TLS、单域名与通配符证书、HTTPS 泛解析排错指南 — 网站 HTTPS CA 证书配置实战:SSL/TLS、单域名与通配符证书、HTTPS 泛解析排错指南

问题背景与根因:HTTP 明文、证书覆盖与链路校验

网站从 HTTP 切换到 HTTPS,不只是“买一张证书装上去”这么简单。浏览器在 TLS 握手时会校验证书 SAN 域名、有效期、签发链、SNI 返回证书,以及页面资源是否仍走 http。常见根因有四类:只给首页配了 HTTPS,内页仍跳 http;证书 SAN 不含当前访问域名;Nginx/Apache 只配了 leaf 证书,缺少中间 CA,导致链不完整;页面里的图片、CSS、JS 写死 http,触发 Mixed Content。公共 TLS 证书单张最长有效期按 398 天执行,若 CA 执行更短策略以 CA 公告为准;Let’s Encrypt 默认 90 天,必须自动续期。DNS 的“泛解析”只负责把 *.example.com 解析到服务器,不等于通配符证书自动覆盖所有子域。

快速定位可执行:

curl -Iv https://example.com 2>&1 | grep -E 'SSL|subject|issuer|HTTP/'
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

若 SAN 不含当前域名,查证书类型;若出现 unable to get local issuer certificate,查 fullchain 中间链;若 HTTPS 页面仍请求 http 资源,查 CMS、CDN 和反代配置里的协议头。

证书类型与覆盖规则:单域名、多域名、通配符怎么选

单域名证书只覆盖一个 FQDN,例如 example.com;多域名证书通过 SAN 覆盖多个明确域名,例如 example.com、www.example.com、api.example.com;通配符证书用 *.example.com 覆盖同一级子域,例如 a.example.com、shop.example.com,但不覆盖 example.com,也不覆盖 a.b.example.com。实际签发时 CN 已不再是唯一依据,必须检查 SAN。若既要主域又要所有一级子域,SAN 应同时写 DNS:example.com 和 DNS:*.example.com。若有多级子域,需增加 DNS:*.sub.example.com 或改用多域名证书。DV 证书只验证域名控制权,适合大多数网站;OV/EV 增加组织审核,但浏览器 UI 差异已很小,不应把 EV 当作“加密更强”。

查看已有证书覆盖范围:

openssl x509 -in cert.pem -noout -subject -issuer -dates -ext subjectAltName
openssl x509 -in fullchain.pem -noout -ext subjectAltName

选型原则:域名固定且少,用单域名或多域名;子域数量多且都是一级子域,用通配符;需要 apex 主域,务必把 example.com 加入 SAN。不要用“HTTPS 泛解析”代替证书规划,DNS 通配和证书通配是两件事。

实操一:用 OpenSSL 生成 CSR 与私钥(单域名/通配符)

CSR 是向 CA 申请证书时提交的公钥和域名信息,私钥必须留在本机。以下命令生成 RSA 2048 位私钥和单域名/多域名 CSR:

umask 077
openssl req -new -newkey rsa:2048 -sha256 -nodes -keyout example.com.key -out example.com.csr -subj '/C=CN/ST=Shanghai/L=Shanghai/O=Example/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'
openssl req -in example.com.csr -noout -text | grep -A2 'Subject Alternative Name'
openssl rsa -in example.com.key -check
chmod 600 example.com.key

通配符 CSR 把 CN 和 SAN 改为:

openssl req -new -newkey rsa:2048 -sha256 -nodes -keyout wildcard.key -out wildcard.csr -subj '/CN=*.example.com' -addext 'subjectAltName=DNS:*.example.com,DNS:example.com'

注意:CA 以 SAN 为准,CN 仅作兼容;通配符必须用 DNS-01 验证。若使用 ECDSA,可用 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1,但需确认旧客户端兼容性。CSR 可公开,私钥绝不能上传或提交工单;Nginx/Apache 读取私钥的用户需有权限,建议 root 或专用证书目录。

实操二:ACME 自动签发与续期(Let’s Encrypt 90 天)

Let’s Encrypt 默认有效期 90 天,适合用 ACME 自动化。Nginx 站点可用 HTTP-01 验证,certbot 会自动改配置并加跳转:

sudo apt-get update
sudo apt-get install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com --redirect
sudo certbot renew --dry-run
sudo certbot certificates
systemctl list-timers | grep certbot

通配符证书必须用 DNS-01,因为 HTTP-01 无法证明 *.example.com 的控制权。手动模式示例:

sudo certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com

按提示在 DNS 添加 TXT 记录 _acme-challenge.example.com,值用 certbot 输出;若一次申请多个通配符,可能需添加多条 TXT,等 TTL 生效后再回车。生产环境建议用 DNS 插件自动写入,例如 certbot-dns-cloudflare、certbot-dns-aliyun,避免手动续期失败。续期后执行 nginx -t && systemctl reload nginx,或用 –deploy-hook 自动 reload。检查证书路径通常在 /etc/letsencrypt/live/example.com/ 下的 fullchain.pem 与 privkey.pem。

配置示例:Nginx/Apache 启用 TLS 与 HTTP 跳转

Nginx 示例把 80 端口 301 到 HTTPS,443 加载 fullchain 和私钥,仅启用 TLS 1.2/1.3。fullchain 必须包含站点证书和中间 CA,顺序是 leaf 在前、中间证书在后。

server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://$host$request_uri;
}
server {
  listen 443 ssl;
  http2 on;
  server_name example.com www.example.com;
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 1d;
  add_header Strict-Transport-Security 'max-age=31536000' always;
  location / { root /var/www/html; index index.html; }
}

老版本 Nginx 用 listen 443 ssl http2;,新版本用 http2 on;。Apache 需启用 ssl、headers 模块:

sudo a2enmod ssl headers
<VirtualHost *:80>
  ServerName example.com
  Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLProtocol -all +TLSv1.2 +TLSv1.3
  Header always set Strict-Transport-Security 'max-age=31536000'
</VirtualHost>

改完执行 nginx -t && systemctl reload nginx 或 apachectl configtest && systemctl reload apache2。HSTS 会强制浏览器后续走 HTTPS,建议全站和所有子域都支持 HTTPS 后再加,避免把自己锁在门外。

HTTPS 泛解析与通配符证书:最容易混淆的边界

DNS 泛解析是“解析记录”:在 DNS 里配置 *.example.com A 1.2.3.4,任意一级子域都会解析到该 IP。通配符证书是“身份凭证”:*.example.com 只匹配一个标签,例如 a.example.com、shop.example.com;不匹配 example.com,也不匹配 a.b.example.com。二者可以一起用,但不能互相替代。若业务有 user.api.example.com,需要额外申请 *.api.example.com 或把具体域名加入 SAN。SNI 场景下,同一 IP 多证书时,客户端必须发送 SNI,服务端才能返回正确证书。

验证命令:

dig +short test.example.com
dig +short TXT _acme-challenge.example.com
openssl s_client -connect example.com:443 -servername test.example.com </dev/null 2>/dev/null | openssl x509 -noout -ext subjectAltName
curl -Iv https://test.example.com

若 dig 能解析但浏览器报证书域名不匹配,说明解析正常、证书覆盖不对;若 openssl 返回的 SAN 有 *.example.com 但访问的是 example.com,则 apex 主域未被覆盖。若访问 a.b.example.com 报错,不要重复申请同一张 *.example.com,应补 *.b.example.com 或具体域名。泛解析还会把不存在的子域解析到源站,建议在 Nginx 加默认 server 返回 444,减少扫描和误解析流量。

证书选型对比表:单域名、多域名、通配符与 Let’s Encrypt

下表按常见公共 CA 和 ACME 场景对比。公共 TLS 证书单张最长有效期按 398 天执行,CA 若执行更短策略以公告为准;Let’s Encrypt 默认 90 天。选择时先看域名层级,再看验证方式和运维能力。

类型 覆盖示例 验证方式 有效期 适用场景 限制
单域名证书 example.com HTTP-01 或 DNS-01 最长 398 天 单站点、单入口 不覆盖 www 或子域
多域名 SAN 证书 example.com、www.example.com、api.example.com HTTP-01 或 DNS-01 最长 398 天 域名固定且数量少 新增域名需重签
通配符证书 *.example.com,可加 example.com 必须 DNS-01 最长 398 天 大量一级子域 不覆盖 apex 和多级子域
Let’s Encrypt 按申请 SAN 覆盖 HTTP-01 或 DNS-01 默认 90 天 自动化运维、成本敏感 必须自动续期
商业 OV/EV 按 SAN 覆盖 组织审核 + 域名验证 以 CA 上限为准 需要组织信息展示 审核周期和成本更高

如果子域数量多但都是 a.example.com 这一级,通配符最省事;如果还有 example.com 和 a.b.example.com,通配符必须配合 SAN 或额外证书。不要为了“看起来高级”选 EV,加密强度由 TLS 协议和套件决定,不由 DV/OV/EV 决定。

常见错误排查与效果验证

常见错误排查

报错 NET::ERR_CERT_COMMON_NAME_INVALID 或 SSL_ERROR_BAD_CERT_DOMAIN:证书 SAN 不含当前域名。执行 openssl x509 -in fullchain.pem -noout -ext subjectAltName 核对;通配符不覆盖 apex 和多级子域。报错 curl: (60) SSL certificate problem: unable to get local issuer certificate:缺中间 CA 或 fullchain 顺序错误,用 openssl s_client -showcerts -connect example.com:443 查看链。Nginx 启动报 nginx: [emerg] SSL_CTX_use_PrivateKey_file(…) failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch):证书和私钥不匹配,用 openssl x509 -noout -modulus 与 openssl rsa -noout -modulus 对比。浏览器报 Mixed Content: The page at ‘https://…’ was loaded over HTTPS, but requested an insecure resource ‘http://…’:页面资源仍走 http,改 CDN、CMS、反代 X-Forwarded-Proto。ERR_SSL_PROTOCOL_ERROR:协议/端口/SNI 不匹配,检查 listen 443 ssl、TLS 版本和证书路径。ACME 报 Challenge failed … Invalid response … 404:HTTP-01 路径 /.well-known/acme-challenge/ 被重写或防火墙拦截。

效果验证

curl -I http://example.com
curl -I https://example.com
curl -Iv https://example.com 2>&1 | grep -E 'SSL certificate verify|subject|issuer|HTTP/'
openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/null 2>/dev/null | openssl x509 -noout -dates -subject -issuer -ext subjectAltName
nginx -t && nginx -T | grep -E 'ssl_certificate|ssl_protocols|Strict-Transport-Security'
certbot certificates
systemctl list-timers | grep certbot

验证标准:http 返回 301 到 https;curl 显示 SSL certificate verify ok;SAN 含当前域名;有效期未过期;证书链完整;TLS 1.2/1.3 可握手;HSTS 头存在;页面无 Mixed Content;续期定时器正常。上线前再用 SSL Labs 或 testssl.sh 复测,目标至少 A,关键业务再处理 HSTS preload。

常见问题

不能。*.example.com 只匹配 a.example.com 这类一级子域,不匹配 example.com。若主域也要 HTTPS,申请时把 example.com 加入 SAN,或单独签发单域名证书,否则浏览器会报证书域名不匹配。


需要。DNS 泛解析只负责把子域解析到服务器,TLS 握手仍校验证书 SAN。没有对应通配符或多域名证书,浏览器会报 NET::ERR_CERT_COMMON_NAME_INVALID,访问不会因解析成功而自动加密。


不必。用 certbot 或 acme.sh 配置自动续期,执行 certbot renew –dry-run 并检查 systemd timer 即可。商业证书可减少运维投入,但加密强度由 TLS 协议和套件决定,不由证书品牌决定。


公共 TLS 证书单张上限长期按 398 天执行,若 CA 执行更短策略以 CA 公告为准。Let’s Encrypt 默认 90 天,因此不要依赖手工续期,应配置定时任务和到期告警,避免证书过期。


这是 Mixed Content。把图片、CSS、JS 的 http:// 改为 https:// 或协议相对 URL,并检查 CDN、CMS、反代是否传递 X-Forwarded-Proto。浏览器控制台会列出具体不安全资源,逐条替换即可。


证书和私钥不匹配。分别用 openssl x509 -noout -modulus 与 openssl rsa -noout -modulus 计算模数,两者一致才可配对;不一致就重新签发或找回对应私钥,再执行 nginx -t 重载。

内容审核

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