【问题标题】:How to convert an nginx reverse proxy "sub_filter" to IIS10 URL Rewrite - Outbound Rule?如何将 nginx 反向代理“sub_filter”转换为 IIS10 URL Rewrite - Outbound Rule?
【发布时间】:2021-02-25 01:07:52
【问题描述】:

我目前正在使用 nginx(适用于 Windows)创建以下反向代理,并使用相应的 sub_filter 将 CSS 注入 HTML。我想在 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 Rewrite 设置相同的反向代理。但是,我不知道如何将相应的 sub_filter 代码添加到 URL 重写规则中。我猜它可以通过Outbound Rule 来完成;但是,我无法创建正常工作的出站规则。

下面是我在反向代理http://mywebsite.com/files 之外添加出站规则的最佳尝试。不幸的是,我得到的响应是:“500 - 内部服务器错误”。当我访问网页时。

另外,我不确定如何限制此出站规则以将此 CSS 模块仅应用于反向代理:http://mywebsite.com/files。我试图添加一个条件以将其限制为我的反向代理;但显然,我做得不对。我所做的一切都因内部服务器错误消息而破坏了我的多个网站。

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

【问题讨论】:

    标签: css nginx iis url-rewrite-module


    【解决方案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>
    

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

    <conditions>
                        <add input="{REQUEST_URI}" pattern="files\/?(.*)" />
                    </conditions>
    

    【讨论】:

    • 和我之前的完全没有区别。 500内部服务器错误。它也破坏了我的其他 URL。如果我禁用此出站规则,我可以再次访问mywebsite.com/files 以及其他 URL。
    • @MKANET 请检查失败的请求跟踪结果image 它工作正常,“文件”没有任何问题,也没有文件。尝试清除浏览器的缓存。运行失败的请求跟踪并共享跟踪结果。
    • @Jalpal Panchal。我清除了缓存并为您运行了失败的跟踪。请从以下链接下载。谢谢你。 workupload.com/file/NMwv3vMQtTR
    • @MKANET 在您的 web.config 文件中 前提条件“IsHTML”不存在。确保它可用。另一个是确保在失败的请求跟踪区域重写复选框被选中。 image
    猜你喜欢
    • 2015-10-31
    • 2014-03-16
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多