apache实现服务器负载均衡

1. 什么是负载均衡
负载均衡 (Load Balancing) 负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

2.实现(我用的是反向代理来实现的)
1) 条件:3台服务器:103服务器,220服务器,200服务器,以220作为代理服务器,103和200为实际处理请求的服务器,以下简称为(103,220,200)

2) 利用php写一个测试程序,比如:我分别在103,220,200服务器apache的根目录(/var/www/html)新建一个文件:
vi /var/www/html/test/proxy_test.php
各个服务器上对应输出:“proxy test:103”,“proxy test:220”,“proxy test:200”

修改代理服务器220的apache的配置文件

    
    DocumentRoot /var/www/html
    ServerName test2.domain.com
    ErrorLog logs/test2.domain.com.com-error_log
    CustomLog logs/test2.domain.com.com-access_log common
    ProxyRequests Off  #开启反向代理
    #设置实际处理请求的代理服务器地址
       BalancerMember http://xx.xx.xx.200/test
       BalancerMember http://xx.xx.xx.103/test
       BalancerMember http://xx.xx.xx.220/test
    ProxyPass /test balancer://mycluster/
    ProxyPassReverse /test balancer://mycluster/

3) 以上配置的意思为:凡是访问test2.domain.com/test这个地址都会自动代理到

http://xx.xx.xx.200/test或http://xx.xx.xx.103/test或http://xx.xx.xx.220/test地址下

4) ProxyPass 这里我用的是‘/test’如果写成:ProxyPass / balancer://mycluster/,
那么其意思为:凡是访问test2.domain.com这个地址的都会自动代理到

http://xx.xx.xx.200/test或http://xx.xx.xx.220/test或http://xx.xx.xx.220/test地址下

5) 值得注意的是我这里写了一个代理请求的地址为http://xx.xx.xx.220/test,他其实为代理服务器的地址,但是如果直接这样按上面的写,他是不会有作用的,因为
test2.domain.com域名指向的是/var/www/html地址,而ip地址xx.xx.xx.220实际指向也是/var/www/html地址,所以当通过ip访问的时候,他还是会被代理到其他服务器,
所以我们还得将ip和/var/www/html再关联一遍,如下:

 
    DocumentRoot /var/www/html
    ServerName xx.xx.xx.220
    ErrorLog logs/xx.xx.xx.220-error_log
    CustomLog logs/xx.xx.xx.220-access_log common

注:加上上面的配置后,重启apache可能会报错
‘_default_ VirtualHost overlap on port 80, the first has precedence’
错误原因是:NameVirtualHost *:80被注释掉了,打开就好了

3.测试
访问test2.domain.com/test/proxy_test.php你会发现他会将请求均衡转向到

http://xx.xx.xx.200/test或http://xx.xx.xx.103/test或http://xx.xx.xx.220/test地址下

即会依次循环输出:“proxy test:200”,“proxy test:103”,“proxy test:220”

4.其他用法
1) ProxyPass和ProxyPassReverse的用法:
A) ProxyPass:
语法:ProxyPass [path] !|url

它主要是用作URL前缀匹配,不能有正则表达式,它里面配置的Path实际上是一个虚拟的路径,在反向代理到后端的url后,path是不会带过去的,使用示例:

a)ProxyPass /images/ !

这个示例表示,/images/的请求不被转发。

b)ProxyPass /mirror/foo/ http://backend.example.com/

我们假设当前的服务地址是http://example.com/,如果我们做下面这样的请求:

http://example.com/mirror/foo/bar

那将被转成内部请求:

http://backend.example.com/bar

注:配置的时候,不需要被转发的请求,要配置在需要被转发的请求前面。

B) ProxyPassReverse
语法:ProxyPassReverse [路径] url

它一般和ProxyPass指令配合使用,此指令使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL,这样可以避免在Apache作为反向代理使用时,。后端服务器的HTTP重定向造成的绕过反向代理的问题。参看下面的示例:

 
    ProxyPass /example http://www.example.com/
    ProxyPassReverse /example http://www.example.com/

ProxyPassReverse的作用就是反向代理,如果没有加这样的反向代理设置的情况下,访问http://www.test.com/example/a,如果www.example.com对请求进行了redirect至http://www.example.com/b,那么,客户端就会绕过反向代理,进而访问http://www.test.com/example/b。如果设置了反向代理,则会在转交HTTP重定向应答到客户端之前调整它为http://www.test.com/example/a/b,即是在原请求之后追加上了redirect的路径。

C) ProxyPassMatch:
语法:ProxyPassMatch [regex] !|url

这个实际上是url正则匹配,而不是简单的前缀匹配,匹配上的regex部分是会带到后端的url的,这个是与ProxyPass不同的。使用示例:

a) ProxyPassMatch ^/images !

这个示例表示对/images的请求,都不会被转发。

b) ProxyPassMatch ^(/.*\.gif)$ http://backend.example.com$1

这个示例表示对所有gif图片的请求,都被会转到后端,如此时请求http://example.com/foo/bar.gif,那内部将会转换为这样的请求

http://backend.example.com/foo/bar.gif。

2) 反向代理其他分配方式(以上配置我使用的是平均分配,即每个服务器的请求次数是相同的)
1. 负载比例分配
打开 balancer-manager 的界面,可以看到请求是平均分配的。

如果不想平均分配怎么办?给 BalancerMember 加上 loadfactor 参数即可,取值范围为1-100。
比如你有三台服务器,负载分配比例为 7:2:1,只需这样设置:
Httpd.conf代码

    ProxyRequests Off 

        BalancerMember http://node-a.myserver.com:8080 loadfactor=7 
        BalancerMember http://node-b.myserver.com:8080 loadfactor=2 
        BalancerMember http://node-c.myserver.com:8080 loadfactor=1 

    ProxyPass / balancer://mycluster

2. 负载分配算法

默认情况下,负载均衡会尽量让各个服务器接受的请求次数满足预设的比例。如果要改变算法,可以使 用 lbmethod 属性。如:

Httpd.conf代码

    ProxyRequests Off

        BalancerMember http://node-a.myserver.com:8080 loadfactor=7 
        BalancerMember http://node-b.myserver.com:8080 loadfactor=2 
        BalancerMember http://node-c.myserver.com:8080 loadfactor=1 

    ProxyPass / balancer://mycluster 
    ProxySet lbmethod=bytraffic

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

各种算法的原理请参见Apache的文档。

3) 热备份(Hot Standby)
热备份的实现很简单,只需添加 status=+H 属性,就可以把某台服务器指定为备份服务器:
Httpd.conf代码

    ProxyRequests Off 

        BalancerMember http://node-a.myserver.com:8080 
        BalancerMember http://node-b.myserver.com:8080 status=+H 

    ProxyPass / balancer://mycluster

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

 

本文参考文献:http://blog.51yip.com/apachenginx/873.html

http://koda.iteye.com/blog/465061

http://www.ayuelee.cn/apache-error-default-VirtualHost-overlap.html

http://blog.csdn.net/fenglibing/article/details/6796094

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>