IIS上的反向代理

时间:2014-08-11 12:17:07

标签: asp.net iis-7.5

我在iis 7.5上有一个asp.net网站。我正在尝试使用iis重写器模块进行反向代理。我在web.config文件中实现了以下代码。该代码适用于1个网站news.mysite.co.uk但不适用于blog.mysite.co.uk。看起来我没有正确实施outbond规则。

        

      <rule name="Route the requests for newa" stopProcessing="true">
        <match url="^news/(.*)" />
        <conditions>
          <add input="{CACHE_URL}" pattern="^(https?)://" />
        </conditions>
        <action type="Rewrite" url="{C:1}://news.mysite.co.uk/{R:1}" />
        <serverVariables>
          <set name="HTTP_ACCEPT_ENCODING" value="" />
        </serverVariables>
      </rule>
      <rule name="Route the requests for blog" stopProcessing="true">
        <match url="^blog/(.*)" />
        <conditions>
          <add input="{CACHE_URL}" pattern="^(https?)://" />
        </conditions>
        <action type="Rewrite" url="{C:1}://blog.mysite.co.uk/{R:1}" />
        <serverVariables>
          <set name="HTTP_ACCEPT_ENCODING" value="" />
        </serverVariables>
      </rule>
    </rules>
    <outboundRules>
      <rule name="Add application prefix 2" preCondition="ResponseIsHtml2">
        <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
        <conditions>
          <add input="{URL}" pattern="^/.*" />
        </conditions>
        <action type="Rewrite" value="/blog/{R:1}" />
      </rule>
      <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml2">
        <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.mysite.co.uk/(.*)" />
        <action type="Rewrite" value="/blog/{R:2}" />
      </rule>

      <rule name="Add application prefix" preCondition="ResponseIsHtml1">
        <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
        <conditions>
          <add input="{URL}" pattern="^/.*" />
        </conditions>
        <action type="Rewrite" value="/news/{R:1}" />
      </rule>
      <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
        <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://news.mysite.co.uk/(.*)" />
        <action type="Rewrite" value="/news/{R:2}" />
      </rule>
      <preConditions>
        <preCondition name="ResponseIsHtml1">
          <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
        </preCondition>
        <preCondition name="ResponseIsHtml2">
          <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
        </preCondition>
      </preConditions>
    </outboundRules>
  </rewrite>

1 个答案:

答案 0 :(得分:0)

这是最终的工作代码。

<rewrite>
<rules>
  <rule name="ReverseProxyInboundRule1" stopProcessing="true">
    <match url="^news/(.*)" />
    <action type="Rewrite" url="http://news.mysite.co.uk/{R:1}" />
    <serverVariables>
      <set name="HTTP_ACCEPT_ENCODING" value="" />
    </serverVariables>
  </rule>
  <rule name="ReverseProxyInboundRule2" stopProcessing="true">
    <match url="^blog/(.*)" />
    <action type="Rewrite" url="http://blog.mysite.co.uk/{R:1}" />
    <serverVariables>
      <set name="HTTP_ACCEPT_ENCODING" value="" />
    </serverVariables>
  </rule>
</rules>
<outboundRules>
  <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml2">
    <match filterByTags="A, Img" pattern="^/(.*)" />
    <conditions>
      <add input="{URL}" pattern="^/(news|blog)/.*" />
    </conditions>
    <action type="Rewrite" value="/{C:1}/{R:1}" />
  </rule>
  <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml2">
    <match filterByTags="A, Form, Img" pattern="^http(s)?://news.mysite.co.uk/(.*)" />
    <action type="Rewrite" value="/news/{R:2}" />
  </rule>
  <rule name="ReverseProxyOutboundRule4" preCondition="ResponseIsHtml2">
    <match filterByTags="A, Form, Img" pattern="^http(s)?://blog.mysite.co.uk/(.*)" />
    <action type="Rewrite" value="/blog/{R:2}" />
  </rule>
  <preConditions>
    <preCondition name="ResponseIsHtml2">
      <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
    </preCondition>
  </preConditions>
</outboundRules>
</rewrite>