1

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?

AbhiAbzs
  • 21
  • 4
  • 2
    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 – Lex Li Nov 18 '21 at 01:47
  • 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 Nov 20 '21 at 19:11
  • 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 – AbhiAbzs Nov 20 '21 at 19:24
  • 2
    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. – Lex Li Nov 20 '21 at 20:02
  • 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 – AbhiAbzs Nov 27 '21 at 20:38

0 Answers0