建发浅水湾别墅:apache?ProxyPass?ProxyPassReverse概述

来源:百度文库 编辑:中财网 时间:2024/05/15 00:01:47

apache ProxyPass ProxyPassReverse概述

       mod_proxy代理的作用主要就是进行url的转发,而应用转发功能,可以实现同tomcat等应用服务器的整合,同时也可以实现web集群负载均衡的功能。以下先介绍大致的规则:
       proxy是位于客户端与实际的服务器之间的服务器,一般称为facade server,负责将外部的请求分流,也负责对内部的响应做一些必要的处理。如果结合mod_cache,则可提高访问速度,适当的减轻网络流量压力。闲话少说,直接拿个例子来:
    设本站地址为 http://www.test.com/
    ProxyPass /images/ !
    ProxyPass /js/ !
    ProxyPass /css/ !
    ProxyPass /example http://www.example.com/
    ProxyPassReverse /example http://www.example.com/
    ProxyPass / ajp://127.0.0.1:8009/
    ProxyPassReverse / ajp://127.0.0.1:8009/
    还是上一篇的例子,ProxyPass易理解,就是转发url上的请求,而其中的配置顺序也是需要遵守。要禁止转发的url需要放在一般的请求之前。对于http://www.test.com/images/  http://www.test.com/js/ http://www.test.com/css/的请求是不予转发的,对于http://www.test.com/example/的请求,会转发到http://www.example.com/。
    值得注意的就是ProxyPassReverse的配置了,这是反向代理。为什么要在这里加上这样的配置?我们来看个例子:
    在没有加这样的反向代理设置的情况下,访问http://www.test.com/example/a,如果http://www.example.com/对请求进行了redirect至http://www.example.com/b,那么,客户端就会绕过反向代理,进而访问http://www.test.com/example/b。如果设置了反向代理,则会在转交HTTP重定向应答到客户端之前调整它为http://www.test.com/example/a/b,即是在原请求之后追加上了redirect的路径。
更多更详细的关于mod_proxy的描述可以参见手册:
http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/mod_proxy.html


(一).ProxyPass 指令

说明: 将一个远端服务器映射到本地服务器的URL空间中
语法: ProxyPass [路径] !|url
上下文: 服务器配置, 虚拟主机
状态: Extension
模块: mod_proxy

指令对于您不想对某个子目录进行反向代理时很有用。比如说:

ProxyPass /mirror/foo/i !
ProxyPass /mirror/foo http://foo.com/

将会代理除对/mirror/foo/i请求之外的所有对 foo.com 的/mirror/foo请求。

注意:顺序很重要,您需要把特例情况放在一般代理通过指令
当在配置段中使用时,第一个参数会被忽略而是采用由指令指定的本地目录。

如果您需要一个更加灵活的反向代理配置,请参见使用[P]标记的RewriteRule指令。


(二).ProxyPassReverse 指令

说明: 调整由反向代理服务器发送的HTTP回应头中的URL
语法: ProxyPassReverse [路径] url
上下文: 服务器配置, 虚拟主机
状态: Extension
模块: mod_proxy

此指令使 Apache 调整HTTP重定向回应中Location, Content-Location和URI头里的URL。 HTTP redirect responses. This is essential when Apache is used as a reverse proxy to avoid by-passing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

路径是本地虚拟路径的名称。
url远端服务器的部分URL。与ProxyPass指令中的使用方法相同。

示例:
假定本地服务器拥有地址http://wibble.org/;那么

ProxyPass /mirror/foo/ http://foo.com/
ProxyPassReverse /mirror/foo/ http://foo.com/

will not only cause a local request for the to be internally converted into a proxy request to (the functionality ProxyPass provides here). It also takes care of redirects the server foo.com sends: when http://foo.com/bar is redirected by him to http://foo.com/quux Apache adjusts this to http://wibble.org/mirror/foo/quux before forwarding the HTTP redirect response to the client. Note that the hostname used for constructing the URL is chosen in respect to the setting of the UseCanonicalName directive.

Note that this ProxyPassReverse directive can also be used in conjunction with the proxy pass-through feature ("RewriteRule ... [P]") from mod_rewrite because its doesn't depend on a corresponding ProxyPass directive.

When used inside a section, the first argument is ommitted and the local directory is obtained from the .

(三).实际问题解决及说明:


      ProxyPass /mysys/ http://192.168.8.11:81/mysys/
      ProxyPassReverse /mysys/ http://192.168.8.11:81/mysys/


①  这里有两个mysys,我们分别叫做叫做path_wapm和path_iis

ProxyPass /path_wapm/
     http://192.168.8.11:81/path_iis/
     path_wapm:这个是虚拟的目录名称,可以任意指定一个

②ProxyPassReverse /path_wapm/
     http://192.168.8.11:81/path_iis/
     path_iis:这个必须通过81端口可以访问的


那么,访问http://wapm/服务器/path_wapm的时候,实际访问的将自动转换为了
http://192.168.8.11:81/path_iis/的.

③path_wapm如果不加,就不知道访问哪一个虚拟目录的时候需要使用ASP,

path_iis不加,那么就是访问http://192.168.8.11:81/的效果了

简单来说:就是把IIS站点的一个目录,当作WAPM的一个虚拟目录来访问,
可以是IIS站点的一个子目录,也可以是根目录.