正如标题所说,我正在为反向代理功能配置重写规则:
为此,我使用 Powershell DSC,在脚本资源中设置配置。这对于出站规则非常有效,但不会创建入站规则,也不会给出错误/警告。
尝试在其上设置属性时(在之后的 3 行上),确实会显示一条警告,说它找不到入站规则,顺便说一句,它还显示了正确使用的变量名称)。在 IIS 管理控制台中,它也是不可见的(是的,我在上面使用了 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"
}
}
我希望有人知道为什么会这样,因为我在试图解决这个问题时感到非常沮丧。
最佳答案
好吧,在尝试了更多之后(比如使用更新的 xScript 资源而不是 Script 并尝试将 1 个失败的命令放入 Invoke-Command Scriptblock 中),我找到了解决方案。
解决方案:在 Powershell 中,或者至少对于 IIS 配置,有两种方法可以将位置传递给命令。众所周知的是-PSPath,但也有-Location。
当单独执行命令(以及与其他命令一起)时,简单有效的是 passint -PSPath "IIS:\Sites\SITENAME"
到命令。
我现在如何让它在我的脚本中为该命令工作是:-PSPath "IIS:\Sites" -Location "SITENAME"
.
在我看来,我需要使用此解决方法这一事实是 Powershell 到 IIS 转换(或仅在重写模块中)中的一个错误。如果我错了,请纠正我!
无论如何,我的问题已经解决,我希望这个答案可以帮助其他人在 future 解决这个问题。
感谢那些看着我的问题并给出一些想法的人:)
关于powershell - 在 DSC 脚本中添加入站重写规则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45284426/