当前位置首页 > IIS知识

使用IIS实现反向代理

阅读次数:177 次  来源:admin  发布时间:

IIS的反向代理是通过ARR模块来完成的,ARR模块需要另外安装,而且只能通过Web PlatForm Installer安装。关于安装来源与步骤,帖子已有很多,不做描述。启用“Application Request Routing”功能以及简单设置URL Rewrite的步骤也不做描述,帖子很多,几乎是千篇一律的贴图,意义不大。在此只记录一些知识要点:

1、规则设置中的第一个步骤“匹配URL”,匹配来源仅仅是请求的完整URL中除去协议、主机头、端口号、querystring后剩下的那一部分:

使用IIS实现反向代理

比如,预计浏览器的请求地址为:http://szh-it-2e02d/test/KWF/Preview.html?ticket=b037ec8c-6579-4e27-be43-dfc3e821c2d2&flowID=1278355,那么仅仅是”test/KWF/Preview.html“这一段会用于匹配测试。所产生的捕获组“{R:0}”“{R:1}”,即为正则表达式执行时匹配成功的块,数目因正则的不同而不同,将作为变量,可在后面的配置中引用。

2、“条件”这一块设置中,条件输入必须是可用的变量,而可以使用的变量有哪些呢?微软的帮助文档url-rewrite-module-configuration-reference一文已经列举得很清楚:

For an HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>

The <path> is matched against the pattern of the rule. The <querystring> is available in the server variable called QUERY_STRING and can be accessed by using a condition within a rule. The <host> is available in the server variable HTTP_HOST and can be accessed by using a condition within a rule. The <port> is available in the server variable SERVER_PORT and can be accessed by using a condition within a rule. Server variables SERVER_PORT_SECURE and HTTPS can be used to determine if a secure connection was used. These server variables can be accessed by using a condition within a rule. The server variable REQUEST_URI can be used to access the entire requested URL path, including the query string.

每一条规则,可以设置零到若干个条件,只有满足步骤1中的URL模式匹配时,才会进入步骤2的“条件”验证,只有满足条件验证,才会进入后续的处理。如果设置了条件,那么也会产生一些变量值,形如{C:0}{C:1},可在后续中使用。

界面上的条件设置只有按正则匹配这一种方式,实际上还有另外两种,但不支持可视化操作,需要手工编写配置文件。

IsFile – This match type is used to determine whether the input string contains a physical path to a file on a file system. If a condition input string is not specified, the URL Rewrite Module uses the physical path of the requested file as a default value for the condition input. This match type can be used only for distributed rules. IsDirectory – This match type is used to determine whether the input string contains a physical path to a directory on a file system. If a condition input string is not specified, the URL Rewrite Module uses the physical path of the requested file as a default value for the condition input. This match type can be used only for distributed rules.

3、服务器变量。这一步可以自由添加一些变量值,也可以修改变量值,如:期望把端口从80转到81,可以做如下配置:

使用IIS实现反向代理

4、操作。这一步骤里首先要选择操作类型,如果是要做反向代理,那么应该选择“重写”这一种类型。重写的URL中,可以使用变量、常量来描述,比如:

使用IIS实现反向代理

5、很重要也是很有用的一点,使用反向代理时,客户端(浏览器)只需要能够访问反向代理服务器即可,与代理后的目标服务器是不产生直接访问的,代理充当了一个请求转发的作用。这一点已做验证。

上一篇:RedHat/Fedora/Centos下bash自动补全命令
下一篇:成功的从fedora7升级到了fedora8