跳到主要内容
ssl证书按装

网站 HTTP 转 HTTPS 与 SSL 证书安装配置实战指南(Nginx/Apache/IIS)

网站 HTTP 转 HTTPS 与 SSL 证书安装配置实战指南(Nginx/Apache/IIS) — 网站 HTTP 转 HTTPS 与 SSL 证书安装配置实战指南(Nginx/Apache/IIS)

问题背景与根因:HTTP 为什么必须升级 HTTPS

HTTP 以明文传输 URL、Cookie、表单和登录态,公共 Wi-Fi、代理和链路劫持都能读取或篡改内容。浏览器会把 HTTP 页面标记为「不安全」,HTTP/2 在实际部署中也基本要求 TLS。升级 HTTPS 不是只装一个证书文件,而是同时满足四件事:证书链完整、证书域名与访问域名匹配、私钥匹配、证书在有效期内。最常见的根因是只部署了叶子证书而缺少中间证书,导致部分 Android 或 curl 报 unable to get local issuer certificate;其次是证书只签了 example.com,却用 www.example.com 访问;第三是页面里的图片、JS、CSS 仍写死 http://,形成混合内容;第四是反向代理后应用仍判断为 HTTP,产生 301 循环。

有效期规则必须更新:公开信任的 TLS 证书最长有效期为 398 天,这是 CA/B Forum 与 Apple 等根计划共同约束的结果;Let’s Encrypt 免费证书有效期是 90 天,靠 ACME 自动续期,并不是原文所说的一年免费证书。商业 DV 证书最长也只能签 398 天,OV/EV 同理。因此正确做法是使用 Certbot、acme.sh 等 ACME 客户端自动续期,并把到期监控做成告警。免费证书可选 Let’s Encrypt、ZeroSSL、Google Trust Services 等 ACME 服务;Symantec DV 免费证书的说法已经过时,Symantec 品牌证书现由 DigiCert 运营,商业证书需按实际产品购买。下文以 Nginx 为主,给出 Apache、IIS 的配置和排错命令。

前置检查与证书选型:域名、DNS、端口与有效期规则

先确认域名解析和端口。HTTP-01 验证要求 80 端口可从公网访问,DNS-01 则不要求 80 端口但需要能改 TXT 记录。执行以下命令检查 A/AAAA 记录、80/443 监听和当前 HTTP 响应:

dig +short A example.com
dig +short AAAA www.example.com
ss -lntp | grep -E ':80|:443'
curl -I http://example.com
ufw allow 'Nginx Full'

如果使用云服务器,还要在安全组放行 80 和 443。证书选型可按场景:Let’s Encrypt 适合个人站和中小企业,90 天有效期,Certbot 自动续期;ZeroSSL 提供 ACME 和商业套餐;商业 DV 适合需要发票、人工支持或更长有效期的场景,但最长仍为 398 天;OV/EV 需要组织验证,浏览器 UI 已不再突出 EV 标识,不必为绿色地址栏买单。通配符证书必须用 DNS-01,普通多域名证书可用 HTTP-01。若用 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'

注意 CSR 里的 SAN 必须同时包含裸域和 www,否则浏览器会报证书域名不匹配。证书文件通常需要 fullchain.pem 和 privkey.pem;Nginx 的 ssl_certificate 应指向 fullchain,而不是单独的 cert.pem。若使用商业证书,拿到的可能是 .crt、.ca-bundle、.pfx,需要按服务器类型转换。上线前记录当前证书到期时间:openssl x509 -in fullchain.pem -noout -dates。同时确认服务器系统时间准确,时间偏差会导致证书被判定为未生效或已过期。

完整实操步骤:用 Certbot 为 Nginx 申请并安装证书

在 Ubuntu/Debian 上安装 Certbot 的 Nginx 插件,然后一次性签发并让 Certbot 自动写入 443 配置和 80 跳转。命令中的邮箱用于到期通知,域名必须已解析到本机:

apt update
apt install -y certbot python3-certbot-nginx
certbot --nginx -d example.com -d www.example.com --redirect --agree-tos -m admin@example.com --no-eff-email
certbot certificates
systemctl enable --now certbot.timer
certbot renew --dry-run

如果 80 端口被其他程序占用,或你希望自己控制 Nginx 配置,可用 webroot 模式只申请证书:

mkdir -p /var/www/html/.well-known/acme-challenge
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com
nginx -t && systemctl reload nginx

Certbot 生成的证书在 /etc/letsencrypt/live/example.com/ 下,fullchain.pem 是叶子证书加中间链,privkey.pem 是私钥。不要移动或重命名这两个文件,续期后路径不变,Nginx 重载即可加载新证书。检查自动续期定时器:systemctl list-timers | grep certbot,默认每天检查两次,证书剩余 30 天内续期。若使用 acme.sh,可执行 acme.sh --issue -d example.com -d www.example.com --webroot /var/www/html,再用 --install-cert 把 fullchain 和 key 安装到 Nginx 路径并设置 reloadcmd。无论哪种客户端,都要做一次 dry-run,确认 80 端口验证和 reload 命令都能成功。签发失败时先看 /var/log/letsencrypt/letsencrypt.log,常见原因是 DNS 未生效、80 被防火墙拦截、Nginx 配置语法错误。

配置示例:Nginx、Apache 与 IIS 的 HTTPS 与 301 跳转

Nginx 推荐把 80 和 443 分成两个 server。80 只做 301,443 加载 fullchain 和私钥,并启用 TLS 1.2/1.3。以下配置中 HSTS 先不要加 includeSubDomains,确认所有子域都支持 HTTPS 后再加:

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';
    add_header Strict-Transport-Security 'max-age=31536000' always;
    location / { root /var/www/html; index index.html; }
}

Apache 先启用模块,再配置 443 虚拟主机和 80 跳转。SSLCertificateFile 指向 fullchain,私钥权限设为 600:

a2enmod ssl headers rewrite
a2ensite example-ssl.conf
systemctl reload apache2
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>
<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
    Header always set Strict-Transport-Security 'max-age=31536000'
</VirtualHost>

IIS 需要先导入含中间链的 PFX,再绑定 443。PowerShell 示例:Import-PfxCertificate -FilePath 'C:/certs/example.pfx' -CertStoreLocation 'Cert:\LocalMachine\My',然后 New-WebBinding -Name 'Default Web Site' -Protocol https -Port 443 -HostHeader 'example.com' -SslFlags 1。HTTP 跳转在站点根 web.config 的 rewrite 规则中匹配 (.*),条件 {HTTPS} 等于 OFF,操作类型 301,目标 https://{HTTP_HOST}/{R:1}。绑定后用 netsh http show sslcert 检查证书哈希和主机名。三者的共同点是:80 端口只做跳转,443 加载完整链,证书私钥必须匹配。

HTTP 到 HTTPS 强制跳转与 HSTS 配置对比表

301 是永久跳转,浏览器和搜索引擎会缓存;调试阶段可先用 302,确认无误再换 301。HSTS 通过 Strict-Transport-Security 响应头告诉浏览器未来一段时间只走 HTTPS,它只在 HTTPS 响应中生效,且一旦下发,浏览器在 max-age 内不会允许用户忽略证书错误。建议先设置 max-age=300 测试,再逐步提升到 31536000;includeSubDomains 要等所有子域都完成 HTTPS 后再加,preload 需要向浏览器厂商提交申请,不要随意开启。

项目 Nginx Apache IIS
80 跳转 return 301 https://$host$request_uri; Redirect permanent / https://example.com/ URL Rewrite 条件 {HTTPS}=OFF,301
证书文件 fullchain.pem + privkey.pem fullchain.pem + privkey.pem PFX 导入本地计算机个人存储
HSTS 指令 add_header Strict-Transport-Security ‘max-age=31536000’ always; Header always set Strict-Transport-Security ‘max-age=31536000’ web.config 出站规则或应用代码添加
自动续期 certbot.timer + reload nginx certbot.timer + reload apache2 win-acme 计划任务 + 重新绑定
常见坑 http2 指令版本差异 未启用 headers 模块 PFX 缺中间链导致链不完整

表中 Nginx 的 http2 写法在新版本中推荐 listen 443 ssl; 加 http2 on;,旧版本可写 listen 443 ssl http2;,混用会报 duplicate listen options。Apache 的 HSTS 头必须启用 mod_headers,否则配置不生效但不一定报错。IIS 的 PFX 最好在导出时勾选包含所有证书路径,否则客户端可能只收到叶子证书。跳转目标要统一到主域名,例如统一跳到 https://example.com,避免 www 与裸域互相跳转造成循环。对比表的核心结论是:协议层配置差异不大,真正决定成败的是证书链、域名 SAN、私钥匹配和代理头。

常见错误排查:证书链、域名不匹配、混合内容与跳转循环

排错先看真实报错,不要只看浏览器提示。以下命令能定位证书链、域名和有效期:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates。常见错误一:curl: (60) SSL certificate problem: unable to get local issuer certificate,说明服务端只发了叶子证书,Nginx 的 ssl_certificate 必须用 fullchain.pem,Apache 用 fullchain 或配置 SSLCertificateChainFile,IIS 导入含链 PFX。错误二:浏览器 NET::ERR_CERT_AUTHORITY_INVALID,多为自签证书、链不完整或系统时间错误。错误三:SSL_ERROR_BAD_CERT_DOMAIN,证书 SAN 不含当前域名,重新签发时加 -d www.example.com。错误四:控制台 Mixed Content: The page at 'https://example.com' was loaded over HTTPS, but requested an insecure resource 'http://cdn.example.com/a.js',把资源改成 https 或相对协议,临时可用 CSP upgrade-insecure-requests。

错误五:ERR_TOO_MANY_REDIRECTS,常见于 Nginx 反代后应用仍看到 HTTP,需在 location 中加 proxy_set_header X-Forwarded-Proto $scheme;,并让应用信任该头。错误六: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 -in fullchain.pem | openssl md5 和 openssl rsa -noout -modulus -in privkey.pem | openssl md5 对比。错误七:certbot: Problem binding to port 80: Could not bind to IPv4 or IPv6,80 被占用,停掉占用进程或改用 webroot/DNS-01。错误八:Apache AH01909: RSA certificate configured for example.com does not include an ID which matches the server name,检查 ServerName 与证书 SAN。错误九:IIS HTTP Error 403.4 - Forbidden,站点要求 SSL 但绑定或跳转规则错误,检查 443 绑定和 URL Rewrite。排错顺序建议:先 openssl 看链,再 curl -I 看跳转,最后看应用日志和浏览器 Console。

效果验证与自动续期监控

验证分四层:跳转、证书链、HSTS、续期。先看 80 是否 301 到 HTTPS,再看 443 是否返回 200:

curl -I http://example.com
curl -I https://example.com
curl -sI https://example.com | grep -i strict-transport-security
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -checkend 2592000

第一条应返回 HTTP/1.1 301 Moved Permanently 和 Location: https://example.com/;第二条应返回 200;grep 应看到 HSTS 头;openssl 输出中 Issuer 应包含中间 CA,日期应覆盖当前时间;checkend 2592000 表示 30 天内不过期时返回 0,否则返回 1。再用 certbot renew --dry-run 模拟续期,看到 Congratulations 才算自动续期可用。生产环境建议加到期监控:openssl x509 -enddate -noout -in /etc/letsencrypt/live/example.com/fullchain.pem,把结果接入 Zabbix、Prometheus blackbox_exporter 或云监控,阈值设为剩余 21 天。浏览器侧用开发者工具 Console 搜 Mixed Content,用 Network 面板看失败资源;外部可用 SSL Labs 扫描,目标 A 或 A+,重点看证书链、协议版本和 HSTS。最后用 grep -R 'http://' /var/www/html 扫硬编码 HTTP 资源,但注意不要把 XML 命名空间和注释误判。

常见问题

免费,有效期 90 天,不是一年。它通过 ACME 协议自动续期,Certbot 默认在剩余 30 天内续签。商业 DV 证书最长有效期为 398 天,也需要在到期前续期。不要手工记日期,启用 certbot.timer 并做 renew –dry-run 和到期告警。

301 只解决入口协议,不解决证书链和混合内容。检查 openssl 输出是否缺中间证书、证书 SAN 是否含当前域名、页面是否仍加载 http:// 资源。Console 里的 Mixed Content 和 NET::ERR_CERT_AUTHORITY_INVALID 是最直接线索,按报错修链或改资源地址。

可以。HTTP-01 验证需要 80 端口,若 80 被封或占用,可改用 DNS-01,通过添加 TXT 记录验证域名控制权,适合通配符证书。Certbot 可用 DNS 插件,acme.sh 也支持阿里云、Cloudflare 等 DNS API。webroot 模式仍需要 80 可访问。

通常不需要重启系统。Nginx 执行 nginx -t 后 systemctl reload nginx;Apache 执行 apachectl configtest 后 systemctl reload apache2;IIS 绑定证书后回收应用池即可。只有更换监听端口、内核参数或证书存储异常时才考虑重启服务,避免直接重启生产服务器。

不建议。先确认全站 HTTPS、证书链正确、无混合内容,再用 max-age=300 测试,逐步提升到 31536000。includeSubDomains 要求所有子域都支持 HTTPS,否则子域会被浏览器强制阻断。preload 需向浏览器厂商申请,加入后移除周期很长,务必谨慎。

内容审核

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