正如标题所述,我正在为反向代理功能配置重写规则,其中包括:
为此,我使用Powershell DSC,在脚本资源中设置配置。这对于出站规则非常好,但是入站规则没有被创建,也没有给出错误/警告。当试图在它上设置属性时(在后面的3行),一个警告显示它找不到入站规则,顺便说一下,它还显示了正确使用的变量名)。在中,它也是不可见的(是的,我在它上使用了F5 )。
我使用从IIS本身生成的命令,它等于我在网上看到的所有东西,包括StackOverflow。这个特别的问题不容易找到,所以我一定是错过了一些非常琐碎的东西。我已经试过了
使命令正常工作的唯一方法是在(提升的) 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"
}
}我希望有人知道为什么会发生这种事,因为我对解决这个问题感到非常沮丧。
发布于 2017-07-26 08:12:09
好的,在尝试了更多之后(比如使用更新的xScript资源而不是脚本,并尝试将1条失败的命令放在调用命令脚本块中),我找到了解决方案。
解决方案: Powershell中的,或者至少对于IIS,有两种方法可以将位置传递给命令。众所周知的是-PSPath,但也有-Location。当执行命令solo (以及其他命令)时,只需将-PSPath "IIS:\Sites\SITENAME"传递到命令。我现在如何让它在我的脚本中用于该命令:-PSPath "IIS:\Sites" -Location "SITENAME"。
事实上,我需要使用这个解决方法,这在我看来是Powershell到IIS转换中的一个bug (或者只是在重写模块中)。如果我错了,请纠正我!无论如何,我的问题已经解决了,我希望这个答案将来能帮助别人解决这个问题。感谢那些看了我的问题并给出一些想法的人:)
https://stackoverflow.com/questions/45284426
复制相似问题