在IIS7中配置反向代理
时间:2010-5-24来源:互联网 点击:次
同样配置的web.config文件如下:
<rewrite>
<rules>
<rule name="phpweb">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^phpweb.leven.com.cn$" />
</conditions>
<action type="Rewrite" url="http://localhost:8081/{R:1}" />
</rule>
</rules>
</rewrite>
测试访问http://phpweb.leven.com.cn/test.php,结果如下:
下面同样可以配置levenblog.leven.com.cn和realblog.leven.com.cn
Ui界面配置不再说明,配置完成的web.config如下:
<rewrite>
<rules>
<rule name="levenblog">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^levenblog.leven.com.cn$" />
</conditions>
<action type="Rewrite" url="http://localhost:8080/{R:1}" />
</rule>
<rule name="realblog">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^realblog.leven.com.cn$" />
</conditions>
<action type="Rewrite" url="http://leven.com.cn/{R:1}" />
</rule>
<rule name="phpweb">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^phpweb.leven.com.cn$" />
</conditions>
<action type="Rewrite" url="http://localhost:8081/{R:1}" />
</rule>
</rules>
</rewrite>
访问结果分别为:
和
我们再添加最后一项,将http://localhost/leven代理到 http://leven.com.cn/
<rule name="leven.com.cn">
<match url="^leven/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" />
</conditions>
<action type="Rewrite" url="http://leven.com.cn/{R:1}" />
</rule>
但是此时访问会出现问题,如下图:
显然,出现了css丢失等情况,通过查看源码:
可以看到css的路径有误,不仅如此,所有的img,a标签路径全部出现了错误,代理之后的地址是/leven/xxx的,但是源地址仍然是/xxx,因此我们还需要增加一个Outbound Rule
配置好的config文件如下:
<outboundRules>
<rule name="Add application prefix">
<match filterByTags="A,Img,Script,Link" pattern="^/(.*)" />
<conditions>
<add input="{URL}" pattern="^/leven/.*" />
</conditions>
<action type="Rewrite" value="/leven/{R:1}" />
</rule>
</outboundRules>
然后刷新:

可见路径正确.
在使用了反向代理之后,编程上也有些地方需要注意了,在取客户端IP的时候,由于多了一层代理,直接是无法获取的,因此,我们需要开启
然后通过获取Header中的X-Forworded-For字段来取得客户端IP
从测试来看,ARR是个非常有用的代理模块,能完全满足我们反向代理的需求,不仅如此,ARR还提供了UrlRewrite,ServerFarms,Cache等很多功能,很是值得我们挖掘.
.
分页: [
1] [
2]
TAG:
IIS7 反向代理