█ 3 配置haproxy L7负载均衡-之根据URL地址目录转发
●3.1 准备RS SERVER测试环境
建立测试目录:
for name in php nginx pic ;do mkdir -p /var/$name/$name;echo $name >/var/$name/$name/index.html;done
配置http服务
删除上面做的任何apache配置(或者还原httpd.conf)
[root@ha2 conf]# cd /etc/httpd/conf
[root@ha2 conf]# /bin/cp httpd.conf.oldboy.110625 httpd.conf
然后编辑httpd.conf 最下面加
<Directory "/var">
Options FollowSymLinks
AllowOverride none
Order allow,deny
Allow from all
</Directory>
Listen 8080
Listen 8090
NameVirtualHost *:80
NameVirtualHost *:8080
NameVirtualHost *:8090
<VirtualHost *:80>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
ServerAlias etiantian.org
DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:8090>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
DocumentRoot "/var/pic"
</VirtualHost>
● 3.2 基于目录转发的haproxy配置
frontend oldboy_test
bind *:80
acl short_domain hdr(Host) -i etiantian.org
redirect prefix http://www.etiantian.org code 301 if short_domain
#acl lang_domain hdr(Host) -i www.etiantian.org
#use_backend staticpools if lang_domain
default_backend staticpools
#http://www.etiantian.org/nginx/...,提供静态内容访问(htm,html,css,js)
backend staticpools
balance roundrobin
server oldboy-1 10.0.0.151:80 check port 80 inter 3000 fall 3
server oldboy-2 10.0.0.151:81 check port 81 inter 3000 fall 3
server oldboy-3 10.0.0.151:82 check port 82 inter 3000 fall 3
#提示:后面两个rs是虚构的,只是告诉大家可以这样加server.有没有都不会影响访问。以下同。
#http://www.etiantian.org/php/... 提供动态内容访问(以*.php结尾)
backend dynamicpools
balance roundrobin
cookie SERVERID insert indirect
server oldboy-4 10.0.0.151:8080 maxconn 2048 weight 50 cookie A check port 8080 inter 3000 fall 3
server oldboy-5 10.0.0.151:8180 maxconn 2048 weight 100 cookie B check port 8180 inter 3000 fall 3
server oldboy-6 10.0.0.151:8280 maxconn 2048 weight 100 cookie C check port 8280 inter 3000 fall 3
#列出更详细的参数,供大家参考使用。
#http://www.etiantian.org/pic/... 提供图片内容访问(以*.jpg,*.png,*.gif等结尾)
backend picpools
balance roundrobin
server oldboy-7 10.0.0.151:8090 check port 8090 inter 3000 fall 3
server oldboy-8 10.0.0.151:8190 check port 8190 inter 3000 fall 3
server oldboy-9 10.0.0.151:8290 check port 8290 inter 3000 fall 3
#提示:server后面的名称是自己定义的,名称不要重复就好。
当多台服务器时,每个池子可配置成如下形式:
cookie SERVERID insert indirect
server oldboy-1 10.0.0.163:8080 cookie ett-1-1 check port 8080 inter 3000 fall 3
配置基于URL的健康检查
option httpchk HEAD /checkstatus.jsp HTTP/1.0
●3.3 配置用户端hosts
10.0.0.162 www.etiantian.org
提示:10.0.0.162 为haproxy的IP,这里是模拟DNS的解析,实际配置时,在DNS里解析即可
●3.4 在用户端进行访问测试
3.4.1 查看hosts解析是否正确
C:\Documents and Settings\hyran>ping www.etiantian.org
Pinging www.etiantian.org [10.0.0.162] with 32 bytes of data:
Reply from 10.0.0.162: bytes=32 time=5ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
3.4.2 测试URL地址 看是否符合预期要求
分别访问:
http://www.etiantian.org/nginx/
http://www.etiantian.org/php/
http://www.etiantian.org/pic/
如果能返回对应自己目录的内容,那恭喜你,搞定了。
下面还有一个配置代理的例子
global
maxconn 12345
nbproc 2
frontend myproxy_1000
mode http
bind *:10000
monitor-uri /haproxy
acl baiduduty url_beg /duty
acl baiduhost hdr_beg(host) -i www.baidu.com
acl itrushost hdr_beg(host) -i www.itrus.com.cn
use_backend baidu if baiduhost
use_backend itrus if itrushost
# default_backend google
backend baidu
mode http
server server2 www.baidu.com:80