日期:2014-05-17  浏览次数:20685 次

Apache 代理(Proxy)

代理分为:正向代理(Foward Proxy)和反向代理(Reverse Proxy)

?

1、正向代理(Foward Proxy)

?

正向代理(Foward Proxy)用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器,由代理服务器负责请求Internet,然后返回Internet的请求给内网的客户端。

?

Internal Network Client ——(request-url)——> Foward Proxy Server?———— > Internet

?

2、反向代理(Reverse Proxy)

?

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。如图:

?

/————> Internal Server1

Internet?————> Reverse Proxy Server ?————> Internal Server2

\————> internal serverN

?

Apache 代理

?

apache支持正向代理和反向代理,但一般反向代理使用较多。

?

?

#正向代理

# 正向代理开关
ProxyRequests On
ProxyVia On

<Proxy *>
Order deny,allow
Deny from all
Allow from internal.example.com
</Proxy>
?

?

?

# Reverse Proxy

# 设置反向代理
ProxyPass /foo http://foo.example.com/bar
# 设置反向代理使用代理服务的HOST重写内部原始服务器响应报文头中的Location和Content-Location
ProxyPassReverse /foo http://foo.example.com/bar
?

? ?注意:ProxyPassReverse 指令不是设置反向代理指令,只是设置反向代理重新重定向(3xx)Header头参数值。

?

举例:

?

下面是典型的APACHE+TOMCAT负载均衡和简单集群配置

?

    ProxyRequests Off  
    ProxyPreserveHost on 
    
    ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=Off
    ProxyPassReverse / balancer://cluster/  
    <Proxy balancer://cluster>  
      BalancerMember  http://localhost:8080 loadfactor=1 retry=10  
      BalancerMember  http://localhost:8081 loadfactor=1 retry=10  
      ProxySet lbmethod=bybusyness  
    </Proxy>

?

? ? ProxyPassReverse / balancer://cluster/ 表示负载均衡配置中的所有TOMCAT服务器,如果响应报文的Header中有Location(3xx指定重定向的URL)或Content-Location(指定多个URL指向同一个实体),则使用请求报文中HOST替换URL中的HOST部分。

?

?

  1. GET http://apache-host/entityRelativeUrl
  2. tomcat response 307 ,Header Location: http://localhost:8080/entityRelativeUrl
  3. apache 重写 response header中的Location为:http://apache-host:8080/entityRelativeUrl

?

注意:只有TOMCAT RESPINSE Location中的URL的Host部分匹配tomcat原始HOST的情况才重写。如307到http://localhost:8088/entityRelativeUrl是不会重写的。

?

?

?

?

?

?