ZeroSSL 证书 IIS 部署完整指南:PFX 打包、SNI 绑定、TLS 1.2 加固与报错排查
问题背景:为什么 IIS 部署 ZeroSSL 证书总在“链”和“绑定”上出问题
IIS 与 Nginx、Apache 的证书加载模型完全不同:它不读取磁盘上的 PEM 文件,而是把证书导入 Windows 证书存储(LocalMachineMy),再由 HTTP.sys 在内核态完成 TLS 握手。ZeroSSL 网页端签发完成后通常给出三个文件:certificate.crt(叶子证书)、ca_bundle.crt(中间证书)、private.key(私钥)。很多教程只让你把 certificate.crt 导入 IIS,结果本机浏览器显示小锁正常,而 curl、Java、微信小程序、旧版 Android 却报证书链不完整——因为本机信任库里恰好缓存了中间证书,外部客户端没有。
第二个高频根因是协议与绑定。Windows Server 2012 / 2012 R2 的 Schannel 默认没有为服务端启用 TLS 1.2,证书虽然签出来了,握手却直接失败;同一台服务器跑多个站点时,443 端口未启用 SNI,所有域名返回同一张证书,浏览器报 ERR_CERT_COMMON_NAME_INVALID。
第三个根因是有效期管理。ZeroSSL 免费 DV 证书有效期 90 天,付费 OV/EV 受 CA/Browser Forum 约束,单张证书最长不超过 398 天(后续还将进一步缩短至 200 天、100 天)。续期后如果只替换了磁盘文件、没更新 Windows 证书存储里的绑定,IIS 仍然吐出旧证书。理解这三条,后面的操作才有意义。
第一步:生成密钥与 CSR,并核对 ZeroSSL 下发的文件
ZeroSSL 网页流程(90 天免费 DV)有两种方式:让它自动生成密钥对,或由你提供 CSR。生产环境建议自己生成私钥,避免私钥经手第三方。在 Linux 或已装 OpenSSL 的 Windows 上执行:
openssl req -new -newkey rsa:2048 -nodes
-keyout example.com.key
-out example.com.csr
-subj "/C=CN/ST=Zhejiang/L=Hangzhou/O=Example/CN=example.com"
-addext "subjectAltName=DNS:example.com,DNS:www.example.com"
关键点:subjectAltName 必须包含所有要用的域名,现代浏览器只看 SAN,不看 CN;-nodes 表示私钥不加密,便于后续打包 PFX,但私钥文件权限要收紧为 chmod 600。生成后把 example.com.csr 内容粘贴到 ZeroSSL 控制台,选择 HTTP 文件验证或 DNS CNAME 验证,通过后下载证书压缩包。
拿到文件先自检,确认域名、有效期、签发者与密钥匹配:
openssl x509 -in certificate.crt -noout -subject -issuer -dates -ext subjectAltName
openssl rsa -in example.com.key -noout -modulus | openssl md5
openssl x509 -in certificate.crt -noout -modulus | openssl md5
两条 modulus 输出的 MD5 必须完全一致,否则说明私钥与证书不匹配,导入 IIS 必然失败。
第二步:把 PEM 打包成 IIS 可用的 PFX(必须含完整链)
IIS 导入需要 PKCS#12 格式的 .pfx,其中必须同时包含叶子证书、中间证书和私钥。ZeroSSL 有时直接提供 pfx,多数情况需要自己转换。核心命令只有一个,重点是 -certfile 参数不能漏:
cat certificate.crt ca_bundle.crt > fullchain.pem
openssl pkcs12 -export
-out example.com.pfx
-inkey example.com.key
-in certificate.crt
-certfile ca_bundle.crt
-name "example.com"
-passout pass:Str0ngPfxPwd
若下载包里已含 fullchain.pem,直接 -in fullchain.pem 亦可;但绝不能只写 -in certificate.crt 又不加 -certfile,那正是“链不完整”的源头。导入前先验证包内证书数量:
openssl pkcs12 -in example.com.pfx -info -nokeys -passin pass:Str0ngPfxPwd
输出中应能看到 subject=CN = example.com 的叶子证书,以及 ZeroSSL 中间证书(如 ZeroSSL RSA Domain Secure Site CA)。OpenSSL 3.x 默认用 AES-256 + PBES2 加密,Windows Server 2012 R2 及更早版本可能不识别,导入报密码错误时改用 -legacy:
openssl pkcs12 -export -legacy -out example.com.pfx -inkey example.com.key
-in certificate.crt -certfile ca_bundle.crt -passout pass:Str0ngPfxPwd
第三步:在 IIS 中导入 PFX 并完成 443 端口 SNI 绑定
图形界面路径:IIS 管理器 → 选中服务器节点 → 双击“服务器证书” → 右侧“导入” → 选择 pfx、输入导出密码。更推荐 PowerShell,便于写进发布脚本:
$pwd = ConvertTo-SecureString -String 'Str0ngPfxPwd' -AsPlainText -Force
Import-PfxCertificate -FilePath C:certsexample.com.pfx `
-CertStoreLocation Cert:LocalMachineMy -Password $pwd
Get-ChildItem Cert:LocalMachineMy |
Where-Object { $_.Subject -like '*example.com*' } |
Select-Object Subject, Thumbprint, NotAfter
记下 Thumbprint,然后建立 HTTP.sys 层绑定。注意 hostnameport 写法是 SNI 的关键,多站点共用 443 时必须用域名而非 0.0.0.0:443:
netsh http add sslcert hostnameport=example.com:443 ^
certhash=1A2B3C4D5E6F7890ABCDEF1234567890ABCDEF12 ^
appid={4dc3e181-e14b-4a21-b022-59fc669b0914} certstorename=MY
IIS 站点绑定可图形操作(站点 → 绑定 → 添加 → https / 443 / 主机名 example.com / 选择证书),也可用 appcmd:
%windir%system32inetsrvappcmd.exe set site /site.name:"example.com" ^
/+bindings.[protocol='https',bindingInformation='*:443:example.com']
若站点已有旧的 443 绑定,先 netsh http delete sslcert hostnameport=example.com:443 再重建,否则新旧证书会冲突。
第四步:配置 HTTP 跳转 HTTPS 与 TLS 1.2 加固
跳转依赖 URL 重写模块(URL Rewrite 2.1),未安装需先装。在站点根目录 web.config 的 system.webServer 中加入规则,务必排除 ACME 验证路径,否则 ZeroSSL 续期验证会被 301 打断:
<rewrite>
<rules>
<rule name="ForceHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="^/.well-known/" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
协议加固走 Schannel 注册表。Windows Server 2012 R2 上必须显式启用 TLS 1.2 服务端,否则证书再正确也握手失败:
reg add "HKLMSYSTEMCurrentControlSetControlSecurityProvidersSCHANNELProtocolsTLS 1.2Server" /v Enabled /t REG_DWORD /d 1 /f
reg add "HKLMSYSTEMCurrentControlSetControlSecurityProvidersSCHANNELProtocolsTLS 1.2Server" /v DisabledByDefault /t REG_DWORD /d 0 /f
reg add "HKLMSYSTEMCurrentControlSetControlSecurityProvidersSCHANNELProtocolsTLS 1.0Server" /v Enabled /t REG_DWORD /d 0 /f
改完必须重启系统(shutdown /r /t 0),仅 iisreset 不会重载 Schannel 配置。生产环境建议先用 IISCrypto 备份再改,避免远程桌面被锁在门外。
完整配置示例:web.config、HSTS 与自动续期脚本
把跳转、HSTS 与安全响应头合并到一个 web.config,可直接放到站点根目录(IIS 10 / Windows Server 2016 及以上):
<configuration>
<system.webServer>
<rewrite> ... 上文的 ForceHTTPS 规则 ... </rewrite>
<httpProtocol>
<customHeaders>
<add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
<add name="X-Content-Type-Options" value="nosniff" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
注意 HSTS 的 max-age 一旦下发便无法撤回,建议先用 300 秒测试,确认全站 HTTPS 无混合内容后再改为一年。ZeroSSL 免费证书 90 天到期,续期后必须重新导入 PFX 并刷新绑定,可用下面脚本一次完成“导入 + 换绑”:
$pfx = Import-PfxCertificate -FilePath C:certsexample.com.pfx `
-CertStoreLocation Cert:LocalMachineMy `
-Password (ConvertTo-SecureString 'Str0ngPfxPwd' -AsPlainText -Force)
netsh http delete sslcert hostnameport=example.com:443
netsh http add sslcert hostnameport=example.com:443 certhash=$($pfx.Thumbprint) `
appid={4dc3e181-e14b-4a21-b022-59fc669b0914} certstorename=MY
iisreset /noforce
旧证书不要立即删除,保留一个续期周期便于回滚;PFX 导出密码应存入凭据管理系统,而不是明文写在脚本里。
方案对比:导入方式、证书格式与签发方式怎么选
三种导入与绑定方式各有适用场景,自动化程度差别明显:
| 方式 | 入口/命令 | 优点 | 局限 |
|---|---|---|---|
| IIS 管理器图形界面 | 服务器证书 → 导入 | 直观,适合单台一次性部署 | 无法审计,易漏勾 SNI |
| PowerShell | Import-PfxCertificate |
可批量化、可写入 CI/CD | 需管理员权限,旧系统需装模块 |
| netsh + certutil | netsh http add sslcert |
直接控制 HTTP.sys 绑定,排障精准 | 参数多,appid 写错会报 1312 |
证书格式对比:
| 项目 | PEM(crt + key) | PFX(PKCS#12) |
|---|---|---|
| IIS 是否支持 | 不支持(无私钥) | 原生支持 |
| 私钥存放 | 分离文件,靠权限保护 | 打包加密,需导出密码 |
| 典型用途 | Nginx / Apache / HAProxy | IIS / Exchange / RDP |
签发方式上,ZeroSSL 网页流程对新手友好、90 天免费 DV、签发快,但续期需人工;ACME 客户端(win-acme、Certify)可自动完成验证并刷新 IIS 绑定,适合域名多、要求零停机的场景。付费 OV/EV 有效期上限 398 天,需要企业实名材料,适合对外展示主体身份的站点。
常见错误排查:真实报错与对应处理
1)curl: (60) SSL certificate problem: unable to get local issuer certificate:证书链不完整。用 openssl pkcs12 -in example.com.pfx -info -nokeys 确认包内是否有中间证书;没有就带 -certfile ca_bundle.crt 重新打包。只重装证书、不改 PFX 是无效的。
2)导入 PFX 报 0x80070520 或“指定的网络密码不正确”:OpenSSL 3.x 默认 PBES2/AES-256,旧版 Windows 不识别。用 -legacy 重新导出,或用 certutil -importPFX -p 密码 example.com.pfx 交叉验证。
3)浏览器报 ERR_CERT_COMMON_NAME_INVALID:访问域名不在 SAN 列表。检查 openssl x509 -in certificate.crt -noout -ext subjectAltName,缺 www 只能重新签发,靠 IIS 绑定“补救”无效。
4)netsh http add sslcert 返回 1312“指定的登录会话不存在”:appid 的 GUID 格式错误或缺失大括号。固定用 {4dc3e181-e14b-4a21-b022-59fc669b0914} 即可。
5)续期后浏览器仍显示旧证书:绑定还指向旧 Thumbprint。执行 netsh http show sslcert hostnameport=example.com:443 比对哈希,删除后重建绑定,再 iisreset /noforce。
6)事件日志出现 Schannel 36871 / 36874:协议版本或加密套件不匹配,回到第四步启用 TLS 1.2 并重启系统,同时确认客户端未强制 TLS 1.0。
效果验证:从本机绑定到外部客户端的四层检查
第一层确认绑定已落到 HTTP.sys:
netsh http show sslcert hostnameport=example.com:443
输出中的“证书哈希”应与 Get-ChildItem Cert:LocalMachineMy 里新证书的 Thumbprint 一致,“应用程序 ID”为上文那个 GUID。
第二层在服务器外部验证链与有效期,务必加 -servername,否则拿到的是默认站点证书:
openssl s_client -connect example.com:443 -servername example.com -showcerts < /dev/null
| openssl x509 -noout -subject -issuer -dates
curl -vI https://example.com 2>&1 | grep -E "SSL certificate verify|expire"
出现 SSL certificate verify ok 才算链完整。第三层做静态校验:openssl verify -CAfile ca_bundle.crt certificate.crt,返回 OK 说明打包无误。
第四层用真实客户端:手机 4G 网络(不走公司代理)、微信开发者工具、Java keytool 各访问一次,再跑 SSL Labs 或 ZeroSSL 自带检测,目标 A 级以上。最后确认 HTTP 已 301 到 HTTPS 且 HSTS 头存在:curl -sI http://example.com | findstr /i location。
常见问题
免费 DV 证书有效期为 90 天。到期前在 ZeroSSL 控制台重新签发,下载新证书后重新打包 PFX,用 PowerShell 导入并执行 netsh 删除、重建 443 绑定,最后 iisreset /noforce 即可。
典型是证书链不完整:PFX 里只有叶子证书,没有 ZeroSSL 中间证书。本机信任库缓存了中间证书所以浏览器正常,外部客户端没有。用 -certfile ca_bundle.crt 重新打包 PFX 即可解决。
不能。IIS 通过 Windows 证书存储加载证书,必须是含私钥的 PKCS#12(.pfx)。单独导入 .crt 会因缺少私钥而无法用于 443 绑定,只能看到“服务器证书”列表里没有私钥的条目。
会,如果绑定写成 0.0.0.0:443。必须为每个站点启用 SNI:IIS 绑定界面勾选“需要服务器名称指示”,或 netsh 使用 hostnameport=域名:443 逐条添加 SSL 绑定。
多为 OpenSSL 3.x 默认的 AES-256/PBES2 加密不被旧版 Windows 识别。加 -legacy 参数重新导出 PFX,或在 Windows 上用 certutil -importPFX 导入;同时确认密码中没有会被 shell 转义的特殊字符。
不需要停机。导入新 PFX 后,先 netsh http delete sslcert 删除旧绑定,再用新 Thumbprint 执行 add sslcert,最后 iisreset /noforce 回收应用池,全程秒级,已有连接不受影响。