How to convert an nginx reverse proxy with respective "sub_filter" to IIS?RSS

6 replies

Last post Nov 19, 2020 03:32 AM by mkanet2

  • How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 14, 2020 02:36 AM|mkanet2|LINK

    I'm currently using the below nginx reverse proxy with sub_filter.  This allows me to quickly create a reverse proxy along with a respective CSS filter in realtime.

    I can create the equivalent reverse proxy in an IIS URL Rewrite rule relatively easily.  However, I have no idea how to also add the respective nginx sub_filter code (shown below) to the reverse proxy I added in IIS.  I'm guessing this can be done with URL rewrite outbound rule.

    location /files {
        proxy_pass http://localhost:999/files;
        proxy_set_header Accept-Encoding "";
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
    
        sub_filter
        '</head>'
        '<link rel="stylesheet" type="text/css" href="https://github.io/test/CSS/themes/dark.css">    
        <style>
        .CalendarEvent\/downloaded\/2vSrJ {
            background: rgba(39, 194, 76, 0.7) !important;
            border-left-color: rgba(39, 194, 76, 0.0) !important;
        }
        </style>
        </head>';
        sub_filter_once on;
    
    }
    

    Maybe, I need to add a request filtering rule in IIS? Whatever it is, I have no clue how to do this under IIS. I learn from working examples.  If someone can show me what to add specifically to IIS, I'd really appreciate it.  Surely there has to be a way to do this in IIS.

    PS: I asked on StackOverflow a similar question, in case you would also like to get credit for a StackOverflow answer:

    Converting nginx reverse proxy (with sub_filter) to IIS10 URL Rewrite / Request Filtering

    Thanks in advance to anyone who can figure out how to do this.

    Below, is the current reverse proxy rewrite code I have currently in my web.config:

            <rewrite>
                <rules>
                    <rule name="Redirect to HTTPS" enabled="false" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="^OFF$" />
                            <add input="{REMOTE_ADDR}" pattern="192.168.1.2" negate="true" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                    </rule>
                    <rule name="ReverseProxy-FILES" enabled="false" stopProcessing="false">
                        <match url="files\/?(.*)" />
                        <action type="Rewrite" url="http://localhost:999/files/{R:1}" />
                    </rule>
                </rules>
            </rewrite>

  • Rovastar Rovastar

    5486 Posts

    MVP

    Moderator

    Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 14, 2020 02:48 PM|Rovastar|LINK

    I am not fimiliar with the syntax of ngnix's sub filter.

    What are you trying to do? 

    I imagine you want to restrict the reverse proxy to be for only css but I'm really not sure. Are you wanting to had css content for certain cases? 

    Can you explain what the subfilter is doing with examples? 

    Troubleshoot IIS in style
    https://www.leansentry.com/
  • Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 14, 2020 08:07 PM|mkanet2|LINK

    Hi, thanks for offering to help.  I'm trying to inject CSS dynamically to traffic for my reverse proxy: http://mywebsite.com/files.  The CSS code modifications just applying a CSS file + some minor CSS mods.  What I'm trying to do is similar to how the browser addon Stylish works; except I want to do it on the backend, not on the web browser.  I'm already able to do this easily with nginx.  However, it would be nice to be able to do with only IIS+URL Rewrite+ARR.

    Anyway, it looks like this can be done with an Outbound Rule; however, I'm not able to create an Outbound Rule that works correctly.  Doing so results in "500 - Internal server error." responses.  To make things worse, I also haven't figured out to limit the Outbound Rule to only my reverse proxy URL:  http://mywebsite.com/files.  Youll notice, I added a condition in hopes the Outbound Rule would only be limited to my reverse proxy with regex pattern "files\/?(.*)".  However, this doesn't work

    The Below web.config, is my best attempt to add the outbound rule to inject CSS specific to my current reverse proxy http://mywebsite.com/files.  

    Web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpErrors errorMode="Custom" lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath" />
            <rewrite>
                <rules>
                    <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="^OFF$" />
                            <add input="{REMOTE_ADDR}" pattern="192.168.1.2" negate="true" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                    </rule>
                    <rule name="ReverseProxy-FILES" enabled="true" stopProcessing="false">
                        <match url="files\/?(.*)" />
                        <action type="Rewrite" url="http://localhost:999/files/{R:1}" />
                    </rule>
                </rules>
                <outboundRules rewriteBeforeCache="true">
                    <rule name="FILES custom CSS" preCondition="IsHTML" enabled="true">
                        <match filterByTags="None" pattern="&lt;/head>" />
                        <action type="Rewrite" value="&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://github.io/test/CSS/themes/dark.css&quot;>&lt;/head>" />
                        <conditions>
                            <add input="FILES" pattern="files\/?(.*)" />
                        </conditions>
                    </rule>
                    <preConditions>
                        <preCondition name="IsHTML">
                            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                        </preCondition>
                    </preConditions>
                </outboundRules>
            </rewrite>
            <defaultDocument>
                <files>
                    <clear />
                    <add value="index.html" />
                    <add value="default.aspx" />
                    <add value="index.htm" />
                    <add value="Default.htm" />
                    <add value="Default.asp" />
                    <add value="iisstart.htm" />
                </files>
            </defaultDocument>
            <staticContent>
                <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
                <mimeMap fileExtension=".todo" mimeType="text/plain" />
            </staticContent>
            <directoryBrowse enabled="false" />
        </system.webServer>
        <system.web>
            <compilation debug="true" />
            <sessionState mode="InProc" timeout="480" />
        </system.web>
    </configuration>

    Outbound Rule

    Screenshot of IIS URL Rewrite rule

    PS:  In my outbound rule, I purposely didn't add the entire CSS code in my OP until I could get it to work with something a little simpler with just a CSS file.

  • Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 18, 2020 05:32 AM|samwu|LINK

    Hi mkanet2,

    Can you post the contents of the logs you obtained using failed request tracking?

    Best regards,

    Sam

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 18, 2020 09:18 PM|mkanet2|LINK

    Hi Sam, please find the trace you asked for:

    FailedRequestTracing.zip

  • Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 19, 2020 03:21 AM|samwu|LINK

    Hi mkanet2,

    Please post key logs to this thread instead of uploading a file.

    Best regards,

    Sam

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: How to convert an nginx reverse proxy with respective "sub_filter" to IIS?

    Nov 19, 2020 03:32 AM|mkanet2|LINK

    The trace log is quite long.. over 3100 lines.  If there's a specific part that you need, I can paste it here.