How to convert an nginx reverse proxy “sub_filter” to IIS10 URL Rewrite - Outbound Rule?

老子叫甜甜 提交于 2021-01-01 06:09:38

问题


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. 

回答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>


来源:https://stackoverflow.com/questions/64813895/how-to-convert-an-nginx-reverse-proxy-sub-filter-to-iis10-url-rewrite-outbou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!