代码之家  ›  专栏  ›  技术社区  ›  Devvox93

在DSC脚本中添加入站重写规则无效

  •  4
  • Devvox93  · 技术社区  · 8 年前

    正如标题所述,我正在使用以下配置反向代理功能的重写规则:

    • IIS 8.5
    • URL重写2.1
    • Powershell 5.1

    当尝试在其上设置属性时(在后面的3行上),会显示一条警告,表示无法找到入站规则,顺便说一句,还会显示正确使用的变量名)。在IIS管理控制台中,它也不可见(是的,我在上面使用了F5)。

    我已经试过了:

    • 在不同凭据下运行脚本
    • 使用URL重写2.0
    • 重新启动IIS
    • 重新启动服务器

    然后是脚本本身。(为了便于参考,还添加了一条出站规则):

    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"
                }
    }
    

    我希望有人知道为什么会这样,因为我在试图解决这个问题时感到非常沮丧。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Devvox93    8 年前

    解决方案: 当单独执行命令(以及与其他命令一起执行)时,简单有效的是passint -PSPath "IIS:\Sites\SITENAME" 现在,我是如何在脚本中使用该命令的: -PSPath "IIS:\Sites" -Location "SITENAME" .

    无论如何,我的问题已经解决了,我希望这个答案能帮助其他人解决这个问题。