——前期环境部署:
haproxy 192.168.64.129
nginx 192.168.64.129
client 192.168.64.128
(1.在server端口添加4个虚拟机 [haproxy和nginx共用一台服务器,不同端口]
[[email protected] ~]# 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 [[email protected] ~]# 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信息 [[email protected] ~]# tail -n2 /etc/hosts 192.168.64.129 www.example.com 192.168.64.129 bbs.example.com #访问测试域名 [[email protected] ~]# curl bbs.example.com:8001 [[email protected] ~]# curl bbs.example.com:8001 bbs.example.com:8001 [[email protected] ~]# curl www.example.com:8002 www.example.com:8002 [[email protected] ~]# 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
测试效果如下:
(4.基于后缀以css,js结尾的静态文件文件跳转
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
测试效果如下:
待补充...
时间: 2024-10-06 03:57:56