【问题标题】:Where should <allowedServerVariables> tag live in Azure Website applicationHost.config?<allowedServerVariables> 标记应位于 Azure 网站 applicationHost.config 中的什么位置?
【发布时间】:2016-06-08 15:27:29
【问题描述】:

技术信息

场景

我已经在我的Azure Website 上实现了一个反向代理,但是接收服务器没有得到任何关于初始请求是否超过HTTPS 的指示。

我想要做的是通过自定义 HTTP HeaderON/OFFHTTPS 标志从初始请求发送到代理服务器。

理论上

  • 使用shibayanIIS Manager Site Extension,我可以编辑applicationHost.xdt文件,给它一个转换来插入一个&lt;allowedServerVariables&gt;标签,这应该允许我设置一个自定义HTTP Header

实践中

我已经这样配置了我的重写规则:

<rule name="Proxy" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">
  ...
  <serverVariables>
    <set name="HTTP_X_USE_HTTPS" value="{HTTPS}" />
  </serverVariables>
  ...
</rule>

并尝试了几种将&lt;serverVariables&gt;标签放在哪里的组合...

尝试一:

this answer 中所述。

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <proxy enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" xdt:Transform="Insert" />
    <rewrite>
      <allowedServerVariables>
        <add name="HTTP_X_USE_HTTPS" xdt:Transform="Insert" />
      </allowedServerVariables>
    </rewrite>
  </system.webServer>
</configuration>

结果:

HTTP 错误 500.50 - URL 重写模块错误。

不允许设置服务器变量“HTTP_X_USE_HTTPS”。添加 将服务器变量名添加到允许的服务器变量列表中。

尝试二:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="~1[app service name]" overrideMode="Allow">
    <system.webServer>
      <proxy enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" xdt:Transform="Insert" />
      <rewrite>
        <allowedServerVariables>
          <add name="HTTP_X_USE_HTTPS" xdt:Transform="Insert" />
        </allowedServerVariables>
      </rewrite>
    </system.webServer>
  </location>
</configuration>

结果:HTTP 500.50

尝试三:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="" overrideMode="Allow">
    <system.webServer>
      <proxy enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" xdt:Transform="Insert" />
      <rewrite>
        <allowedServerVariables>
          <add name="HTTP_X_USE_HTTPS" xdt:Transform="Insert" />
        </allowedServerVariables>
      </rewrite>
    </system.webServer>
  </location>
</configuration>

结果:HTTP 503

尝试四:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="[app service name]" overrideMode="Allow">
    <system.webServer>
      <proxy enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" xdt:Transform="Insert" />
      <rewrite>
        <allowedServerVariables>
          <add name="HTTP_X_USE_HTTPS" xdt:Transform="Insert" />
        </allowedServerVariables>
      </rewrite>
    </system.webServer>
  </location>
</configuration>

结果:HTTP 503

我知道在Azure WebsiteapplicationHost.config 文件中,有几个地方可以定义&lt;system.webServer&gt;,例如在以下元素下:

  • &lt;configuration&gt;
  • &lt;configuration&gt;&lt;location&gt;

...但是我尝试了这些组合无济于事。

问题

  • 还有其他可能的位置吗?
  • 我是否以任何方式错误配置了我的 .xdt 文件?
  • 我的applicationHost.config 是否遗漏了什么?

【问题讨论】:

    标签: azure iis url-rewriting reverse-proxy


    【解决方案1】:

    您必须在站点文件夹d:\home\site\applicationHost.xdt 下创建一个applicationHost.xdt 文件,其中包含以下内容:

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

    现在您可以在 web.config 文件中使用新变量

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>           
                <rules>
                    <rule name="Proxy">
                        <serverVariables>
                            <set name="HTTP_X_USE_HTTPS" value="{HTTPS}"/>
                        </serverVariables>
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    另见https://azure.microsoft.com/en-us/documentation/articles/web-sites-transform-extend/https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples

    【讨论】:

    • 在 XML 配置文件中使用 &lt;add&gt; 类型的集合时,您必须同时指定 xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)",否则 XDT 只会复制第一个元素如果目标为空,而不是按预期合并所有元素。我已经编辑了您的答案,以便那些不看就复制并粘贴您的代码的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多