详细说明 IIS 不同版本之间的 SSL 证书安装配置差异,帮助您正确配置 SSL 证书
| 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 格式。
# # 合并证书链(如果需要)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 密码,请妥善保管此密码
所有 IIS 版本都支持通过图形界面导入证书,但界面略有不同:
打开"服务器管理器" → "工具" → "Internet Information Services (IIS) 管理器"
# # 导入 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*"}
# # 导入 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
# # 导入 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 允许在同一个 IP 地址上绑定多个 SSL 证书,每个证书对应不同的域名。这对于共享主机环境非常有用。
# # 为多个域名配置 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")
Windows Server 的 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 服务器端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
# # 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
需要安装 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>
# # 查看 IIS 版本Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\InetStp" | Select-Object MajorVersion, MinorVersion
# # 或使用命令行%windir%\system32\inetsrv\appcmd.exe list config /section:system.webServer
# # 查看所有 SSL 绑定Get-ChildItem IIS:\SslBindings
# # 查看特定站点的绑定Get-WebBinding -Name "YourSiteName" -Protocol https
# # 查看证书详细信息Get-ChildItem -Path Cert:\LocalMachine\My | Format-List Subject, Thumbprint, NotAfter
证书链中的中间证书丢失无法找到证书私钥此站点无法提供安全连接SNI 绑定失败# # 使用在线工具验证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
# # IIS 日志默认位置C:\inetpub\logs\LogFiles\
# # 使用 PowerShell 查看最近的错误Get-Content "C:\inetpub\logs\LogFiles\W3SVC*\*.log" -Tail 50 | Select-String "error"