Apache可通过mod_proxy*.so系列模块很好的实现正向、反向代理功能,亦可通过反向代理实现负载均衡,本文将讲述基于Apache反向代理实现负载均衡的配置方法。
1、环境配置说明:
代理服务器域名:ap.proxy
web server 1 ip:192.168.195.18 port:80
web server 2 ip:192.168.195.19 port:80
backup server ip:192.168.195.20 port:80
2、确认开启模块:mod_proxy, mod_proxy_balancer, mod_proxy_http
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
3、简单反向代理设置:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName ap.proxy
ProxyRequests Off
ProxyMaxForwards 100
ProxyPreserveHost On
ProxyPass / http://192.168.195.18/
ProxyPassReverse / http://192.168.195.18/
<Proxy *>
Order Deny,Allow
Allow from all
</Proxy>
</VirtualHost>
4、反向代理实现负载均衡:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName ap.proxy
ProxyRequests Off #开启反向代理
Proxypass / balancer://proxy/
<Proxy balancer://proxy>
Order Deny,Allow
Allow from all
BalancerMember http://192.168.195.18 loadfactor=3 #web server 1, loadfactor--负载比例分配
BalancerMember http://192.168.195.19 loadfactor=1 #web server 2, loadfactor--负载比例分配
BalancerMember http://192.168.195.20 status=+H #backup server
#负载分配算法
#lbmethod=byrequests 按照请求次数均衡(默认)
#lbmethod=bytraffic 按照流量均衡
#lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
ProxySet lbmethod=bytraffic
</Proxy>
</VirtualHost>
更多设置项:请参考apache手册……