IIS反向代理在特定路径上应用出站规则

时间:2015-12-02 22:06:10

标签: iis proxy reverse rule outbound

我定义了以下入站和出站规则以使我的反向代理工作。

<rewrite>
    <rules>
        <rule name="Route the requests for backend app" stopProcessing="true">
            <match url="^foldera/folderb/(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="www.site1.com" />
            </conditions>
            <action type="Rewrite" url="http://www.site2.com/{R:1}" />
            <serverVariables>
                <set name="HTTP_ACCEPT_ENCODING" value="" />
            </serverVariables>
        </rule>
    </rules>
    <outboundRules>
        <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml" enabled="true" stopProcessing="false">
            <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
            <action type="Rewrite" value="/foldera/folderb/{R:1}" />
            <conditions>
                <add input="{URL}" pattern="^/foldera/folderb/.*" />
            </conditions>
        </rule>
        <preConditions>
            <preCondition name="ResponseIsHtml">
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

现在,网站&#34; http://www.site2.com/&#34;在#34; http://www.site1.com/foldera/folderb/&#34;中加载了相关内容。并且出站规则确保将site2中的每个资源都重写为http://www.site1.com/foldera/folderb/ {resourcefromsite1} 不幸的是,出站规则也会导致我网站的其余部分崩溃。可能是因为他试图将每个原生资源重写为同一个&#34; http://www.site1.com/foldera/folderb/&#34; folderstructure。 如何才能使出站规则仅响应通过路径http://www.site1.com/foldera/folderb/请求/加载的资源,而不是通过http://www.site1.com/foldera

干杯 的Jeroen

1 个答案:

答案 0 :(得分:0)

你非常接近解决方案。要解决此问题,您必须在 preCondition 中添加 {URL} 作为输入。你的重写规则应该最终看起来像这样:

<rewrite>
    <rules>
        <rule name="Route the requests for backend app" stopProcessing="true">
            <match url="^foldera/folderb/(.*)" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="www.site1.com" />
            </conditions>
            <action type="Rewrite" url="http://www.site2.com/{R:1}" />
            <serverVariables>
                <set name="HTTP_ACCEPT_ENCODING" value="" />
            </serverVariables>
        </rule>
    </rules>
    <outboundRules>
        <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml" enabled="true" stopProcessing="false">
            <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
            <action type="Rewrite" value="/foldera/folderb/{R:1}" />
            <!-- Removed condition from here -->
        </rule>
        <preConditions>
            <preCondition name="ResponseIsHtml">
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                <add input="{URL}" pattern="^/foldera/folderb/.*" /> <!-- Added your condition here -->
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

这种方式出站规则仅在响应 HTML 且当前 URL 获得指定模式时应用。