【问题标题】:Set header from URL Rewrite on Azure Websites - AppCmd or applicationhost.config?从 Azure 网站上的 URL 重写设置标头 - AppCmd 或 applicationhost.config?
【发布时间】:2014-02-09 23:21:19
【问题描述】:

我想在 Azure 网站上使用 IIS URL 重写模块从 Web.config 设置请求标头(准确地说是 HTTP_HOST)。基本上我想在我的网站的 Web.config 中有这样的东西:

<system.webServer>
  <rules>
    <clear />
    <rule name="My rule" enabled="true">
      <match url=".*" />
      <serverVariables>
        <set name="HTTP_HOST" value="my value" />
      </serverVariables>
      <action type="None" />
    </rule>

这会导致不允许设置 HTTP_HOST 的错误。这是正常的,对于标准 IIS,下一步是直接或通过 AppCmd 将 HTTP_HOST 添加到 &lt;allowedServerVariables&gt; 元素到 applicationhost.config。但是我找不到任何关于能够以某种方式访问​​此配置的提示。

是否有可能以某种方式修改 apphost 配置,或以其他方式添加允许的服务器变量?

【问题讨论】:

    标签: asp.net azure azure-web-app-service url-rewrite-module


    【解决方案1】:

    Expanding on Joris' answer,你应该使用xdt:Transform="InsertIfMissing"xdt:Locator="Match(name)" 否则它不会像你期望的那样工作(here's an example of it not working as-expectedand another example)。

    所以你的applicationHost.xdt 应该是这样的:

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <system.webServer>
          <rewrite>
            <allowedServerVariables>
              <add name="HTTP_HOST" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" />
            </allowedServerVariables>
          </rewrite>
        </system.webServer>
    </configuration>
    

    【讨论】:

    • 这个 xdt:Locator="Match(name)" 为我工作...谢谢!
    【解决方案2】:

    可以通过应用 xdt 转换来更改 Azure 的 ApplicationHost.config。

    将文件上传到 /site 并重新启动您的站点以使更改生效:

    ApplicationHost.xdt

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <system.webServer>
          <rewrite>
            <allowedServerVariables>
              <add name="HTTP_HOST" xdt:Transform="Insert" />
            </allowedServerVariables>
          </rewrite>
        </system.webServer>
    </configuration>
    

    另见:

    1. https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
    2. http://azure.microsoft.com/nl-nl/documentation/articles/web-sites-transform-extend/

    【讨论】:

    • 谢谢。我认为这是与站点扩展一起提供的。
    • 可能需要小心使用xdt:Transform="Insert",因为它可能会完全破坏您的网络应用程序实例,因为它会在每次评估时尝试在您的applicationgHost.config 中添加重复条目。改用xdt:Transform="InsertIfMissing" 可能更安全
    • 你还需要指定xdt:Locator="Match(name)",否则XDT只匹配&lt;add&gt;元素名,根本没用!
    【解决方案3】:

    此答案已过时(应删除)。

    【讨论】:

    • 谢谢!我最终从 HTTP 模块中交换了标头。
    猜你喜欢
    • 2021-10-04
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多