2017-07-24 35 views
3

正如標題所說,我配置重寫規則的反向代理功能有:在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" 
      } 
} 

我希望有人知道爲什麼會這樣,當我越來越漂亮沮喪試圖解決這個問題。

回答

0

好吧,嘗試了更多(如使用更新的xScript資源,而不是腳本,並試圖將1失敗的命令放入Invoke-Command Scriptblock)後,我找到了解決方案。

解決方案:在Powershell中,或者至少對於IIS配置,有兩種方法將位置傳遞給命令。衆所周知的是-PSPath,但也有 - 位置。 當執行獨奏命令(和其他命令)時,簡單地起作用的是passint -PSPath "IIS:\Sites\SITENAME"到命令。 我如何在腳本中使用該命令:-PSPath "IIS:\Sites" -Location "SITENAME"

事實上,我需要使用這種解決方法在我看來,這是Powershell到IIS轉換(或僅在重寫模塊)中的一個錯誤。如果我錯了,請糾正我! 無論如何,我的問題已解決,我希望這個答案將來可以幫助其他人解決這個問題。 謝謝看到我的問題的人,並給了它一些想法:)