I have configured IIS to act as a proxy server that forwards requests to an internal backend application. The proxy functionality works fine but I noticed that responses coming back from the backend are not getting compressed before being sent to the client browser.
Here is my current configuration:
- Internal backend application running at
https://backend.internal(not publicly accessible) - Public IIS 7.5 server at
https://public.example.comthat handles both the main website and proxy requests - Need to forward requests from
https://public.example.com/service/PATHtohttps://backend.internal/PATH
I am using URL Rewrite 2.0 and Application Request Routing extensions. Here is the relevant web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Forward to internal service" stopProcessing="true">
<match url="^service/(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://backend.internal/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="FixAbsoluteUrls" preCondition="IsHtmlContent">
<match filterByTags="A, Form, Link, Script" pattern="^http(s)?://backend.internal/(.*)" />
<action type="Rewrite" value="/service/{R:2}" />
</rule>
<rule name="FixRedirects" preCondition="IsHtmlContent">
<match serverVariable="RESPONSE_LOCATION" pattern="^http(s)?://backend.internal/(.*)" />
<action type="Rewrite" value="/service/{R:2}" />
</rule>
<preConditions>
<preCondition name="IsHtmlContent">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression dynamicCompressionBeforeCache="false" />
</system.webServer>
The issue is that I must clear the HTTP_ACCEPT_ENCODING header to avoid this error: “HTTP Error 500.52 - URL Rewrite Module Error. Outbound rewrite rules cannot be applied when the content of the HTTP response is encoded (gzip).”
I have tried setting dynamicCompressionBeforeCache="false" and verified the module order in IIS but compression still does not work when proxying to a different application. Simple URL rewrites within the same app work fine with compression.
Is there a way to enable gzip compression for responses that come from the proxied backend server?