如何转换nginx反向代理"sub_filter"到IIS10 URL重写-出站规则? [英] How to convert an nginx reverse proxy "sub_filter" to IIS10 URL Rewrite - Outbound Rule?

查看:136
本文介绍了如何转换nginx反向代理"sub_filter"到IIS10 URL重写-出站规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用nginx(适用于Windows)通过相应的sub_filter创建以下反向代理,以将CSS注入HTML.我想在IIS 10中复制它:

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;

}

我可以使用IIS URL重写设置相同的反向代理.但是,我不知道如何将相应的sub_filter代码添加到URL重写规则.我猜可以用Outbound Rule完成;但是,我无法创建正常运行的出站规则.

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.

下面,除了我的反向代理服务器http://mywebsite.com/files之外,这是我最好的添加出站规则的方法.不幸的是,我得到的响应是:"500-内部服务器错误".当我访问该网页时.

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.

此外,我不确定如何限制此出站规则以仅将此CSS mod应用于反向代理:http://mywebsite.com/files.我试图添加一个条件以将其限制为仅我的反向代理.但显然,我做得不好.无论我做什么,都通过内部服务器错误"消息破坏了我的多个网站.

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. 

推荐答案

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

没有服务器变量名称"FILES"在iis中.而不是尝试使用如下条件:

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>

这篇关于如何转换nginx反向代理"sub_filter"到IIS10 URL重写-出站规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆