Wednesday, February 14, 2018

Working Reverse Proxy in IIS

Too many references I see about IIS talk about using URL Rewrite to proxy requests to other applications. It's never worked right for me. However, I've found in the past day or so, references that break that impasse.

Procedure

  1. Install URL Rewrite & Application Request Routing (ARR) into your IIS installation.
  2. Create an empty directory where IIS can access it. This is where web.config will live.
  3. Create a virtual directory in IIS for your application. Name it & the path per the subdirectory of the website you're creating (ex: /webservice ). Use the empty directory you created earlier.
  4. Replace the contents of web.config of the directory "hosting" the virtual directory, with a modified version of the example here.

References

web.config

Change DOMAINSERVERNAME to your "external" URL. Change 3000 to whatever local port your other application uses.

Formatted with CodeFormatter


 <?xml version="1.0" encoding="UTF-8"?>  
 <configuration>  
   <system.webServer>  
     <rewrite>  
       <rules>  
         <rule name="ReverseProxyInboundRule1" stopProcessing="true">  
           <match url="(.*)" />  
           <action type="Rewrite" url="http://localhost:3000/{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="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">  
           <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:3000/(.*)" />  
           <action type="Rewrite" value="http{R:1}://DOMAINSERVERNAME/{R:2}" />  
         </rule>  
         <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">  
           <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />  
           <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />  
         </rule>  
         <preConditions>  
           <preCondition name="ResponseIsHtml1">  
             <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />  
           </preCondition>  
           <preCondition name="NeedsRestoringAcceptEncoding">  
             <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />  
           </preCondition>  
         </preConditions>  
       </outboundRules>  
     </rewrite>  
   </system.webServer>  
 </configuration>