云中漫步 发表于 2019-1-2 10:16:34

基于haproxy

  ——前期环境部署:
  haproxy192.168.64.129
  nginx   192.168.64.129
  client192.168.64.128
  

  (1.在server端口添加4个虚拟机
# 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
# 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信息
# tail -n2 /etc/hosts
192.168.64.129www.example.com
192.168.64.129bbs.example.com
#访问测试域名
# curl bbs.example.com:8000
bbs.example.com:8000
# curl bbs.example.com:8001
bbs.example.com:8001
# curl www.example.com:8002
www.example.com:8002
# 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  测试效果如下:
http://s3.运维网.com/wyfs02/M02/70/43/wKioL1W1zjey-nMuAAF8i8YzLbw846.jpg
  (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  测试效果如下:
http://s3.运维网.com/wyfs02/M02/70/43/wKioL1W10PihKBN9AAFpGE-WSBo260.jpg
  (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_pathpath_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  测试效果如下:
http://s3.运维网.com/wyfs02/M01/70/43/wKioL1W104DxThisAAGjGMSfYZ4718.jpg(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_staticpath_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://s3.运维网.com/wyfs02/M01/70/47/wKioL1W13P3BEMGTAAOrNHRjOQY935.jpg
  待补充...



页: [1]
查看完整版本: 基于haproxy