2

I'm currently using nginx (for Windows) to create the below reverse proxy with respective sub_filter to inject CSS to the HTML. I would like to replicate this in IIS 10:

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;

}

I can set up the same reverse proxy using IIS URL Rewrite. However, I don't know how to add the respective sub_filter code to URL Rewrite rules. I'm guessing it can be done with an Outbound Rule; however, I'm not able to create an Outbound Rule that works correctly.

Below, is my best attempt to adding the outbound rule in addition to my reverse proxy http://mywebsite.com/files. Unfortunately, the response I get is: "500 - Internal server error." when I visit the webpage.

Also, I'm not sure how to limit this outbound rule to apply this CSS mod only to the reverse proxy: http://mywebsite.com/files. I tried to add a condition to limit it to just my reverse proxy; but apparently, I didn't do it right. Whatever I did broke more than one of my websites with the Internal Server Error message.

<?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>

PS:  In my outbound rule, purposely didn't add the entire CSS until I could get it to work with something a little simpler. 
MKANET
  • 573
  • 6
  • 27
  • 51

1 Answers1

-1
<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>

there is not server variable name "FILES" in iis. instead of it try to use a condition like below:

<conditions>
                    <add input="{REQUEST_URI}" pattern="files\/?(.*)" />
                </conditions>
Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • No difference at all from what I had before. 500 - Internal server error. It also breaks my other URL's too. If I disable this outbound rule, I am able to get to http://mywebsite.com/files again as well as other URLs. – MKANET Nov 17 '20 at 02:50
  • @MKANET please check the failed request tracing result [image](https://imgur.com/a/y3aSsO2) it is working properly without any issue with the "files" and without files. try to clear the cache of the bwroser. run failed request tracing and share the tracing result. – Jalpa Panchal Nov 18 '20 at 02:35
  • @Jalpal Panchal. I cleared the cache and ran failed tracing for you. Please download from the link below. Thank you. https://workupload.com/file/NMwv3vMQtTR – MKANET Nov 18 '20 at 04:49
  • @MKANET in your web.config file The precondition "IsHTML" does not exist. make sure it is available. another is make sure in the failed request tracing area rewrite checkbox is checked. [image](https://i.imgur.com/UNrKqx3.png) – Jalpa Panchal Nov 19 '20 at 08:55