反向代理可以实现负载均衡这个是apache环境一个非常重要的功能了,今天我们一起来看看利用apache反向代理、负载均衡及会话保持的例子,希望下文可以帮助到各位.
apache的反向代理及负载均衡效率以及配置过程都比nginx麻烦,在这研究apache的负载均衡只是为了对比其它软件的优缺点.
测试过程如下,查看是否安装proxy模块:
- [root@localhost ~]# httpd -M | grep proxy
- httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName
- Syntax OK
- proxy_module (shared)
- proxy_balancer_module (shared)
- proxy_ftp_module (shared)
- proxy_http_module (shared)
- proxy_ajp_module (shared)
- proxy_connect_module (shared)
查看是否已经加载proxy模块:
- [root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep proxy | grep -v “^#”
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
- LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
- LoadModule proxy_http_module modules/mod_proxy_http.so
- LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
- LoadModule proxy_connect_module modules/mod_proxy_connect.so
编辑httpd.conf配置文件,添加:
- #关闭正向代理
- ProxyRequests Off
- #添加响应头
- Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
- #负载均衡
- <Proxy balancer://cluster1>
- BalancerMember http://192.168.0.153:80 loadfactor=3 route=www1
- BalancerMember http://192.168.0.154:80 loadfactor=1 route=www2
- ProxySet lbmethod=byrequests stickysession=ROUTEID
- </Proxy>
- #对监控页面不传到后端服务器
- ProxyPass /balancer-manager !
- ProxyPass / balancer://cluster1/ nofailover=On
- ProxyPassReverse / balancer://cluster1/
- #设置监控页
- <Location /balancer-manager>
- SetHandler balancer-manager
- Order Deny,Allow //Vevb.com
- Allow from all
- </Location>
loadfactor 参数:
值越高,响应的服务越多,取值在1到100之间.
lbmethod 参数:
lbmethod=byrequests 按请求次数均衡(默认)
lbmethod=bytraffic 按流量均衡
lbmethod=bybusyness 按繁忙程度均衡(总是分配给活跃请求数最少的服务器)
会话保持功能:
route= 设置服务器id
stickysession=ROUTEID 使用服务器ID进行会话保持
apache英文手册,中文版中没有会话保持举例:
http://www.rootop.org/ApacheManual/Apache2.2_en_US/mod/mod_proxy_balancer.html 重启apache,测试.
打开页面时,提示服务器500错误,查看日志:
- [warn] proxy: No protocol handler was valid for the URL /public/js/jquery.js. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.
原因是ProxyPass / balancer://cluster1/ cluster1后面有个斜杠,而不是”balancer://cluster1″,跟网上大部分说的模块加载没关系.
新闻热点
疑难解答