首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在DSC脚本中添加入站重写规则不起作用

在DSC脚本中添加入站重写规则不起作用
EN

Stack Overflow用户
提问于 2017-07-24 15:24:21
回答 1查看 1.3K关注 0票数 4

正如标题所述,我正在为反向代理功能配置重写规则,其中包括:

  • IIS 8.5
  • Windows 2012R2
  • URL重写2.1
  • 应用请求路由3.0
  • Powershell 5.1

为此,我使用Powershell DSC,在脚本资源中设置配置。这对于出站规则非常好,但是入站规则没有被创建,也没有给出错误/警告。当试图在它上设置属性时(在后面的3行),一个警告显示它找不到入站规则,顺便说一下,它还显示了正确使用的变量名)。在中,它也是不可见的(是的,我在它上使用了F5 )。

我使用从IIS本身生成的命令,它等于我在网上看到的所有东西,包括StackOverflow。这个特别的问题不容易找到,所以我一定是错过了一些非常琐碎的东西。我已经试过了

  • 在不同凭据下运行脚本
  • 使用硬编码的名称而不是变量
  • 使用URL重写2.0
  • 重新启动IIS
  • 重新启动服务器

使命令正常工作的唯一方法是在(提升的) Powershell中将其作为单个命令执行(下面用>>>>标记)。

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

代码语言:javascript
复制
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"
            }
}

我希望有人知道为什么会发生这种事,因为我对解决这个问题感到非常沮丧。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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 (或者只是在重写模块中)。如果我错了,请纠正我!无论如何,我的问题已经解决了,我希望这个答案将来能帮助别人解决这个问题。感谢那些看了我的问题并给出一些想法的人:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45284426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档