IIS 环境配置 SSL 证书完整教程
作者
2026-07-12
发布日期
约 3 分钟
阅读时间
IIS 推荐使用包含证书、私钥和中间证书链的 .pfx 文件。本文讲解从证书准备到 HTTPS 配置的完整流程。
准备证书
如果只有 PEM 格式证书,可转换为 PFX:
openssl pkcs12 -export ^
-out certificate.pfx ^
-inkey privkey.pem ^
-in server.crt ^
-certfile chain.pem ^
-name example.com
执行时设置一个 PFX 密码。
导入证书
- 打开”IIS 管理器”
- 选择左侧服务器节点
- 双击”服务器证书”
- 点击右侧”导入”
- 选择 certificate.pfx
- 输入 PFX 密码并完成导入
也可以通过管理员 PowerShell 导入:
$password = Read-Host "PFX password" -AsSecureString
Import-PfxCertificate `
-FilePath "C:\certs\certificate.pfx" `
-CertStoreLocation "Cert:\LocalMachine\My" `
-Password $password
添加 HTTPS 绑定
- 在 IIS 中选择目标网站
- 点击”绑定”
- 点击”添加”
- 类型选择 https
- 端口填写 443
- 选择刚导入的证书
如果一个 IP 上运行多个 HTTPS 网站:
- •填写正确的主机名,例如 example.com
- •勾选”需要服务器名称指示(SNI)”
HTTP 跳转 HTTPS
安装 IIS URL Rewrite 模块,在网站根目录的 web.config 中添加:
<?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>
</system.webServer>
</configuration>
验证
Test-NetConnection example.com -Port 443
curl.exe -vI https://example.com
如果 IIS 可以看到证书,但绑定时无法选择,通常表示导入的证书没有私钥。续期后还需要把网站绑定切换到新证书,仅导入新 PFX 不会自动替换绑定。
—
本文到此结束
—