HTTPS 证书安装配置与 SSL 证书申请全指南:CSR、Let’s Encrypt、Nginx 跳转与报错排查
问题背景与根因:为什么 HTTPS 证书配置仍是上线必答题
HTTPS 不是装个证书就完事,而是域名、私钥、证书链、TLS 协议、跳转和混合内容共同生效。旧文提到微信 2016-12-01、Apple 2017-01-01、Chrome 2017-01-01 强制证书,这些是历史节点;现在微信小程序服务器域名必须 HTTPS 且不能使用自签名证书,iOS ATS 默认要求 TLS 1.2 以上,Chrome、Edge、Firefox 会对 HTTP 页面标记不安全。公共信任 TLS 证书已没有长期 CA 证书:自 2020-09-01 起 Apple 信任的公开 TLS 证书最长 398 天,Let’s Encrypt 为 90 天,因此必须自动化续期。常见根因包括:只部署域名证书未部署中间证书,导致链不完整;Nginx 使用 cert.pem 而非 fullchain.pem;证书 SAN 不包含 www 或裸域;CDN 回源 443 未放行;80 跳 443 规则循环;页面仍加载 http:// 图片脚本。先确认业务域名、证书类型、部署入口(Nginx、Apache、IIS、负载均衡、CDN),再按私钥、CSR、签发、部署 fullchain、续期、验证的顺序处理。
证书类型、有效期与选型对比
按验证强度分 DV、OV、EV:DV 只验证域名控制权,签发快,适合博客、API、测试和大多数业务站;OV 验证组织身份,适合企业官网和对外系统;EV 曾用于浏览器地址栏绿色公司名,现在主流浏览器已弱化 EV 展示,不建议仅为绿条采购。按覆盖范围分单域名、通配符、多域名 SAN:单域名只保护一个 FQDN;通配符 *.example.com 只覆盖同一级子域,不覆盖 example.com 和 a.b.example.com;多域名可在一张证书放多个 SAN。有效期方面,公开信任证书最长 398 天,Let’s Encrypt 90 天,续期应交给 certbot 或 acme.sh。选型时先列域名清单,再决定是否加 www、裸域、API 子域、CDN 域名。自签名证书只能内网测试,小程序、App Store、浏览器公开访问会报错。
| 方案 | 覆盖范围 | 有效期 | 适用场景 | 注意点 |
|---|---|---|---|---|
| Let’s Encrypt DV | 单域名或多域名 SAN | 90 天 | 大多数网站、API、测试环境 | 免费,需开放 80 或配置 DNS API 自动续期 |
| 商业 DV | 单域名、通配符、多域名 | 最长 398 天 | 小程序、无 80 端口、需要通配符 | 付费,部署时注意 fullchain 证书链 |
| 商业 OV | 单域名或多域名 | 最长 398 天 | 企业官网、对外业务系统 | 需组织验证,签发周期通常 1 至 3 天 |
| 商业 EV | 单域名或多域名 | 最长 398 天 | 强合规展示场景 | 浏览器弱化 EV 展示,不建议只为绿条采购 |
| 安装配置与代维 | 按域名和入口 | 随证书 | 无运维团队、CDN 或负载均衡复杂 | 价格随 CA 与渠道变化,验收看链、跳转、续期 |
完整实操步骤:私钥、CSR 与 Let’s Encrypt 签发
先在服务器生成私钥和 CSR。以下命令可直接执行,OpenSSL 1.1.1 以上才支持 -addext 写入 SAN;如果版本较旧,请用 openssl-san.cnf 配置 subjectAltName。私钥权限必须 600,CSR 可以交给 CA,私钥绝不能上传或发邮件。
mkdir -p /etc/ssl/example.com && cd /etc/ssl/example.com
openssl genrsa -out example.com.key 2048
chmod 600 example.com.key
openssl req -new -sha256 -key 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 -A1 'Subject Alternative Name'
使用 Let’s Encrypt 时,webroot 方式要求 80 端口可访问,域名已解析到本机。先 dry-run 再正式签发;通配符必须用 DNS-01,生产环境应接 DNS API,不建议长期用 –manual。
sudo apt install certbot python3-certbot-nginx
sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --email admin@example.com --agree-tos --no-eff-email --dry-run
sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --email admin@example.com --agree-tos --no-eff-email
sudo certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com
配置示例:Nginx 80 跳 443、TLS 参数与 Apache 要点
Nginx 的 ssl_certificate 必须指向 fullchain.pem,ssl_certificate_key 指向 privkey.pem。只写 cert.pem 会缺中间证书,浏览器和部分客户端会报证书链错误。Nginx 1.25.1 以上用 http2 on;旧版本把 listen 443 ssl 改为 listen 443 ssl http2。修改后先 nginx -t,再 reload。
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 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
root /var/www/html;
index index.html;
}
Apache 2.4.8 以上使用 fullchain 即可,旧版本再配 SSLCertificateChainFile。启用模块和站点后执行 apachectl configtest,再 reload。若前面有 CDN 或负载均衡,证书要同时部署到边缘节点和回源入口,否则会出现 526 或回源证书校验失败。
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
a2enmod ssl
apachectl configtest && systemctl reload apache2
证书链、自动续期与部署检查
浏览器信任的是叶子证书、中间证书、根证书组成的链。Nginx 的 ssl_certificate 必须指向 fullchain.pem,ssl_certificate_key 指向 privkey.pem;只填 cert.pem 会出现 unable to verify the first certificate 或 NET::ERR_CERT_AUTHORITY_INVALID。Apache 2.4.8 以上用 fullchain 即可,旧版本再配 SSLCertificateChainFile。Let’s Encrypt 90 天有效,certbot 安装时已带 systemd timer 或 cron;检查 systemctl list-timers | grep certbot,手动测试 sudo certbot renew –dry-run。续期后要 reload:sudo certbot renew –deploy-hook ‘systemctl reload nginx’,或写 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh 并 chmod +x。若用 CDN 或负载均衡,证书要上传到 CDN 边缘,回源也要受信;否则源站 443 证书错误会被 CDN 报 526。部署前用 openssl x509 -in fullchain.pem -noout -dates -subject 和 openssl crl2pkcs7 -nocrl -certfile fullchain.pem | openssl pkcs7 -print_certs -noout 检查链。
常见错误排查:真实报错与修复
- NET::ERR_CERT_AUTHORITY_INVALID:证书链不完整或自签名。用 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 看是否返回中间证书;Nginx 改用 fullchain.pem。
- SSL_ERROR_BAD_CERT_DOMAIN 或证书域名不匹配:SAN 缺少 www、裸域或 API 子域。执行 openssl x509 -in cert.pem -noout -text | grep -A1 ‘Subject Alternative Name’ 核对,重新签发包含全部域名。
- unable to verify the first certificate:客户端拿不到中间证书。合并顺序应为叶子证书在前、中间证书在后;不要只部署 cert.pem。
- ERR_SSL_PROTOCOL_ERROR:协议或加密套件不匹配。确认 ssl_protocols 至少包含 TLSv1.2,老旧客户端才考虑兼容;用 openssl s_client -tls1_2 测试。
- 526 Invalid SSL certificate:常见于 CDN 回源。检查源站 443 证书是否受信、是否过期、回源域名与证书 SAN 是否一致。
- ERR_TOO_MANY_REDIRECTS:80 跳 443 与 CDN 强制 HTTPS 叠加,或 Nginx 在 443 里又 301 到 https。用 curl -I http://example.com 和 curl -I https://example.com 逐跳查看 Location。
- Mixed Content:HTTPS 页面加载 http:// 图片、JS、CSS。把资源改为 https://,必要时加 Content-Security-Policy: upgrade-insecure-requests,并检查 CDN 回源协议。
效果验证与上线检查清单
上线后不要只看浏览器小锁。先验证 80 跳 443、证书链、协议版本和 HSTS。curl -I http://example.com 应返回 301 且 Location 为 https;curl -I https://example.com 应返回 200,并看到 Strict-Transport-Security。openssl s_client 要看到 Verify return code: 0 (ok),Protocol 为 TLSv1.2 或 TLSv1.3。再用浏览器开发者工具检查 Security 面板、Console 混合内容、Network 中是否仍有 http 请求。最后用 SSL Labs 或 testssl.sh 做外部评分,目标 A 或 A+;同时检查证书到期时间、自动续期 timer、CDN 证书同步和回源 443 放行。若使用小程序,还要在微信公众平台配置 request 合法域名,确保域名已备案、证书受信、TLS 版本满足要求。
curl -I http://example.com
curl -I https://example.com
curl -vI https://example.com 2>&1 | grep -E 'SSL|subject|issuer|expire'
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null
openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/null | grep -E 'Protocol|Verify return code'
ss -lntp | grep 443
sudo nginx -t && sudo systemctl reload nginx
sudo certbot renew --dry-run
常见问题
现在还能申请多年期或长期 CA 证书吗?
不能。公开信任 TLS 证书最长 398 天,Let’s Encrypt 为 90 天。所谓长期证书通常是内部 CA 或旧文宣传,公开浏览器和小程序不信任。正确做法是缩短续期周期,用 certbot 或 acme.sh 自动续期并 reload。
小程序和 APP 必须使用 HTTPS 吗?
微信小程序服务器域名必须 HTTPS,且不能使用自签名证书;iOS ATS 默认要求 TLS 1.2 以上;Chrome 等浏览器会对 HTTP 页面标记不安全。上线前要确认域名已备案、证书 SAN 正确、TLS 版本满足平台要求。
通配符证书能覆盖二级域名和裸域吗?
*.example.com 只覆盖 a.example.com 这类同一级子域,不覆盖 a.b.example.com,也不覆盖裸域 example.com。若业务同时使用裸域和 www,需在 SAN 中额外加入 example.com,或分别签发。
为什么浏览器提示证书无效但 curl 正常?
常见原因是证书链不完整、客户端根证书不同、SNI 未命中或 CDN 回源证书错误。用 openssl s_client -showcerts 检查服务端是否返回中间证书,Nginx 使用 fullchain.pem,Apache 旧版本补 SSLCertificateChainFile。
配置 HTTPS 后出现混合内容怎么办?
把页面中的 http:// 图片、JS、CSS、接口全部改为 https:// 或协议相对地址,检查 CDN 回源协议和数据库中的旧链接。可临时加 Content-Security-Policy: upgrade-insecure-requests,但根因仍是资源地址未改。
证书续期后需要重启 Nginx 吗?
不需要重启,reload 即可。执行 sudo nginx -t && sudo systemctl reload nginx;certbot 可配置 deploy-hook 自动 reload。若使用 CDN,还要把新证书同步到边缘节点并检查回源证书。