<allowedServerVariables> 标记应该位于 Azure 网站 applicationHost.config 的什么位置?

Where should <allowedServerVariables> tag live in Azure Website applicationHost.config?

技术信息

场景

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

我想做的是通过自定义 HTTP Header.

从初始请求向代理服务器发送 HTTPS 标志 ON/OFF

理论上

练习中

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

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

并尝试了几种放置 <serverVariables> 标签的组合...

尝试一:

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 Error 500.50 - URL Rewrite Module Error.

The server variable "HTTP_X_USE_HTTPS" is not allowed to be set. Add the server variable name to the allowed server variable list.

尝试二:

<?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 文件中有几个地方可以定义 <system.webServer>,例如在以下元素下:

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

问题

您必须在网站文件夹 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/ or https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples