Microsoft IIS(Internet Information Services)使用 PFX 格式的 SSL 证书——不同于 GetHTTPS 和大多数 Linux 工具生成的 PEM 文件。本指南涵盖转换证书并在 IIS 10 上安装的全过程。
前提条件
- Windows Server 已安装 IIS 10
- 来自 GetHTTPS 的证书文件:
cert.pem、privkey.pem、chain.pem - OpenSSL 已安装(Git for Windows 自带,或单独安装)
第一步:将 PEM 转换为 PFX
IIS 无法直接读取 PEM 文件。使用 OpenSSL 转换为 PFX 格式:
openssl pkcs12 -export -out certificate.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem
系统会提示你设置导出密码——在导入步骤中需要用到。
没有 OpenSSL? 如果你安装了 Git for Windows,OpenSSL 位于
C:\Program Files\Git\usr\bin\openssl.exe。或者从 slproweb.com/products/Win32OpenSSL.html 下载。
更多证书格式信息:PEM、PFX、DER 详解
第二步:将 PFX 导入 IIS
- 打开 IIS Manager(运行
inetmgr) - 在左侧面板点击服务器名称
- 在中间面板双击 Server Certificates
- 在右侧操作面板点击 Import…
- 浏览到你的
certificate.pfx文件 - 输入你在第一步设置的导出密码
- 选择证书存储:Web Hosting(或 Personal)
- 点击 OK
证书现在出现在 Server Certificates 列表中。
第三步:将 HTTPS 绑定到站点
- 在 IIS Manager 中,展开左侧面板的 Sites
- 右键你的网站 → Edit Bindings…
- 点击 Add…
- 设置:
- Type: https
- Port: 443
- Host name:
yourdomain.com(留空表示所有主机名) - SSL certificate: 选择你刚导入的证书
- 点击 OK
- 如需要,对
www.yourdomain.com重复操作
第四步:将 HTTP 重定向到 HTTPS
安装 URL Rewrite 模块(如果尚未安装),然后在站点的 web.config 中添加:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP 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>
</configuration>
第五步:验证
在浏览器中打开 https://yourdomain.com。点击锁头图标验证证书详情。
PowerShell 验证:
# Check the certificate bound to port 443
netsh http show sslcert
故障排查
”A specified logon session does not exist”
私钥未正确导入。重新导出 PFX 文件:
openssl pkcs12 -export -out certificate.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem -passout pass:YourPassword
确保 PFX 是从匹配的密钥 + 证书文件创建的。
证书在绑定下拉菜单中不显示
导入可能静默失败了。检查 Event Viewer → Windows Logs → Application 中的错误。确保 PFX 密码正确且文件未损坏。
绑定后显示”此站点不安全”
检查证书链是否完整。PFX 转换时必须包含 chain.pem(-certfile chain.pem)。没有它,浏览器无法验证信任链。
常见问题
可以在 IIS 上使用 Let’s Encrypt 证书吗?
可以。Let’s Encrypt 证书适用于任何服务器,包括 IIS。唯一额外的步骤是将 PEM 转换为 PFX 格式。GetHTTPS 提供 PEM 文件;上面的 OpenSSL 命令可以转换它们。
有类似 Certbot 的 IIS 工具吗?
win-acme(原名 letsencrypt-win-simple)是 Windows/IIS 上最流行的 ACME 客户端。它一站式处理证书签发、PFX 转换、IIS 绑定和自动续签。如果你想要全自动的 Let’s Encrypt on IIS,可以使用它。
如何在 IIS 上续签?
- 从 GetHTTPS 获取新证书
- 转换为 PFX:
openssl pkcs12 -export -out new.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem - 在 IIS Manager → Server Certificates 中导入新 PFX
- 更新站点绑定使用新证书
- 从 Server Certificates 中移除旧证书
要自动续签,考虑 win-acme——它自动处理整个周期。
IIS 支持 ECDSA/ECC 证书吗?
Windows Server 2016+ 上的 IIS 10 支持 ECDSA 证书。GetHTTPS 默认生成 ECDSA P-256,适用于现代 IIS。较旧的 IIS 版本(8.5 及以下)可能需要 RSA 证书。
IIS 能在同一 IP 上使用多个站点吗?
可以。IIS 8+ 支持 SNI(Server Name Indication),允许在同一 IP 地址上使用多个 SSL 证书。添加 HTTPS 绑定时,勾选”Require Server Name Indication”并输入主机名。每个站点可以有自己的证书。
如何在 Windows 上找到 OpenSSL?
Windows 默认不包含 OpenSSL。常见来源:
- Git for Windows 包含它,位于
C:\Program Files\Git\usr\bin\openssl.exe - Chocolatey:
choco install openssl - Win32/Win64 构建: 从 slproweb.com 下载
安装后,将其添加到 PATH 或在命令中使用完整路径。