IIS has been supporting reverse proxy configuration since URL Rewrite and Application Request Routing modules were released a few years ago. It is possible to configure an IIS hosted web site to act as a reverse proxy and forward web request to other URL’s based on the incoming request URL path. This is described in details in Reverse Proxy with URL Rewrite v2 and Application Request Routing.
Not too many people know however that the same kind of configuration can be achieved with a web site hosted in Azure Web Sites. This blog post explains the configurations steps to enable that.
For example if I want to forward all the requests that come to http://ruslany.net/proxy/ to some other URL I’ll need to do two things:
- Enable proxy functionality in ARR
- Add a proxy rewrite rule
Any site hosted in Azure Web Sites has URL Rewrite and ARR enabled. However the proxy functionality is disabled by default in ARR. To enable that we will use the Azure Site Extension XDT transform which will modify the applicationHost.config file for our site and will enable proxy features.
This is the xdt transform file content to enable proxy:
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.webServer> <proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" /> </system.webServer> </configuration>
Create a file named applicationHost.xdt and copy paste this code in there. Then upload this file into the “site” directory of your web site:
After that restart your site for the change to take effect.
Next add the following rewrite rule:
<rule name="Proxy" stopProcessing="true"> <match url="^proxy/?(.*)" /> <action type="Rewrite" url="http://www.iis.net/" /> </rule>
For the purposes of this example I configured proxy to forward all requests to http://www.iis.net/ but you can replace it with the target url of your site. Now if I browse to http://ruslany.net/proxy/ the url rewrite rule will rewrite the request to a different url and then ARR will forward the request (notice the URL in the browser window. It is still my domain name which confirms that this not an HTTP redirect).
This functionality enables some interesting use cases. For example I could have two separate azure web sites ruslany-forum.azurewebsites.net and ruslany-blog.azurewebsites.net that can appear as if they are under same hostname but in different subfolders, e.g. http://ruslany.net/blog/ and http://ruslany.net/forum/.
Thanks to Bill Harts who discovered one additional important configuration step that is required if your site also uses ASP.NET MVC. When MVC is enabled on the web site used as a proxy then the MVC Router intercepts all the requests so they are not processed by ARR. So instead of request being forwarded to a destination server you get HTTP 404 – File Not Found error.
In order to fix this you’ll need to exclude the proxy route from the MVC routes by adding the following code to you MVC application: