关于apache反向代理实现负载均衡,编译mod_proxy模块解决办法,利用apache和tomcat做集群,有好几种方式:
一:查看是否有mod_proxy模块功能
- [root@localhost ~]# /usr/local/apache/bin/httpd -l
- [root@localhost ~]# /usr/local/apache/bin/httpd -M
可以看到是否有mod_proxy的模块,直接去 /usr/local/apache/modules/ 去查看是否有对应的mod_proxy.so,mod_proxy_balancer.so,mod_proxy_http.so,mod_proxy_ajp.so的模块包
二:已经安装好的apache不过没有编译此模块要自己动手编译
- [root@localhost ~]# cd /usr/local/src/httpd-2.2.31/modules/proxy
- [root@localhost proxy]# /usr/local/apache/bin/apxs -c -i mod_proxy.c proxy_util.c
- [root@localhost proxy]# /usr/local/apache/bin/apxs -c -i mod_proxy_balancer.c
- [root@localhost proxy]# /usr/local/apache/bin/apxs -c -i mod_proxy_http.c
安装proxy_ajp模块
- [root@localhost proxy]# /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=10;
- server 127.0.0.1:8001 weight=20;
- server 127.0.0.1:8002 weight=30;
- server 127.0.0.1:8080 weight=40;
- }
-
- 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://mytomcat;
- proxy_set_header Host $host;
- }
另附:Linux apache单独编译mod_rewrite.so
1.寻找apache源码安装目录中的mod_rewrite.c文件
#find / -name mod_rewrite.c
/usr/httpd-2.2.22/modules/mappers/mod_rewrite.c
2.切换到mod_rewrite.c文件目录
#cd /usr/httpd-2.2.22/modules/mappers/
3.编译
#/usr/apache/Apache2.2/bin/apxs -c mod_rewrite.c
#/usr/apache/Apache2.2/bin/apxs -i -a -n mod_rewrite mod_rewrite.la
在/usr/apache/Apache2.2/modules中会编译好mod_rewrite.so
4.如果是Apache2.2.22版本
LoadModule mod_rewrite_module modules/mod_rewrite.so 前面添加#注释符;
1 . 如何让自己的本地APACHE服务器支持.htaccess
如何让自己的本地APACHE服务器支持".htaccess"呢?其实只要简单修改一下apache的httpd.conf设置就可以让APACHE支持.htaccess了,来看看操作
打开httpd.conf文件(在那里? APACHE目录的CONF目录里面),用文本编辑器打开后,查找
(1)
Options FollowSymLinks
AllowOverride None
改为
Options FollowSymLinks
AllowOverride All
(2)去掉下面的注释
LoadModule rewrite_module modules/mod_rewrite.so
就可以了