正如標題所說,我配置重寫規則的反向代理功能有:在DSC腳本添加入站重寫規則不起作用
- IIS 8.5
- 的Windows Server 2012R2
- URL重寫2.1
- 應用程序請求路由3.0
- Powershell的5.1
爲此,我使用PowerShell DSC,在腳本資源中設置配置。這對於出站規則來說非常合適,但入站規則不會被創建,也不會給出錯誤/警告。 當試圖設置它的屬性(後面的3行)時,警告會顯示說它找不到入站規則,btw也顯示正確使用的變量名稱)。在IIS管理控制檯中,它也不可見(是的,我已經使用了F5)。
我使用IIS自身生成的命令,這與我在線看到的所有內容相同,包括StackOverflow。這個特殊的問題不容易找到,所以我必須錯過一些非常微不足道的東西。 我已經嘗試過:
- 使用變量
- 使用URL的硬編碼的名稱,而不是重寫2.0
- 重新啓動IIS
- 重新啓動服務器腳本運行在不同的憑據
我得到的命令工作的唯一方法是,如果它作爲(升高)Powershell中的單個命令執行(由>>>>標記如下)。
然後腳本本身。 (僅供參考,一個出站規則太加):
SetScript = {
Write-Verbose "Checking if inbound rewrite rule is present..."
$inbound = (Get-WebConfiguration -Filter "//System.webServer/rewrite/rules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:inboundRuleName"}
if($inbound -eq $null) {
Write-Verbose "Inbound rewrite rule not present, configuring..."
>>>> Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules" -Name "." -Value @{name=$using:inboundRuleName;stopProcessing="True"} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/match" -Name "url" -Value "^api/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "url" -Value "http://localhost:8000/api/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"
Write-Verbose "Inbound rewrite rule configured"
}
Write-Verbose "Checking if outbound HTML rule is present..."
$outboundHTML = (Get-WebConfiguration -Filter "//System.webServer/rewrite/outboundRules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:outboundHTMLRuleName"}
if($outboundHTML -eq $null) {
Write-Verbose "Outbound HTML rule not present, configuring..."
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions" -Name "." -Value @{name='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions/preCondition[@name='IsHTML']" -Name "." -Value @{input='{RESPONSE_CONTENT_TYPE}';pattern='^text/html'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules" -Name "." -Value @{name=$using:outboundHTMLRuleName;preCondition='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "filterByTags" -Value "A" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "pattern" -Value "^/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"
Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/conditions" -Name "." -Value @{input='{URL}';pattern='^/api/.*'} -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "value" -Value "/{C:1}/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"
Write-Verbose "Outbound HTML rewrite rule configured"
}
}
我希望有人知道爲什麼會這樣,當我越來越漂亮沮喪試圖解決這個問題。