0
votes

I have one windows service which is running some application with REST endpoint on localhost:5001/api/... So I installed IIS 10 with rewrite module to create reverse proxy for HTTPS to that service. That worked great.

Now I want to use the same IIS site for hosting a website, which should consume that service on same url. So I tried to change the pattern of my iis rewrite url to 'api/(.*)' so it should just proxy the /api requests. Testing the pattern is exactly what I want to: Browsing server.domain/api/function matches and the rewrite is done and server.domain is not rewritten. But now browsing server.domain will often return 'HTTP Error 500.52 - URL Rewrite Module Error'. Sometimes loading the page works great. But after reload I will get that error again.

My web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:5001/(.*)" />
                    <action type="Rewrite" value="https://server.domain/{R:2}" />
                </rule>
                <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml1" enabled="true">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:5001/(.*)" />
                    <action type="Rewrite" value="https://server.domain/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
            <rules>
                <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
                    <match url="api/(.*)" />
                    <action type="Rewrite" url="http://localhost:5001/{R:0}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        <httpErrors errorMode="Detailed" />
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, DELETE, PUT" />
                <add name="Access-Control-Max-Age" value="1000" />
                <add name="Access-Control-Allow-Headers" value="x-requested-with, Content-Type, origin, authorization, accept, client-security-token" />
            </customHeaders>
        </httpProtocol>
        <urlCompression doDynamicCompression="false" />
    </system.webServer>
</configuration>

I have no idea why there are two outboundRules. IIS created them automatically when I decided to create reverse proxy rules.

If I disable static content compression, all will work without problems. Dynamic compression module is not installed.

So I have multiple questions: - why do I get the "Rewrite URL" error for pages which should not be rewritten and the rewritten ones will work without any problem - why static compression creates rewrite url errors - how should I configure my IIS to do compression and work with rewriting (I checked the module order and the static compression module is above the rewrite module).

Best, Robin

1

1 Answers

0
votes

Did you create reverse proxy rule by using URL rewrite rule template? If so, you might enable outbound rules. These outbound rules are mainly used when a client can't access source file behind DMZ server. If you client are able to access the domain on backend server, then you don't need these outbound rule.

The reason for IIS return 500.52 error is IIS can't apply outbound rule for a encoded response body from backend server.

So you have to either disable httpcompression or rewrite the incoming accept-encoding header.

I'm afraid it is unavailable to make httpcompression and outbound rule work side by side because rewrite module can't rewrite a compressed entity.

https://docs.microsoft.com/zh-cn/archive/blogs/friis/iis-with-url-rewrite-as-a-reverse-proxy-part-2-dealing-with-500-52-status-codes