Set HTTP_HOST server variable in ASP.NET (4.7) MVC behind reverse proxy in Azure

1.2k Views Asked by At

I inherited an older .NET 4.7 MVC application that is hosted in Azure and sat behind a reverse proxy (Application Gateway). Rewrite rules were setup to get it working using the right domain when requests are forwarded from the reverse proxy, this was the rewrite rule in place:

Web.Config

<rule name="Set HOST header based on X-Original-Host header">
  <match url="(.*)"></match>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_X_ORIGINAL_HOST}" pattern="^$" negate="true" />
  </conditions>
  <serverVariables>
    <set name="HTTP_HOST" value="{HTTP_X_ORIGINAL_HOST}"></set>
  </serverVariables>
</rule>

I understand that Application Gateway (v1) used non-standard headers like X-Original-Host (instead of X-Forwarded-Host) which is why the rule above applied to set the IIS server variable.

Now, we have migrated to Front Door which sends the standard X-Forwarded headers. That now means the rule above doesn't work by default and I have resorted to workarounds by sending the "X-Original-Host" header manually for each request (using routing rules).

I want to do away with all that and use the standard X-Forwarded-Host header to set the HTTP_HOST server variable.

Questions:

  1. Do I just easily replace HTTP_X_ORIGINAL_HOST with HTTP_X_FORWARDED_HOST?
  2. I've tried to find documentation related to the values above but can't, can someone please point me to the right direction?
  3. Some people are saying to use <proxy enabled="true" preserveHostHeader="true" /> under system.webServer but I can't find official Microsoft documentation regarding this

Basically, I want to know how to set the HTTP_HOST variable with the X-Forwarded-Host header.

0

There are 0 best solutions below