-->

Set header from URL Rewrite on Azure Websites - Ap

2019-08-04 01:33发布

问题:

I'd like to set a request header (HTTP_HOST to be precise) from Web.config, using the IIS URL Rewrite module, on Azure Websites. Basically I'd like to have something like this in my site's 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>

This results in an error that HTTP_HOST is not allowed to be set. This is normal and with standard IIS the next step would be to add HTTP_HOST to the <allowedServerVariables> element to applicationhost.config directly or through AppCmd. However I couldn't find any hints on being able to access this config somehow.

Is it possible to somehow modify the apphost config, or add allowed server variables somehow else?

回答1:

This answer was out of date (and should be deleted).



回答2:

It is possible to alter Azure's ApplicationHost.config by applying xdt transformations.

Upload the file to the /site and restart your site for the changes to to take effect:

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>

See also:

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