Score:1

IIS ARR 3.0 - Change Base Url in Web API Response body using Url Rewrite

vn flag

I have a local IIS server hosting Asp.net Core 3.1 web API running at port 5000. Using wireguard the local server is connected with the public facing server. The public facing server also has IIS 10 with ARR3.0 enabled & have the site created as https://api.example.com. Using IIS as reverse proxy the local API has been exposed & is being consumed by the mobile app.

The following Url Rewrite rule has been written in web.config:-

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://192.168.2.79:5000/{R:1}" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
                    <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://192.168.2.79:5000/(.*)" />
                    <action type="Rewrite" value="http{R:1}://api.example.com/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern=".*/.*" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

The API works just fine & return all response. The problem comes when the API response return dynamically generated Image Url's which is correct if directly deployed on the server, but behind the Reverse Proxy its returning the local Url, which is not correct. I want to rewrite the Base Url's in the API response body. how can I do that?

Test API Call from Swagger Docs

Code for Dynamically Generating Image Url:- Code for Dynamically Generating Image Url

Please let me know, how can I rewrite the base url inside API response body?

Lex Li avatar
vn flag
It's purely a programming question then. Ask the developer to learn how to proper use original host name, not the one after rewrite, https://stackoverflow.com/a/53220168/11182
AbhiAbzs avatar
vn flag
Not at all, its IT & OP's task, here some else also asked a similar question but it didn't worked for me with required modifications: https://serverfault.com/questions/508982/using-url-rewrite-to-change-part-of-url.
AbhiAbzs avatar
vn flag
Also if we are able to rewrite html contents, etc inside response, the why not API response? https://techcommunity.microsoft.com/t5/iis-support-blog/iis-and-url-rewrite-rewriting-the-outbound-response-contents/ba-p/846351
Lex Li avatar
vn flag
From IT&OP's point of view, every functionality/feature has its limits, and unfortunately you now see the limits of outbound rules, which are designed to only rewrite simple HTML tags (the other ServerFault thread), but not JSON response (your case). "why not"? Because that's really the responsibilities of the developers.
AbhiAbzs avatar
vn flag
I agree its not possible, IIS can perform rewrite in html response in case of some tags but not in other case. Thanks for your guidance
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.