HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例

          HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.安装Apache Httpd及准备测试数据

1>.试验架构说明

  node102.yinzhengjie.org.cn:
    Haproxy服务器

  node105.yinzhengjie.org.cn:
    测试服务器,模拟客户端

  node106.yinzhengjie.org.cn:
    Apache httpd服务器

  node107.yinzhengjie.org.cn:
    Apache httpd服务器

  node108.yinzhengjie.org.cn:
    Apache httpd服务器

2>.安装Apache httpd服务

  此过程相对简单,我这里就直接略过了,可参考我之前的笔记:https://www.cnblogs.com/yinzhengjie/p/12114195.html

 

二.基于浏览器匹配制案例实战

1>.编辑haproxy的配置文件

[[email protected] ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

frontend WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    #定义ACL,匹配客户端浏览器的类型
    acl google_agent hdr(User-Agent) -m sub -i "Chrome"
    acl firefox_agent hdr(User-Agent) -m sub -i "Firefox"
    #调用ACL
    use_backend chrome_web if google_agent
    use_backend firefox_web if firefox_agent
    #如果前面的ACL都没有匹配成功就访问默认的ACL
    default_backend backup_web

backend chrome_web
    server web01 172.30.1.106:80  check inter 3000 fall 3 rise 5

backend firefox_web
    server web02 172.30.1.107:80  check inter 3000 fall 3 rise 5

backend backup_web
    server web03 172.30.1.108:80  check inter 3000 fall 3 rise 5 
[[email protected] ~]# 
[[email protected] ~]# systemctl restart haproxy            #别忘记重启haproxy服务,重启后可以看到如下图所示的状态页。
[[email protected] ~]# 

HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例_第1张图片

2>.使用谷歌浏览器访问"http://node102.yinzhengjie.org.cn/"

HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例_第2张图片

3>.使用火狐浏览器访问"http://node102.yinzhengjie.org.cn/"

HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例_第3张图片

4>.使用IE浏览器访问"http://node102.yinzhengjie.org.cn/"

HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例_第4张图片

5>.使用curl模拟各种浏览器访问"http://node102.yinzhengjie.org.cn/"

[[email protected] ~]# hostname -i
172.30.1.105
[[email protected] ~]# 
[[email protected] ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[[email protected] ~]# 
[[email protected] ~]# for _ in `seq 10`;do curl -A " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" http://node102.yinzhengjie.org.cn/;done

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

node106.yinzhengjie.org.cn

[[email protected] ~]# [[email protected] ~]#
使用curl命令模拟谷歌浏览器访问
[[email protected] ~]# hostname -i
172.30.1.105
[[email protected] ~]# 
[[email protected] ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[[email protected] ~]# 
[[email protected] ~]# for _ in `seq 10`;do curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" http://node102.yinzhengjie.org.cn/;done

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

node107.yinzhengjie.org.cn

[[email protected] ~]# [[email protected] ~]#
使用curl命令模拟火狐浏览器访问
[[email protected] ~]# hostname -i
172.30.1.105
[[email protected] ~]# 
[[email protected] ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[[email protected] ~]# 
[[email protected] ~]# for _ in `seq 10`;do curl -A "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" http://node102.yinzhengjie.org.cn/;done

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

node108.yinzhengjie.org.cn

[[email protected] ~]# [[email protected] ~]#
使用curl命令模拟IE浏览器访问

HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例_第5张图片

 

你可能感兴趣的:(HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例)