跳到主要内容
IIS

DigiCert SSL 证书 IIS 部署实战:PFX 导入、SNI 绑定、TLS 1.2 与证书链排错

问题背景与根因:IIS 部署 DigiCert 证书为什么容易踩坑

IIS 不直接读取 Nginx/Apache 常用的 PEM 证书,而是通过 Windows 证书存储导入 PFX(PKCS#12)。一个可用的 PFX 必须同时包含私钥、叶子证书和中间证书链。DigiCert 签发后通常提供 example.com.crt、DigiCertCA.crt 以及你自行生成的私钥;如果只把叶子证书导入 IIS,本机浏览器可能因为缓存或 AIA 补链显示正常,但 curl、Java、微信小程序、Android 旧版本会报「unable to get local issuer certificate」。这是 IIS 部署 DigiCert 证书最常见的根因。

第二个根因是 Schannel 协议版本。Windows Server 2012 R2/2016 默认未启用 TLS 1.2 的某些密码套件,TLS 1.3 仅在 Windows Server 2022/Windows 11 原生支持。若注册表未显式启用 TLS 1.2,握手可能直接失败。第三个根因是 443 多站点绑定:未勾选 SNI 时,同一 IP:443 只能绑定一个证书,后绑定站点会覆盖前一个。第四是私钥 ACL:IIS 应用池身份没有读取私钥权限,会报 Keyset does not exist。第五是续期后只替换了文件,没有更新 IIS 绑定指纹。

有效期也要注意:按 CA/Browser Forum 规定,DigiCert 等公共 TLS 证书最长有效期为 398 天;Let’s Encrypt 免费证书为 90 天。不要把「一年证书」写进流程。快速自查可执行 netsh http show sslcert 查看绑定指纹,执行 certutil -store my 查看证书是否含私钥,再用 openssl s_client -connect example.com:443 -servername example.com -showcerts 检查链是否完整。

准备证书文件:从 DigiCert 下载包到可导入的 PFX

DigiCert CertCentral 下载的压缩包通常包含服务器证书和中间证书。若你自行生成 CSR,还会持有私钥。先在 Linux/macOS 或 Git Bash 中合并链并转换 PFX:

cat example.com.crt DigiCertCA.crt > fullchain.pem
openssl pkcs12 -export -out example.com.pfx -inkey example.com.key -in example.com.crt -certfile DigiCertCA.crt -passout pass:'Str0ngPass!'

参数说明:-inkey 指定私钥,-in 指定叶子证书,-certfile 指定中间证书,-passout 设置 PFX 导入密码。不要只写 -in fullchain.pem 而漏掉 -certfile,否则部分 OpenSSL 版本不会把链写入 PFX。转换前先验证私钥匹配:

openssl x509 -noout -modulus -in example.com.crt | openssl md5
openssl rsa -noout -modulus -in example.com.key | openssl md5

两条命令输出的 MD5 必须完全一致,否则说明证书与私钥不匹配。再验证证书链:openssl verify -CAfile DigiCertGlobalRootG2.crt -untrusted DigiCertCA.crt example.com.crt,返回 OK 才继续。查看 PFX 内容:openssl pkcs12 -info -in example.com.pfx -noout。在 Windows 上可用 PowerShell 导入:

$pwd = ConvertTo-SecureString -String 'Str0ngPass!' -AsPlainText -Force
Import-PfxCertificate -FilePath C:/certs/example.com.pfx -CertStoreLocation Cert:\LocalMachine\My -Password $pwd

导入后执行 certutil -store my example.com,确认输出包含「证书哈希」和「私钥」信息。PFX 密码建议使用字母数字组合,避免在脚本中硬编码;生产环境可用 Windows 凭据管理器或 Azure Key Vault 托管。

IIS 导入与 SNI 绑定:GUI 与 PowerShell 两条路径

GUI 路径:打开 IIS 管理器 → 服务器节点 → 「服务器证书」→ 右侧「导入」→ 选择 PFX、输入密码 → 确定。然后选中站点 → 「绑定」→ 「添加」→ 类型 https、端口 443、主机名填 example.com、SSL 证书选择刚导入的证书。若同一 IP 需要绑定多个域名,必须勾选「需要服务器名称指示」(SNI)。

PowerShell 可复现:

$pfx = 'C:/certs/example.com.pfx'
$pwd = ConvertTo-SecureString -String 'Str0ngPass!' -AsPlainText -Force
$cert = Import-PfxCertificate -FilePath $pfx -CertStoreLocation Cert:\LocalMachine\My -Password $pwd
$thumb = $cert.Thumbprint
New-WebBinding -Name 'example.com' -Protocol https -Port 443 -HostHeader 'example.com' -SslFlags 1
$binding = Get-WebBinding -Name 'example.com' -Protocol https
$binding.AddSslCertificate($thumb, 'My')

SslFlags 1 表示启用 SNI。若站点已存在绑定,先 Remove-WebBinding -Name 'example.com' -Protocol https -Port 443 再重建。核对绑定:netsh http show sslcert hostnameport=example.com:443,重点看 Certificate Hash 是否等于 $thumb、Application ID 是否为 IIS 的 {4dc3e181-e14b-4a21-b022-59fc669b0914}。手工添加可用:

netsh http add sslcert hostnameport=example.com:443 certhash=$thumb appid={4dc3e181-e14b-4a21-b022-59fc669b0914} certstorename=MY

若报 Keyset does not exist,在 certlm.msc 中找到证书 → 所有任务 → 管理私钥 → 添加 IIS_IUSRS 读取权限;也可用 winhttpcertcfg -g -c LOCAL_MACHINE\MY -s 'example.com' -a 'IIS_IUSRS'。最后重启应用池:Restart-WebAppPool -Name 'example.com'。

启用 TLS 1.2/1.3 与 HTTP 强制跳转

Windows Server 2012 R2/2016 需要显式启用 TLS 1.2。以管理员身份执行:

reg add 'HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server' /v Enabled /t REG_DWORD /d 1 /f
reg add 'HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server' /v DisabledByDefault /t REG_DWORD /d 0 /f
reg add 'HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client' /v Enabled /t REG_DWORD /d 1 /f
reg add 'HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client' /v DisabledByDefault /t REG_DWORD /d 0 /f

禁用 TLS 1.0/1.1 时,把对应路径的 Enabled 设为 0、DisabledByDefault 设为 1。TLS 1.3 仅在 Windows Server 2022/Windows 11 上支持,路径为 ...\Protocols\TLS 1.3\Server,同样设置 Enabled=1、DisabledByDefault=0。修改注册表后必须重启服务器或至少重启 HTTP 服务。密码套件可用 Get-TlsCipherSuite | Format-Table Name 查看,禁用 3DES:Disable-TlsCipherSuite -Name 'TLS_RSA_WITH_3DES_EDE_CBC_SHA'。

HTTP 跳转需要安装 IIS URL Rewrite 模块,然后在站点 web.config 加入规则。注意:只有 HTTPS 验证通过后再加 HSTS,避免锁死。web.config 片段:

<system.webServer>
  <rewrite>
    <rules>
      <rule name='Redirect to HTTPS' stopProcessing='true'>
        <match url='(.*)' />
        <conditions>
          <add input='{HTTPS}' pattern='off' ignoreCase='true' />
        </conditions>
        <action type='Redirect' url='https://{HTTP_HOST}/{R:1}' redirectType='Permanent' />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

HSTS 响应头可放在 <httpProtocol><customHeaders> 中,值建议先设 max-age=300 测试,再提升到 31536000。

配置示例:web.config 与一键部署脚本

下面是一份可直接放入站点根目录的 web.config,包含 HTTP→HTTPS 301、HSTS 和基础安全头。请把 example.com 替换为你的域名:

<?xml version='1.0' encoding='UTF-8'?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name='Redirect to HTTPS' stopProcessing='true'>
          <match url='(.*)' />
          <conditions>
            <add input='{HTTPS}' pattern='off' ignoreCase='true' />
          </conditions>
          <action type='Redirect' url='https://{HTTP_HOST}/{R:1}' redirectType='Permanent' />
        </rule>
      </rules>
    </rewrite>
    <httpProtocol>
      <customHeaders>
        <add name='Strict-Transport-Security' value='max-age=31536000; includeSubDomains' />
        <add name='X-Content-Type-Options' value='nosniff' />
        <add name='X-Frame-Options' value='SAMEORIGIN' />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

一键部署脚本(管理员 PowerShell):

$site = 'example.com'
$pfx = 'C:/certs/example.com.pfx'
$pwd = ConvertTo-SecureString -String 'Str0ngPass!' -AsPlainText -Force
$cert = Import-PfxCertificate -FilePath $pfx -CertStoreLocation Cert:\LocalMachine\My -Password $pwd
if (-not (Get-WebBinding -Name $site -Protocol https -Port 443 -ErrorAction SilentlyContinue)) {
  New-WebBinding -Name $site -Protocol https -Port 443 -HostHeader $site -SslFlags 1
}
$binding = Get-WebBinding -Name $site -Protocol https
$binding.AddSslCertificate($cert.Thumbprint, 'My')
netsh http show sslcert hostnameport=$site:443

执行后检查输出中的 Certificate Hash 与 $cert.Thumbprint 一致。若站点名称不是域名,把 $site 改成 IIS 站点名,HostHeader 单独填域名。生产环境不要把密码明文写入脚本,可改用 Get-Content C:/certs/pfx.pwd | ConvertTo-SecureString 读取受 ACL 保护的文件。

对比表格:证书格式、部署方式与 CA 选择

先看文件格式。IIS 只认含私钥的 PFX;PEM 适合 Nginx/Apache;CER/DER 只有公钥证书,不能单独导入 IIS 完成 HTTPS。P7B 只有证书链,没有私钥,必须与私钥合并成 PFX。

格式 包含内容 IIS 支持 适用场景
PFX/PKCS12 私钥+叶子证书+中间链 直接导入 IIS 推荐
PEM Base64 证书/链/私钥 需转换 Nginx、Apache、OpenSSL
CER/DER 仅公钥证书 不能单独用 查看/导入受信任根
P7B 证书链,无私钥 需合并 Windows 证书链分发

部署方式对比:

方式 入口/命令 优点 风险
IIS 管理器 GUI 服务器证书→导入→绑定 直观,适合单站点 易漏 SNI、易选错证书
PowerShell Import-PfxCertificate + New-WebBinding 可脚本化、可审计 需处理私钥 ACL
netsh http netsh http add sslcert 精确控制 SNI/指纹 参数复杂,易写错 appid

CA 选择对比(价格为市场参考,以官网为准):

CA 类型 最长有效期 特点
DigiCert 商业 OV/EV 398 天 CertCentral 支持 API/批量管理,EV 审核体验好,银行/证券/大型电商常用,价格较高
Let’s Encrypt 免费 DV 90 天 ACME 自动化,适合个人与 DevOps
Sectigo/GlobalSign 商业 DV/OV/EV 398 天 价格居中,渠道多

结论:IIS + DigiCert 生产环境优先 PFX + PowerShell 脚本 + SNI;测试环境可用 Let’s Encrypt 90 天证书,但要把续期周期设为 60 天以内。

常见错误排查:真实报错与修复命令

报错 1:curl: (60) SSL certificate problem: unable to get local issuer certificate。说明服务器没有发送中间证书。执行 openssl s_client -connect example.com:443 -servername example.com -showcerts,若只看到 1 张证书,重新用 -certfile DigiCertCA.crt 导出 PFX,删除旧绑定后重新导入。不要只导入叶子 .crt。

报错 2:Keyset does not exist(0x80090016)。IIS 进程无权读取私钥。打开 certlm.msc → 个人 → 证书 → 右键 → 所有任务 → 管理私钥 → 添加 IIS_IUSRS 读取;或执行 winhttpcertcfg -g -c LOCAL_MACHINE\MY -s 'example.com' -a 'IIS_IUSRS',然后 iisreset。

报错 3:Cannot find the certificate and private key for decryption。PFX 导入时未包含私钥,或密码错误。用 openssl pkcs12 -info -in example.com.pfx -noout 确认有 Private Key;重新导出时不要漏掉 -inkey。

报错 4:HTTP Error 500.19 – Internal Server Error,错误代码 0x8007000d。web.config 中使用了 URL Rewrite 规则但服务器未安装 IIS URL Rewrite 模块,或 XML 属性引号不合法。安装模块后执行 %windir%\system32\inetsrv\appcmd.exe list config 检查配置。

报错 5:netsh http show sslcert 显示 Certificate Hash 全 0。绑定损坏。先 netsh http delete sslcert hostnameport=example.com:443,再重新 netsh http add sslcert ...。若报 0x800700B7,说明 443 已有冲突绑定,用 netstat -ano | findstr :443 找 PID。

报错 6:SCHANNEL 事件 36888,致命警报 40/70。握手协议或密码套件不匹配。检查 TLS 1.2 注册表、密码套件顺序和证书链;用 openssl s_client -tls1_2 -connect example.com:443 复现。若 Java 客户端报 PKIX path building failed,仍是缺中间证书。

效果验证:浏览器、OpenSSL 与 SSL Labs 三重检查

第一步,浏览器访问 https://example.com,点击锁图标查看证书颁发者为 DigiCert,链中应包含中间证书。注意本机可能缓存或从 AIA 下载中间证书,必须用另一台干净机器或在线工具复验。第二步,命令行检查链和有效期:

openssl s_client -connect example.com:443 -servername example.com -showcerts
openssl s_client -connect example.com:443 -servername example.com -CAfile /etc/ssl/certs/ca-certificates.crt -verify_return_error
openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates -subject -issuer

第一条输出中 Verify return code 应为 0,证书链通常为 2 张(叶子+中间)或 3 张(含根)。第二条若报 verify error,说明链不完整或根不受信任。第三条查看 notBefore/notAfter,确认有效期不超过 398 天且未临近到期。

第三步,检查 HTTP 跳转与 HSTS:curl -I http://example.com 应返回 301 Location: https://example.com/;curl -I https://example.com 应包含 Strict-Transport-Security。第四步,检查协议与套件:nmap --script ssl-enum-ciphers -p 443 example.com,确认 TLS 1.0/1.1 已禁用,TLS 1.2/1.3 正常。也可执行 openssl s_client -tls1_1 -connect example.com:443,预期握手失败。

第五步,Windows 侧验证:Test-NetConnection example.com -Port 443 应 TcpTestSucceeded=True;Invoke-WebRequest https://example.com -UseBasicParsing 应 StatusCode=200。最后把域名提交 SSL Labs,目标 A 或 A+;若只有 B,通常是未启用 HSTS、支持 TLS 1.0/1.1 或证书链不完整。

常见问题

按 CA/Browser Forum 规定,DigiCert 等公共 TLS 证书最长 398 天;Let’s Encrypt 为 90 天。建议到期前 30 天续期,并核对 IIS 绑定指纹,避免只换文件不换绑定。

是。IIS 导入需要含私钥的 PFX/PKCS12;PEM、CER、P7B 不能直接完成 HTTPS 绑定。可用 openssl pkcs12 -export 将私钥、叶子证书和 DigiCertCA.crt 合并成 PFX 后再导入。

通常是 PFX 未包含中间证书。用 openssl s_client -showcerts 检查链数量,重新执行 openssl pkcs12 -export 并加 -certfile DigiCertCA.crt,删除旧绑定后重新导入,再用另一台机器复验。

启用 SNI:IIS 绑定中勾选「需要服务器名称指示」,PowerShell 使用 SslFlags 1,主机名填各自域名。每个域名使用独立证书,并用 netsh http show sslcert hostnameport=域名:443 核对指纹。

先确认新 PFX 已导入且绑定指纹已更新,再重启应用池或执行 iisreset;若前面有 CDN/负载均衡,还要在 CDN 上传新证书并刷新缓存。用 openssl s_client 查看 notAfter 确认。

内容审核

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