接上文 haproxy 7层负载均衡代理转发实战讲解(一)-老男孩笔记系列
http://oldboy.blog.51cto.com/2561410/597523
#########################################################
# haproxy
#date:2010-06-09
#作者:老男孩---《老男孩linux就业培训中心 》
#QQ:31333741 MAIL:[email protected]
#QQ交流群:45039636
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即将开放)
#pblog:http://blog.etiantian.org
##########################################################
� 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 [email protected]
ServerName www.etiantian.org
ServerAlias etiantian.org
DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin [email protected]
ServerName www.etiantian.org
DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:8090>
ServerAdmin [email protected]
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/
如果能返回对应自己目录的内容,那恭喜你,搞定了。
本文出自 “老男孩linux运维” 博客,请务必保留此出处http://oldboy.blog.51cto.com/2561410/603627