版本概述

IIS 6.0
已弃用
Windows Server 2003 的 IIS 版本,使用旧的管理界面,不支持 SNI,TLS 协议支持有限。
⚠️ 注意: Windows Server 2003 已停止支持,存在严重安全风险,强烈建议升级。
IIS 7.0 / 7.5
稳定版本
Windows Server 2008 / 2008 R2 的 IIS 版本,引入了新的管理界面和 PowerShell 支持,但仍不支持 SNI。
IIS 8.0 / 8.5
推荐版本
Windows Server 2012 / 2012 R2 的 IIS 版本,首次支持 SNI,允许在同一 IP 地址上绑定多个 SSL 证书。
✅ 重要特性: 从 IIS 8.0 开始支持 SNI(Server Name Indication),这是多站点 SSL 配置的关键特性。
IIS 10.0
当前版本
Windows Server 2016 / 2019 / 2022 的 IIS 版本,支持所有现代 TLS 协议(包括 TLS 1.2 和 TLS 1.3),提供最佳的安全性和性能。
IIS 版本 Windows Server SNI 支持 TLS 1.2 TLS 1.3 PowerShell 支持
IIS 6.0 2003 ⚠️ 需配置
IIS 7.0 2008
IIS 7.5 2008 R2
IIS 8.0 2012
IIS 8.5 2012 R2
IIS 10.0 2016/2019/2022

证书格式转换

IIS 需要使用 .pfx 或 .p12 格式的证书文件。如果您收到的是 .crt 和 .key 文件,需要先转换为 .pfx 格式。

使用 OpenSSL 转换(推荐)

# # 合并证书链(如果需要)cat your_domain.crt intermediate_ca.crt > fullchain.crt # # 转换为 PFX 格式openssl pkcs12 -export -out your_domain.pfx -inkey your_domain.key -in fullchain.crt # # 系统会提示您设置 PFX 密码,请妥善保管此密码

使用 Windows 证书管理器转换

步骤 1:导入证书到证书存储

  1. 双击 .crt 文件,选择"安装证书"
  2. 选择"本地计算机",点击"下一步"
  3. 选择"将所有证书放入以下存储",点击"浏览"
  4. 选择"个人",点击"确定"
  5. 完成导入

步骤 2:导出为 PFX 格式

  1. 打开"运行"(Win+R),输入 certmgr.msc
  2. 展开"个人" → "证书"
  3. 找到您的证书,右键选择"所有任务" → "导出"
  4. 选择"是,导出私钥"
  5. 选择"个人信息交换 - PKCS #12 (.PFX)"
  6. 设置密码并保存文件
⚠️ 注意: 如果私钥文件是单独提供的,您需要先将私钥和证书合并,然后再转换为 PFX 格式。

GUI 方式导入证书

所有 IIS 版本都支持通过图形界面导入证书,但界面略有不同:

IIS 7.0+ 导入方法(推荐)

步骤 1:打开 IIS 管理器

打开"服务器管理器" → "工具" → "Internet Information Services (IIS) 管理器"

步骤 2:导入证书到服务器证书存储

  1. 在左侧连接树中选择服务器名称
  2. 双击"服务器证书"
  3. 在右侧操作面板中点击"导入"
  4. 选择您的 .pfx 文件
  5. 输入 PFX 文件的密码
  6. 选择证书存储位置(建议选择"个人")
  7. 点击"确定"完成导入

IIS 6.0 导入方法

使用证书管理器导入

  1. 打开"运行"(Win+R),输入 certmgr.msc
  2. 展开"个人" → "证书"
  3. 右键选择"所有任务" → "导入"
  4. 选择您的 .pfx 文件并输入密码
  5. 完成导入
💡 提示: 导入证书后,证书会出现在 IIS 管理器的"服务器证书"列表中,您可以在绑定站点时选择该证书。

PowerShell 导入证书

⚠️ 注意: PowerShell 导入方法需要 IIS 7.0 或更高版本,IIS 6.0 不支持 PowerShell。

导入证书到本地计算机存储

# # 导入 PFX 证书到本地计算机的个人存储$pfxPath = "C:\path\to\your_domain.pfx" $password = ConvertTo-SecureString -String "YourPFXPassword" -Force -AsPlainText Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\LocalMachine\My -Password $password # # 验证证书是否导入成功Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {`$_.Subject -like "*yourdomain.com*"}

使用 IIS PowerShell 模块导入

# # 导入 WebAdministration 模块Import-Module WebAdministration # # 导入证书$pfxPath = "C:\path\to\your_domain.pfx" $password = "YourPFXPassword" $cert = Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString -String $password -Force -AsPlainText) # # 验证导入Get-ChildItem IIS:\SslBindings
📝 说明: PowerShell 方式适合批量部署和自动化场景,可以编写脚本实现证书的自动导入和绑定。

站点绑定配置

GUI 方式绑定(所有版本)

步骤 1:打开站点绑定

  1. 在 IIS 管理器中展开"站点"
  2. 选择要配置的网站
  3. 在右侧操作面板中点击"绑定"

步骤 2:添加 HTTPS 绑定

  1. 在"网站绑定"对话框中点击"添加"
  2. 类型选择"https"
  3. IP 地址选择"全部未分配"或特定 IP
  4. 端口设置为"443"
  5. SSL 证书选择您导入的证书
  6. IIS 8.0+:勾选"需要服务器名称指示(SNI)"(如果使用 SNI)
  7. 主机名填写您的域名(IIS 8.0+ 支持)
  8. 点击"确定"完成绑定

PowerShell 方式绑定(IIS 7.0+)

# # 导入 WebAdministration 模块Import-Module WebAdministration # # 获取证书指纹$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {`$_.Subject -like "*yourdomain.com*"} $thumbprint = $cert.Thumbprint # # 绑定证书到站点(不使用 SNI)New-WebBinding -Name "YourSiteName" -Protocol https -Port 443 -SslFlags 0 # # 为绑定分配证书$binding = Get-WebBinding -Name "YourSiteName" -Protocol https $binding.AddSslCertificate($thumbprint, "My") # # IIS 8.0+ 使用 SNI 绑定New-WebBinding -Name "YourSiteName" -Protocol https -Port 443 -HostHeader "yourdomain.com" -SslFlags 1 $binding = Get-WebBinding -Name "YourSiteName" -Protocol https -HostHeader "yourdomain.com" $binding.AddSslCertificate($thumbprint, "My")
💡 说明:
  • SslFlags 0:SslFlags 0:不使用 SNI(IIS 7.0/7.5)
  • SslFlags 1:SslFlags 1:使用 SNI(IIS 8.0+)
  • 使用 SNI 时,每个域名需要单独绑定

SNI (Server Name Indication) 支持

⚠️ 重要: SNI 支持需要 IIS 8.0 或更高版本。IIS 7.0 和 7.5 不支持 SNI,每个 IP 地址只能绑定一个 SSL 证书。

什么是 SNI?

SNI 允许在同一个 IP 地址上绑定多个 SSL 证书,每个证书对应不同的域名。这对于共享主机环境非常有用。

IIS 8.0+ SNI 配置

# # 为多个域名配置 SNIImport-Module WebAdministration # # 站点 1:yourdomain.com$cert1 = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {`$_.Subject -like "*yourdomain.com*"} New-WebBinding -Name "Site1" -Protocol https -Port 443 -HostHeader "yourdomain.com" -SslFlags 1 $binding1 = Get-WebBinding -Name "Site1" -Protocol https -HostHeader "yourdomain.com" $binding1.AddSslCertificate($cert1.Thumbprint, "My") # # 站点 2:anotherdomain.com(同一 IP)$cert2 = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {`$_.Subject -like "*anotherdomain.com*"} New-WebBinding -Name "Site2" -Protocol https -Port 443 -HostHeader "anotherdomain.com" -SslFlags 1 $binding2 = Get-WebBinding -Name "Site2" -Protocol https -HostHeader "anotherdomain.com" $binding2.AddSslCertificate($cert2.Thumbprint, "My")

IIS 7.0/7.5 的限制

📝 说明: 在 IIS 7.0 和 7.5 中,如果不支持 SNI,您需要:
  • 为每个域名使用不同的 IP 地址,或
  • 使用通配符证书(*.example.com),或
  • 使用多域名证书(SAN 证书)

TLS 协议配置

Windows Server 的 TLS 协议配置通过注册表进行,不同版本支持的协议不同:

查看当前 TLS 协议支持

# # 查看已启用的 TLS 协议Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -Name Enabled # # 查看所有 TLS 协议状态Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols" | ForEach-Object { $protocol = `$_.PSChildName $path = `$_.PSPath $enabled = (Get-ItemProperty -Path "`$path\Server" -Name Enabled -ErrorAction SilentlyContinue).Enabled Write-Host "`$protocol : `$enabled" }

启用 TLS 1.2(IIS 7.0+)

# # 启用 TLS 1.2 服务器端New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -Name Enabled -Value 1 -PropertyType DWORD -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -Name DisabledByDefault -Value 0 -PropertyType DWORD -Force # # 启用 TLS 1.2 客户端端New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -Name Enabled -Value 1 -PropertyType DWORD -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -Name DisabledByDefault -Value 0 -PropertyType DWORD -Force # # 禁用不安全的协议(SSL 2.0, SSL 3.0, TLS 1.0, TLS 1.1)$protocols = @("SSL 2.0", "SSL 3.0", "TLS 1.0", "TLS 1.1") foreach ($protocol in $protocols) { New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\`$protocol\Server" -Force | Out-Null New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\`$protocol\Server" -Name Enabled -Value 0 -PropertyType DWORD -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\`$protocol\Server" -Name DisabledByDefault -Value 1 -PropertyType DWORD -Force } # # 重启服务器使配置生效Restart-Computer

启用 TLS 1.3(IIS 10.0 / Windows Server 2019+)

# # Windows Server 2019+ 支持 TLS 1.3# # 启用 TLS 1.3(需要 Windows 更新支持)New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server" -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server" -Name Enabled -Value 1 -PropertyType DWORD -Force New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server" -Name DisabledByDefault -Value 0 -PropertyType DWORD -Force # 重启服务器 Restart-Computer
⚠️ 注意: 修改注册表后需要重启服务器才能使配置生效。建议在维护窗口期间进行此操作。

HTTP 到 HTTPS 重定向

使用 URL 重写模块(IIS 7.0+)

需要安装 IIS URL Rewrite 模块(所有 IIS 版本都支持):

<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" 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>

方法二:使用 HTTP 重定向(IIS 7.0+)

配置步骤

  1. 在 IIS 管理器中选择网站
  2. 双击"HTTP 重定向"
  3. 勾选"将请求重定向到此目标"
  4. 输入:https://yourdomain.com{REQUEST_URI}
  5. 选择"仅将请求重定向到此目录(非子目录)中的内容"(可选)
  6. 状态代码选择"永久(301)"
  7. 点击"应用"
💡 提示: URL 重写模块方式更灵活,可以处理更复杂的重定向场景。

最佳实践

1. 证书管理

  • 定期检查证书有效期,设置提醒在证书过期前续期
  • 妥善保管 PFX 文件密码,建议使用密码管理器
  • 备份证书和私钥文件到安全位置
  • 使用证书存储的"个人"容器,避免使用"Web 托管"容器

2. 安全配置

  • 禁用不安全的 SSL/TLS 协议(SSL 2.0, SSL 3.0, TLS 1.0, TLS 1.1)
  • 仅启用 TLS 1.2 和 TLS 1.3(如果支持)
  • 使用强加密套件,避免弱加密算法
  • 启用 HSTS(HTTP Strict Transport Security)

3. 多站点配置

  • IIS 8.0+ 使用 SNI 实现多站点 SSL 配置
  • IIS 7.0/7.5 考虑使用通配符证书或多域名证书
  • 为每个站点单独绑定证书,避免证书混淆

4. 性能优化

  • 启用 SSL 会话缓存(Windows 自动管理)
  • 使用 HTTP/2(IIS 10.0+ 支持)
  • 定期更新 Windows Server 和 IIS 以获得最新安全补丁

常见问题

1. 检查 IIS 版本

# # 查看 IIS 版本Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\InetStp" | Select-Object MajorVersion, MinorVersion # # 或使用命令行%windir%\system32\inetsrv\appcmd.exe list config /section:system.webServer

2. 验证证书绑定

# # 查看所有 SSL 绑定Get-ChildItem IIS:\SslBindings # # 查看特定站点的绑定Get-WebBinding -Name "YourSiteName" -Protocol https # # 查看证书详细信息Get-ChildItem -Path Cert:\LocalMachine\My | Format-List Subject, Thumbprint, NotAfter

3. 常见错误及解决方案

错误: 证书链中的中间证书丢失
解决: 确保 PFX 文件包含完整的证书链(域名证书 + 中间证书),使用 OpenSSL 合并后再转换为 PFX
错误: 无法找到证书私钥
解决: 确保 PFX 文件包含私钥,检查 PFX 密码是否正确
错误: 此站点无法提供安全连接
解决:
  • 检查证书是否已正确绑定到站点
  • 验证证书是否过期
  • 检查 TLS 协议是否已启用
  • 确认防火墙允许 443 端口
错误: SNI 绑定失败
解决: 确认 IIS 版本为 8.0 或更高,检查主机名是否正确填写

4. 验证 SSL 配置

# # 使用在线工具验证https://www.ssllabs.com/ssltest/ https://myssl.com/ # # 使用 PowerShell 测试连接Test-NetConnection -ComputerName yourdomain.com -Port 443 # # 使用 OpenSSL 测试openssl s_client -connect yourdomain.com:443 -servername yourdomain.com

5. 查看 IIS 日志

# # IIS 日志默认位置C:\inetpub\logs\LogFiles\ # # 使用 PowerShell 查看最近的错误Get-Content "C:\inetpub\logs\LogFiles\W3SVC*\*.log" -Tail 50 | Select-String "error"