利用apache和tomcat做集群,有好几种方式我们来介绍一种方法。
一:查看是否有mod_proxy模块功能
[i5a6]# /usr/local/apache/bin/httpd -l
可以看到是否有mod_proxy的模块,直接去 /usr/local/apache/modules/ 去查看是否有对应的mod_proxy.so,mod_proxy_balancer.so,mod_proxy_http.so,mod_proxy_ajp.so的模块包
二:本人的是vps已经安装好的apache不过没有编译此模块要自己动手编译
[i5a6]# find / -name mod_proxy.c
注释:我是不知道它的安装目录,直接根目录下检索吧,主要是找对应的模块文件夹;执行结果:
:/home/cpeasyapache/src/httpd-2.2.17/modules/proxy/mod_proxy.c
[i5a6]# cd /home/cpeasyapache/src/httpd-2.2.17/modules/proxy/
[i5a6]# /usr/local/apache/bin/apxs -c -i mod_proxy.c proxy_util.c
[i5a6]# /usr/local/apache/bin/apxs -c -i mod_proxy_balancer.c
[i5a6]# /usr/local/apache/bin/apxs -c -i mod_proxy_http.c
# 安装proxy_ajp模块
[i5a6]# /usr/local/apache/bin/apxs -c -i mod_proxy_ajp.c ajp_header.c ajp_utils.c ajp_msg.c ajp_link.c
可以去 /usr/local/apache/modules/ 看下生成了对应mod_proxy.so,mod_proxy_balancer.so,mod_proxy_http.so,mod_proxy_ajp.so的包。
三:配置httpd.conf,设置权重和session严格复制等。
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
ProxyRequests Off
<Proxy balancer://clusterphpinfo>
BalancerMember http://192.168.1.101:8080 loadfactor=5
BalancerMember http://192.168.1.100:8080 loadfactor=1
ProxySet lbmethod=bytraffic
</Proxy>
ProxyPass /t2 balancer://clusterphpinfo stickysession=STICK_PORT_TOKEN nofailover=On
ProxyPassReverse /t2 balancer://clusterphpinfo
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Allow from all
</Location>
配置说明:
1,平衡算法,由byrequests变成bytraffic
2,由平均分配变成非平均分配
另一个虚拟主机配置:
<VirtualHost *:80>
ServerName hs.i5a6.com
ServerAlias i5a6.com
DocumentRoot "/usr/local/apache/htdocs/hs"
ProxyPass /hs ajp://127.0.0.1:8009/hs
ProxyPassReverse /hs ajp://127.0.0.1:8009/hs
</VirtualHost>
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:8181/hs loadfactor=40
BalancerMember http://127.0.0.1:8282/hs loadfactor=40
BalancerMemberhttp://127.0.0.1:8080/hs loadfactor=10
BalancerMember http://127.0.0.1:8081/hs loadfactor=10
#热备,如果8181服务挂掉,apache会自动把请求发送到8282,如果8181恢复服务,apache会把请求发送到8181
#BalancerMember http://127.0.0.1:8282 status=+H
</Proxy>
<VirtualHost *:80>
ServerName hs.i5a6.com
DocumentRoot "/usr/local/apache/htdocs/hs"
#内部的请求 /hs/ 代理 balancer://mycluster/ 模块的请求
#ProxyPass /hs/ balancer://mycluster/
#stickysession session的性质,是否具有粘性
#nofailover=On不支持会话复制
#lbmethod 负载均衡的算法,lbmethod可能的取值有:
#lbmethod=byrequests 按照请求次数均衡(默认)
#lbmethod=bytraffic 按照流量均衡
#lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
#maxattempts=1 apache在放弃服务前,尝试的次数
ProxyPass /hs/ balancer://mycluster/ stickysession=STICK_PORT_TOKEN nofailover=On lbmethod=byrequests maxattempts=1
ProxyPassReverse /hs balancer://mycluster/
#调整由反向代理服务器发送的HTTP应答头中的URL.
#根据代理打开 balancer://mycluster/ 模块的a.html
#如果点击a.html页面的内部链接(绝对路径另论),需要这个设置将路径修改为代理的路径.
</VirtualHost>
apache 反向代理出现内网ip解决方法
apache中设置如下反向代理后端的tomcat出现request.getServerName();获取到的主机名称就会是localhost,解决这个
<VirtualHost *:80>
ServerAdmin service@i5a6.com
DocumentRoot /usr/local/apache/htdocs
ServerName i5a6.com
ServerAlias *.i5a6.com
DirectoryIndex index.html index.html.var index.jsp
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog "logs/hscode-error.log"
CustomLog "logs/hscode-access.log" common
</VirtualHost>
在httpd.conf 文件中增加:ProxyPreserveHost On 即可,对应的nginx 也有此问题解决之法:
proxy_set_header Host $host;
upstream mytomcat{
#server 127.0.0.1:8000 weight=3;
#server 127.0.0.1:8001;
#server 127.0.0.1:8002;
server 127.0.0.1:8080;
}
server
{
listen 80;
server_name i5a6.com *.i5a6.com;
index index.html index.htm ;
root /usr/local/data/htdocs/www;
location / {
# proxy_set_header Host %host;
# proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://www.最模板zuimoban.com;
proxy_set_header Host $host;
}
|