URL Rewrite Module Error. HTTP Error 500.52

147 views Asked by At

So I am facing this issue for quite some time and have tried multiple possible solutions, however none seem to work for me. I keep getting the gzip error. So my backend application runs off Tomcat(Tomee).

Below is my inbound Rule

        <rule name="ApplicationInbound" enabled="true">
            <match url="(.*)" />
            <action type="Rewrite" url="https://Application:543/{R:0}" />
            <serverVariables>
                        <set name="HTTP_ACCEPT_ENCODING" value="" />
                        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
            </serverVariables>
        </rule>

Outbound:

        <rule name="ApplicationOutbound" preCondition="NeedsRestoringAcceptEncoding" enabled="false">
            <match filterByTags="None" pattern="http(s)?://App\.domain\.local:543/(.*)" />
            <action type="Rewrite" value="https://Public:543/{R:0}" />
        </rule>

Precondition

<preCondition name="NeedsRestoringAcceptEncoding">
   <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>
<preCondition name="NeedsRestoringAcceptEncoding">
   <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>

Thanks in advance

Aditional info

  1. When I disable the outbound rule the application loads
  2. I have made the registry entry reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v LogRewrittenUrlEnabled /t REG_DWORD /d 0
  3. this is the order of my modules Modules order
1

There are 1 answers

2
YurongDai On

Outbound rewrite rules cannot be applied when the content of the HTTP response is encoded ("gzip"). Status code for this is 500.52.

This is because the responses that are coming from the back-end server are using HTTP Compression, and URL rewrite cannot modify a response that is already compressed. This causes a processing error for the outbound rule resulting in the 500.52 status code.

There are two ways to work around this:

  1. Turn off compression on the backend server that is delivering the HTTP responses (which may or may not be possible, depending on your configuration).

  2. Attempt to indicate to the backend server the client does not accept compressed responses by removing the header when the request comes into the IIS reverse proxy and by placing it back when the response leaves the IIS server.

For the second method, we will need to add two variables named HTTP_ACCEPT_ENCODING and HTTP_X_ORIGINAL_ACCEPT_ENCODING. we will need to use these variables both in the inbound rules, to remove the Accept-Encoding header and in the Outbound Rules to place this header back again.

You can try using the following rewrite rules in web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ApplicationInbound" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="https://App.domain.local:543/{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="ApplicationOutbound" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^https://App\.domain\.local:543/(.*)" />
                    <action type="Rewrite" value="https://Public:543/{R:1}" />
                </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>

More information you can refer to this link:

https://techcommunity.microsoft.com/t5/iis-support-blog/iis-acting-as-reverse-proxy-where-the-problems-start/ba-p/846259