【问题标题】:Azure App Service applicationHost.config Set server variable is working but not workingAzure App Service applicationHost.config 设置服务器变量工作但不工作
【发布时间】:2018-05-01 02:35:51
【问题描述】:

我们正在尝试在给定“X-CF-ORIGIN”标头的情况下重写“主机”标头 - 这在本地有效,但是,在应用程序服务中,所有日志记录都表明它已经有效,但我们似乎没有看到效果。

我们预计不正确的 HOST 标头会收到 500 错误,但是,站点解析时就像主机标头没有更改一样。

此外,我们的 XDT 转换表明它已经工作(见下文)。

This was initially raised on Kudu Github,但已针对 Azure 应用服务 IIS 问题转至此处。

代码示例: https://github.com/Workshop2/webforms-host-header-rewrite-spike

实例: http://webforms-fun.azurewebsites.net/ 带 XDT 变换

我们的重写规则:

<rule name="CDN Host Header Rewrite" stopProcessing="false">
  <match url=".*" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
    <add input="{HTTP_X_CF_ORIGIN}" pattern="(.+)" />
  </conditions>
  <serverVariables>
    <set name="HTTP_HOST" value="{C:1}" />
  </serverVariables>    
  <action type="None" />
</rule>

我们的 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" />
            </allowedServerVariables>
        </rewrite>
    </system.webServer>
</configuration>

applicationHost(通过IIS Manager找到):

<rewrite>
  <allowedServerVariables>
    <add name="HTTP_HOST" />
  </allowedServerVariables>
  <globalRules />
  <outboundRules />
  <providers />
  <rewriteMaps />
  <rules />
</rewrite>

失败的请求跟踪:

测试

如果我创建一个测试规则来证明 HTTP_HOST 正在被更改,它会通过 X-CF-ORIGIN 标头正确使用 HTTP_HOST 数据集:

<rule name="test" stopProcessing="true">
  <match url=".*" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
    <add input="{HTTP_X_CF_ORIGIN}" pattern=".+" />
    <add input="{HTTP_HOST}" pattern="(.+)" />
  </conditions>
  <action type="Redirect" url="http://some-site.com/{C:1}" redirectType="Temporary" />
</rule>

请帮忙 - 我们错过了什么?

【问题讨论】:

    标签: asp.net azure iis azure-web-app-service


    【解决方案1】:

    我发现 Azure 不喜欢设置 HTTP_HOST 服务器变量。如果您使用 HTTP_DISGUISED_HOST

    ,它可以正常工作

    改写规则中的这一行:

    <set name="HTTP_HOST" value="{C:1}" />
    

    到这里:

    <set name="HTTP_DISGUISED_HOST" value="{C:1}" />
    

    在 applicationHost.xdt 中更改这一行:

    <add name="HTTP_HOST" xdt:Transform="InsertIfMissing" />
    

    到这里:

    <add name="HTTP_DISGUISED_HOST" xdt:Transform="InsertIfMissing" />
    

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多