0

I'm trying to create a rewrite rule that configures a web site to be used as a reverse proxy, but I'm having some issues setting the host http header to a predefined value different from the domain used in the rewrite (the objective is to use IIS as a reverse proxy for forwarding urls to sendgrid with a custom host value).

In order to illustrate the problem, I've created 2 web sites named url and sendgrid with the bindings url.com and sendgrid.net (I've added custom entries for these names on the hosts file so that the names can be resolved). Now, I need to redirect all requests received on the url web site to the sendgrid website, making sure that the request to the sendgrid web site sets the host http header to url.com. I've started by adding a rewrite rule to the url web site that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{CACHE_URL}" pattern="^(https?)://" />
                    </conditions>
                    <action type="Rewrite" url="http://sendgrid.net/{R:1}" logRewrittenUrl="true" />
                    <serverVariables>
                        <set name="HTTP_HOST" value="url.madeira.gov.pt" />
                    </serverVariables>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The applicationhost.config file has also been updated so that the HTTP_HOST can be changed:

<location path="url">
    <system.webServer>
        <rewrite>
            <allowedServerVariables>
                <add name="HTTP_HOST" />
            </allowedServerVariables>
        </rewrite>
    </system.webServer>
</location>

In order to see what's going on, I've activated Failed Request Tracing and I've noticed that the host header file defined through the previous rule is not applied. I can see that the rule is processed and that the HTTP_HOST header is processed (SET_SERVER_VARIABLE), but when the request is rewritten, it will always set the http host to sendgrid.net (instead of setting it to url.com):

Host header value applied during rewrite

So, is there a way to force the use of a specific value to the host header when a IIS web site is configured to be used as a reverse proxy?

Luis Abreu
  • 4,008
  • 9
  • 34
  • 63

1 Answers1

1

try to set the preserveHostHeader to true by following the below steps:

1)open IIS manager, select the server node.

2)double clic configuration manager.

3)from the section drop down select system.webServer/proxy

4)set preserveHostHeader to true

enter image description here

Note: if you are trying to change the request header it is not possible by using iis URL rewrite rule.

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • Preserving the host grade won't do it because then sendgrid will use it as the domain of the returned response... this url rewrite is a little limited right? – Luis Abreu Nov 09 '20 at 09:11
  • @LuisAbreu this rule only rewrite your page with the destination url. preserve host header use when acting as a reverse proxy, to preserve and retain the original Host header from the client browser when constructing the proxied request to send to the target server.default it is false. – Jalpa Panchal Nov 09 '20 at 09:25
  • I've tried that but didn't inspect the http headers. What I can say is that using postman and setting it with the correct url (replaced the custom url with sendrgrid.net) and forcing the http host header to the original url returns the correct HTML. Doing that with the preserve host header results in a redirect to the wrong url – Luis Abreu Nov 09 '20 at 10:07
  • @LuisAbreu if you just want to use the url.madeira.gov.pt as URL and display the sendgrid.net content just set the reverse proxy rule at url.madeira.gov.pt site.you could refer to this link for more detail about preserve host header https://stackoverflow.com/questions/14841986/iis-aar-url-rewrite-for-reverse-proxy-how-to-send-http-host – Jalpa Panchal Nov 10 '20 at 02:06