首页 > 代码库 > centos7下apache2.4反向代理

centos7下apache2.4反向代理

apache安装目录在/data/apache24,这里就不介绍apache的安装了。

一、反向代理配置

在/data/apache24/conf/extra下创建htttpd-proxy.conf文件

#开启反代理的必须模块LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule slotmem_shm_module modules/mod_slotmem_shm.soLoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.soLoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.soLoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so#关闭正向代理ProxyRequests Off#设置集群组,mycluster名称可以随意取<Proxy "balancer://mycluster">    BalancerMember http://192.168.1.222:8080    BalancerMember http://192.168.1.233:8080</Proxy>#使用集群组,mycluster名称需与上面配置相同ProxyPass "/demo" "balancer://mycluster/"ProxyPassReverse "/demo" "balancer://mycluster/"

修改/data/apache24/conf/httpd.conf中添加

Include conf/extra/htttpd-proxy.conf

这样重启apache后,当我们访问网站/demo时,会反向代理到222和233上的8080端口。

 

二、apache负载均衡管理界面

<Location "/balancer-manager">    SetHandler balancer-manager    order Deny,Allow    Deny from all    Allow from localhost</Location>

访问网站/balancer-manager时,就可以看到apache自带的一个负载均衡管理界面。

 

三、虚拟主机上配置反向代理

<VirtualHost *:80>    DocumentRoot "/data/wwwroot/test"    ServerName www.test.com    ErrorLog "logs/www.test.com-error_log"    CustomLog "logs/www.test.com-access_log" common    #当我们访问www.test.com时,都会代理到mycluster集群中。    ProxyPass "/" "balancer://mycluster/"    ProxyPassReverse "/" "balancer://mycluster/"</VirtualHost>

 

四、负载比例分配

loadfactor取值范围为1-100

ProxyRequests Off<Proxy "balancer://mycluster">    BalancerMember http://192.168.1.222:8080 loadfactor=6    BalancerMember http://192.168.1.233:8080 loadfactor=4    BalancerMember http://192.168.1.244:8080 loadfactor=2</Proxy>ProxyPass "/" "balancer://mycluster/"ProxyPassReverse "/" "balancer://mycluster/"

  

五、负载分配算法

lbmethod取值有:
lbmethod=byrequests 按照请求次数均衡(默认)
lbmethod=bytraffic 按照流量均衡
lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)

ProxyRequests Off<Proxy "balancer://mycluster">    BalancerMember http://192.168.1.222:8080 loadfactor=6    BalancerMember http://192.168.1.233:8080 loadfactor=4    BalancerMember http://192.168.1.244:8080 loadfactor=2</Proxy>ProxyPass "/" "balancer://mycluster/"ProxyPassReverse "/" "balancer://mycluster/"ProxySet lbmethod=bytraffic

 

五、热备份

请求总是流向222,一旦222挂掉, apache会检测到错误并把请求分流给233。apache会每隔几分钟检测一下222的状况,如果222恢复,就继续使用222。

ProxyRequests Off<Proxy balancer://mycluster>    BalancerMember http://192.168.1.222:8080    BalancerMember http://192.168.1.233:8080 status=+H</Proxy>ProxyPass "/" "balancer://mycluster/"ProxyPassReverse "/" "balancer://mycluster/"

 

centos7下apache2.4反向代理