2

I'm trying to create a reverse proxy which will rewrite all requests to a particular URL to another server inside the DMZ. I got the setup and configuration working based on this article https://learn.iis.net/page.aspx/659/reverse-proxy-with-url-rewrite-v2-and-application-request-routing

So my basic setup is like this -

Base URL which should trigger rewrite - http://MySite/Custom?Data=123

Proxy URL - http://MyProxyServer/Service?Data=123

My problem is with the two BOLD keywords above! I just cannot seem to successfully rewrite all requests to new URL.

Explaining in detail -

When i use the following rule -

<rule name="ReverseProxy" stopProcessing="true">
   <match url="Custom/(.*)" />
   <action type="Rewrite" url="http://MyProxyServer/Service{R:1}" />
</rule>

I get an 404 error saying http://MySite/Service not found!

Tracing using failed req logs, i can see the rules working perfectly (new url is created correctly) but eventually the request ends with 404 error.

Thing is, if i slightly tweak the rule, everything works fine.

Following rule works -

<rule name="ReverseProxy" stopProcessing="true">
   <match url="Service/(.*)" />
   <action type="Rewrite" url="http://MyProxyServer/Service{R:1}" />
</rule>

But i have to change my base url to http://MySite/Service?Data=123 from http://MySite/Custom?Data=123

So is there a limitation with reverse proxy implementation in IIS 7 where we have to match the sub directory/virtual directory names between source and proxy or are my rules inadequate?

1

There is an undocumented issue with ARR / rewrite, where it cannot map subfolders. I am adding this for my reference and hoping it helps others

A workaround I have tried that seems to be effective, is to create a folder inside your source web application, add a web.config inside that folder with the rules pointing to the destination web application:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://<<<TARGET_HOSTNAME>>>{R:1}" />
                    <serverVariables>
                        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                        <set name="HTTP_ACCEPT_ENCODING" value="" />
                    </serverVariables>
                </rule>
            </rules>
            <outboundRules>
                <rule name="RestoreAcceptEncoding" preCondition="RestoreAcceptEncoding">
                    <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)$" />
                    <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
                </rule>
                <preConditions>
                    <preCondition name="RestoreAcceptEncoding">
                        <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

Also added instructions from https://blogs.msdn.microsoft.com/friis/2016/08/25/iis-with-url-rewrite-as-a-reverse-proxy-part-2-dealing-with-500-52-status-codes/ around GZIP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.