<allowedServerVariables>标记应位于Azure网站applicationHost.config中的何处?

mar*_*rdy 5 iis reverse-proxy url-rewriting azure

技术信息

情境

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

我想做的是通过custom将初始请求的HTTPS标志ON/OFF发送到代理服务器HTTP Header

理论上

  • 使用shibayanIIS Manager Site Extension,我可以编辑applicationHost.xdt文件,给它一个变换插入一个<allowedServerVariables>标签,并应让我设置自定义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>
Run Code Online (Sandbox Code Playgroud)

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

尝试一个:

答案中所述

<?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>
Run Code Online (Sandbox Code Playgroud)

结果:

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>
Run Code Online (Sandbox Code Playgroud)

结果: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>
Run Code Online (Sandbox Code Playgroud)

结果: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>
Run Code Online (Sandbox Code Playgroud)

结果:HTTP 503

我知道在的applicationHost.config文件Azure Website<system.webServer>可以定义一些位置,例如以下元素:

  • <configuration>
  • <configuration><location>

...但是我没有尝试过这些组合。

问题

  • 还有其他可能的位置吗?
  • 我是否.xdt以任何方式错误配置了文件?
  • 我想念我的东西applicationHost.config吗?

小智 5

您必须applicationHost.xdt在站点文件夹下创建一个d:\home\site\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" />
      </allowedServerVariables>
    </rewrite>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在您可以在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>
Run Code Online (Sandbox Code Playgroud)

另请参阅 https://azure.microsoft.com/zh-CN/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 将仅如果目标为空,则复制第一个元素,而不是按预期合并所有元素。为了那些不看代码就复制粘贴您的代码的人,我编辑了您的答案。 (2认同)