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支持正向代理和反向代理,但一般反向代理使用较多。

Apache-配置文件代码  收藏代码
  1. #正向代理  
  2.   
  3. # 正向代理开关  
  4. ProxyRequests On  
  5. ProxyVia On  
  6.   
  7. <Proxy *>  
  8. Order deny,allow  
  9. Deny from all  
  10. Allow from internal.example.com  
  11. </Proxy>  
Apache-配置代码  收藏代码
  1. # Reverse Proxy  
  2.   
  3. # 设置反向代理  
  4. ProxyPass /foo http://foo.example.com/bar  
  5. # 设置反向代理使用代理服务的HOST重写内部原始服务器响应报文头中的Location和Content-Location  
  6. ProxyPassReverse /foo http://foo.example.com/bar  

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

举例:

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

Apache-配置代码  收藏代码
  1. ProxyRequests Off    
  2. ProxyPreserveHost on   
  3.   
  4. ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=Off  
  5. ProxyPassReverse / balancer://cluster/    
  6. <Proxy balancer://cluster>    
  7.   BalancerMember  http://localhost:8080 loadfactor=1 retry=10    
  8.   BalancerMember  http://localhost:8081 loadfactor=1 retry=10    
  9.   ProxySet lbmethod=bybusyness    
  10. </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是不会重写的。

原文地址:https://www.cnblogs.com/beautiful-code/p/6289302.html