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

简介: 这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于用户代理(User-Agent)即浏览器类型进行匹配和流量分发的高级配置选项,并通过实战案例展示了如何配置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服务器
AI 代码解读

2>.安装Apache httpd服务

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

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

1>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# 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 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy            #别忘记重启haproxy服务,重启后可以看到如下图所示的状态页。
[root@node102.yinzhengjie.org.cn ~]#
AI 代码解读

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

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

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

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

[root@node105.yinzhengjie.org.cn ~]# hostname -i
172.30.1.105
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 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
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
<h1>node106.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]#
AI 代码解读

使用curl命令模拟谷歌浏览器访问

[root@node105.yinzhengjie.org.cn ~]# hostname -i
172.30.1.105
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 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
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
<h2>node107.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]#
AI 代码解读

使用curl命令模拟火狐浏览器访问

[root@node105.yinzhengjie.org.cn ~]# hostname -i
172.30.1.105
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# grep 172.30.1.102 /etc/hosts
172.30.1.102 node102.yinzhengjie.org.cn pc.yinzhengjie.org.cn  mobile.yinzhengjie.org.cn  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 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
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
<h1>node108.yinzhengjie.org.cn</h1>
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]#
AI 代码解读

使用curl命令模拟IE浏览器访问

目录
打赏
0
4
5
1
172
分享
相关文章
服务器部署 Nacos 获取不到配置浏览器可以访问
[Nacos Config] config[dataId=ruoyi-auth.yml,group=DEFAULT_GROUP] is empty、Nacos无法注册、gRPC、端口偏移
300 0
服务器部署 Nacos 获取不到配置浏览器可以访问
一款免配置的浏览器编程工具jupyter,可以同时编写 python,java,c,c++,体积小
一款免配置的浏览器编程工具jupyter,可以同时编写 python,java,c,c++,体积小
235 1
Playwright 是一个强大的工具,用于在各种浏览器上测试应用,并模拟真实设备如手机和平板。通过配置 `playwright.devices`,可以轻松模拟不同设备的用户代理、屏幕尺寸、视口等特性。此外,Playwright 还支持模拟地理位置、区域设置、时区、权限(如通知)和配色方案,使测试更加全面和真实。例如,可以在配置文件中设置全局的区域设置和时区,然后在特定测试中进行覆盖。同时,还可以动态更改地理位置和媒体类型,以适应不同的测试需求。
Playwright 是一个强大的工具,用于在各种浏览器上测试应用,并模拟真实设备如手机和平板。通过配置 `playwright.devices`,可以轻松模拟不同设备的用户代理、屏幕尺寸、视口等特性。此外,Playwright 还支持模拟地理位置、区域设置、时区、权限(如通知)和配色方案,使测试更加全面和真实。例如,可以在配置文件中设置全局的区域设置和时区,然后在特定测试中进行覆盖。同时,还可以动态更改地理位置和媒体类型,以适应不同的测试需求。
257 1
Dolphin指纹浏览器+IPXProxy代理IP:配置与使用全流程
通过代理IP,用户可以轻松绕过地域限制,访问全球范围内的网站和服务。不过想要同时使用多个代理IP的话,就需要借助指纹浏览器。Dolphin指纹浏览器和IPXProxy代理IP是大家常用的组合,这个组合为用户打造了一个既安全又高效的在线环境。下面是Dolphin指纹浏览器配置IPXProxy代理IP使用步骤,帮助大家更好了解这个组合。
194 3
Dolphin指纹浏览器隐私保护升级:IPXProxy代理IP配置实战教程
Dolphin指纹浏览器采用先进的技术,让用户在一台电脑上就可以处理数百个配置文件。每一个配置文件都有着独特的浏览器指纹,极大的保障了用户上网的安全性。并且搭配代理IP一起,还能给每个文件配置不同的IP地址,让网络活动可以畅通无阻。下面给大家带来Dolphin指纹浏览器和IPXProxy代理IP配置详细教程
战斧指纹浏览器与IPXProxy海外代理IP配置详解
对于需要管理多个电商平台店铺的用户而言,战斧指纹浏览器提供了便捷的多账号隔离功能。跨境电商卖家要想在海外顺利的管理自己的店铺,还需要用到海外代理IP来实现IP隔离,确保店铺之间互相独立,不会受到其他店铺的牵连。下面以IPXProxy海外代理IP为例,给大家带来战斧指纹浏览器集成IPXProxy海外代理IP的详细指南。
227 0
如何配置战斧指纹浏览器和IPXProxy海外代理IP?
通过代理IP,用户可以轻松绕过地域限制,访问全球范围内的网站和服务。特别是对于跨境用户来说,需要在目标市场投放广告,而代理IP能帮助实现精准投放,快速的提升品牌或者店铺的知名度。那如何在如何在战斧指纹浏览器中设置IPXProxy海外代理IP?
134 0
JS案例:在浏览器实现自定义菜单
JS案例:在浏览器实现自定义菜单
73 0
nodejs配置express服务器,运行后自动打开浏览器
作为前端开发的项目,有的时候打包完后就想在本地测试是什么样子的,另外一些如cesium等程序,需要在服务的环境下才能启动三维球等。 这里使用nodejs+express搭建一个普通的服务器。
nodejs配置express服务器,运行后自动打开浏览器

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等