代理分爲:正向代理(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部分。
- GET http://apache-host/entityRelativeUrl
- tomcat response 307 ,Header Location: http://localhost:8080/entityRelativeUrl
- apache 重寫 response header中的Location爲:http://apache-host:8080/entityRelativeUrl
注意:只有TOMCAT RESPINSE Location中的URL的Host部分匹配tomcat原始HOST的情況才重寫。如307到http://localhost:8088/entityRelativeUrl是不會重寫的。