——前期环境部署:
haproxy 192.168.64.129
nginx 192.168.64.129
client 192.168.64.128
(1.在server端口添加4个虚拟机 [haproxy和nginx共用一台服务器,不同端口]
[root@haproxy ~]# tree -n /usr/local/nginx/conf/conf.d/ /usr/local/nginx/conf/conf.d/ ├── bbs.example.com.8000.conf ├── bbs.example.com.8001.conf ├── www.example.com.8002.conf └── www.example.com.8003.conf 0 directories, 4 files [root@haproxy ~]# tree -n /usr/local/nginx/html/ /usr/local/nginx/html/ ├── bbs_8000 │ ├── index.html │ ├── index.php │ └── www │ ├── example.css │ ├── example.html │ └── example.js ├── bbs_8001 │ ├── index.html │ ├── index.php │ └── www │ ├── example.css │ ├── example.html │ └── example.js ├── www_8002 │ ├── index.html │ ├── index.php │ └── www │ ├── example.css │ ├── example.html │ └── example.js └── www_8003 ├── index.html ├── index.php └── www ├── example.css ├── example.html └── example.js 8 directories, 20 files
(2.在client添加hosts信息,并访问测试域名
#添加hosts信息 [root@client ~]# tail -n2 /etc/hosts 192.168.64.129 www.example.com 192.168.64.129 bbs.example.com #访问测试域名 [root@client ~]# curl bbs.example.com:8001 [root@client ~]# curl bbs.example.com:8001 bbs.example.com:8001 [root@client ~]# curl www.example.com:8002 www.example.com:8002 [root@client ~]# curl www.example.com:8003 www.example.com:8003
——测试haproxy代理转发
(1.基于虚拟主机的转发 [实现效果:访问www.example.com将会访问192.168.64.129:8002和8003的内容]
frontend http_web bind 192.168.64.129:80 acl www_example_com hdr_beg(host) -i www.example.com use_backend www if www_example_com backend www balance roundrobin option httpchk HEAD /index.html server example_web01 192.168.64.129:8002 check inter 1500 rise 3 fall 3 weight 3 server example_web01 192.168.64.129:8003 check inter 1500 rise 3 fall 3 weight 3
测试效果如下:
(2.基于域名(301)跳转 [实现效果:访问www.example.com将会访问bbs.example.com的内容]
frontend http_web bind 192.168.64.129:80 acl www_example_com hdr_beg(host) -i www.example.com acl bbs_example_com hdr_beg(host) -i bbs.example.com use_backend bbs if www_example_com backend bbs balance roundrobin option httpchk HEAD /index.html server example_web01 192.168.64.129:8000 check inter 1500 rise 3 fall 3 weight 3 server example_web01 192.168.64.129:8001 check inter 1500 rise 3 fall 3 weight 3
测试效果如下:
(3.基于url目录跳转 [实现效果:访问www.example.com/www/examlpe.html将会访问bbs.example.com/www/examlpe.html的内容]
frontend http_web bind 192.168.64.129:80 acl www_example_com hdr_beg(host) -i www.example.com acl bbs_example_com hdr_beg(host) -i bbs.example.com acl www_path path_beg -i /www use_backend bbs_pool if bbs or www_path backend bbs_pool balance roundrobin option httpchk HEAD /index.html server example_web01 192.168.64.129:8000 check inter 1500 rise 3 fall 3 weight 3 server example_web01 192.168.64.129:8001 check inter 1500 rise 3 fall 3 weight 3
测试效果如下:
frontend http_web bind 192.168.64.129:80 acl www_example_com hdr_beg(host) -i www.example.com acl bbs_example_com hdr_beg(host) -i bbs.example.com acl www_static path_end -i .js .css use_backend bbs if www_static backend bbs balance roundrobin option httpchk HEAD /index.html server example_web01 192.168.64.129:8000 check inter 1500 rise 3 fall 3 weight 3 server example_web01 192.168.64.129:8001 check inter 1500 rise 3 fall 3 weight 3
测试效果如下:
待补充...
本文出自 “ˉ、穎濤┃﹎” 博客,请务必保留此出处http://hypocritical.blog.51cto.com/3388028/1678743
基于haproxy-1.5.12版本的http层负载均衡代理转发,附带测试效果
原文地址:http://hypocritical.blog.51cto.com/3388028/1678743