【HAProxy11】企业级反向代理HAProxy高级功能之访问控制列表(ACL)

[复制链接]
发表于 2024-11-24 11:09:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
HAProxy 高级功能

介绍 HAProxy 高级设置及实用案例
ACL

访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条 件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头 部中的源地点、源端口、目标地点、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行 进一步操作,好比允许其通过或丢弃。
官方资助:
   http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#7
http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7
  定义ACL设置选项

   acl   <aclname>  <criterion>   [flags]     [operator]    [<value>]
acl     名称        匹配规范      匹配模式      详细操作符      操作对象类型
  ACL-Name


  1. acl   image_service   hdr_dom(host)   -i     img.wang.com
  2. #ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如:my_acl和My_Acl就是两个完全不同的acl
复制代码

ACL-criterion

定义ACL匹配规范,即:判断条件
  1. hdr string,提取在一个HTTP请求报文的首部
  2. hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出
  3. 现次数
  4. hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
  5. hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
  6. hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name
  7. hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
  8. hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
  9. hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
  10. hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配
  11. #示例:
  12. hdr(<string>)   用于测试请求头部首部指定内容
  13. hdr_dom(host)   请求的host名称,如 www.wang.com,m.wang.com
  14. hdr_beg(host)   请求的host开头,如 www.   img.   video.   download.   ftp.
  15. hdr_end(host)   请求的host结尾,如 .com   .net   .cn
  16. #示例:
  17. acl bad_agent hdr_sub(User-Agent) -i curl wget
  18. http-request deny  if bad_agent
  19. #block if bad_agent 2.1版本后不再支持,用上面替代
  20. hdr(host) ==>www.wang.org:8080
  21. hdr_domain(host) ==> www.wang.org
  22. #有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www
  23. acl short_form  hdr_beg(host)        www.
  24. acl alternate1  hdr_beg(host) -m beg www.
  25. acl alternate2  hdr_dom(host) -m beg www.
  26. acl alternate3  hdr(host)     -m beg www.
  27. base : string
  28. #返回第一个主机头和请求的路径部分的连接,该请求从主机名开始,并在问号之前结束,对虚拟主机有用,下面的例子中是两个#中间的内容,实际#是没有的
  29. <scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
  30.      base     : exact string match
  31.      base_beg : prefix match
  32.      base_dir : subdir match
  33.      base_dom : domain match
  34.      base_end : suffix match
  35.      base_len : length match
  36.      base_reg : regex match
  37.      base_sub : substring match
  38. path : string
  39. #提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
  40. <scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
  41.      path     : exact string match
  42.      path_beg : prefix match  #请求的URL开头,如/static、/images、/img、/css
  43.      path_end : suffix match  #请求的URL中资源的结尾,如 .gif  .png  .css  .js  .jpg  .jpeg
  44.      path_dom : domain match
  45.      path_dir : subdir match
  46.      path_len : length match
  47.      path_reg : regex match
  48.      path_sub : substring match
  49. #示例:
  50.     path_beg -i /haproxy-status/   
  51.     path_end .jpg .jpeg .png .gif
  52.     path_reg ^/images.*\.jpeg$
  53.     path_sub image  
  54.     path_dir jpegs
  55.     path_dom www.wang.org
  56.    
  57. url : string
  58. #提取请求中的整个URL。一个典型的应用是具有预取能力的缓存,以及需要从数据库聚合多个信息并将它们保存在缓存中的网页门户入口,推荐使用path
  59. url        :exact string match  
  60. url_beg : prefix match
  61. url_dir : subdir match
  62. url_dom : domain match
  63. url_end : suffix match
  64. url_len : length match
  65. url_reg : regex match
  66. url_sub : substring match
  67. src         #源IP
  68. src_port     #源PORT
  69. dst          #目标IP
  70. dst_port    #目标PORT
  71. #示例:
  72. acl invalid_src src 10.0.0.7 192.168.1.0/24
  73. acl invalid_src src 172.16.0.0/24
  74. http-request deny if ! invalid_src
  75. acl invalid_port src_port 0:1023
  76. status : integer  #返回在响应报文中的状态码
  77. #七层协议
  78. acl valid_method method GET HEAD
  79. http-request deny if ! valid_method
复制代码

ACL-flags

ACL匹配模式
  1. -i 不区分大小写
  2. -m 使用指定的pattern匹配方法,beg 开头,end 结尾
  3. -n 不做DNS解析
  4. -u 禁止acl重名,否则多个同名
复制代码
ACL-operator

ACL 操作符
  1. 整数比较:eq、ge、gt、le、lt
  2. 字符比较:
  3. - exact match     (-m str) :字符串必须完全匹配模式
  4. - substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配
  5. - prefix match    (-m beg) :在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配
  6. - suffix match    (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配
  7. - subdir match    (-m dir) :查看提取出来的用斜线分隔(“/")的字符串,如其中任一个匹配,则ACL进行匹配
  8. - domain match    (-m dom) :查找提取的用点(“.")分隔字符串,如果其中任何一个匹配,则ACL进行匹配       
复制代码
ACL-value

value的类型
  1. The ACL engine can match these types against patterns of the following types :
  2. - Boolean                       #布尔值
  3. - integer or integer range      #整数或整数范围,比如用于匹配端口范围
  4. - IP address / network          #IP地址或IP范围, 192.168.0.1 ,192.168.0.1/24
  5. - string--> www.wang.com
  6.   exact     #精确比较
  7.   substring #子串
  8.   suffix    #后缀比较
  9.   prefix    #前缀比较
  10.   subdir    #路径, /wp-includes/js/jquery/jquery.js
  11.   domain    #域名,www.wang.com
  12. - regular expression      #正则表达式      
  13. - hex block               #16进制      
复制代码
多个ACL的组合调用方式

多个ACL的逻辑处置处罚

   与:隐式(默认)使用
或:使用“or" 或 “||"表示
否定:使用 "!" 表示 
  多个ACL调用方式:

   #示例:
if valid_src valid_port         #与关系,ACL中A和B都要满足为true,默认为与
if invalid_src || invalid_port  #或,ACL中A大概B满足一个为true
if ! invalid_src                #非,取反,不满足ACL才为true 
  ACL示例:域名匹配

  1. [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
  2. frontend  wang_http_port
  3.   bind 10.0.0.7:80
  4.   mode http
  5.   balance  roundrobin
  6.   log global
  7.   option httplog
  8. ###################### acl setting ###############################
  9.   acl pc_domain  hdr_dom(host)      -i www.wang.org
  10.   acl mobile_domain hdr_dom(host)   -i mobile.wang.org
  11. ###################### acl hosts #################################
  12.   use_backend  pc_hosts         if    pc_domain
  13.   use_backend  mobile_hosts     if    mobile_domain
  14.   default_backend pc_hosts       #所有ACL都不匹配,则使用的默认backend
  15. ###################### backend hosts #############################
  16. backend mobile_hosts
  17.   mode http
  18.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  19. backend pc_hosts
  20.   mode http
  21.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码
 测试结果:
  1. [root@centos6 ~]#cat /etc/hosts
  2. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 centos6.localdomain
  3. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 10.0.0.7 mobile.wang.org  www.wang.org wang.org
  5. [root@centos6 ~]#curl www.wang.org
  6. 10.0.0.27
  7. [root@centos6 ~]#curl mobile.wang.org
  8. 10.0.0.17
  9. [root@centos6 ~]#curl wang.org
  10. 10.0.0.27
复制代码
ACL示例:基于源IP或子网调理访问

将指定的源地点调理至指定的web服务器组。
  1. root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
  2. frontend  wang_http_port
  3.   bind 10.0.0.7:80
  4.   mode http
  5.   balance  roundrobin
  6.   log global
  7.   option httplog
  8. ###################### acl setting ###############################
  9.   acl pc_domain  hdr_dom(host)      -i www.wang.org
  10.   acl mobile_domain hdr_dom(host)   -i mobile.wang.org
  11.   acl ip_range_test src 172.18.0.0/16 10.0.0.6     #基于源地址的ACL,定义多个ACL的顺序无关
  12.   acl ip_range_test2 src 172.18.0.200
  13. ###################### acl hosts #################################
  14.   use_backend  pc_hosts        if   ip_range_test #放在前面的ACL规则优先生效,引用ACL时,严格的ACL应放在前面
  15.   use_backend  pc_hosts        if   pc_domain
  16.   use_backend  mobile_hosts    if   mobile_domain
  17.   default_backend pc_hosts
  18.       
  19. ###################### backend hosts #############################
  20. backend mobile_hosts
  21.   mode http
  22.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  23. backend pc_hosts
  24.   mode http
  25.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码

测试结果
  1. [root@centos6 ~]#hostname -I
  2. 10.0.0.6
  3. [root@centos6 ~]#curl www.wang.org
  4. 10.0.0.27
  5. [root@internet ~]#curl -H "HOST: www.wang.org" 10.0.0.7       
  6. 10.0.0.27
  7. [root@centos6 ~]#curl mobile.wang.org
  8. 10.0.0.27
  9. [root@centos6 ~]#curl wang.org
  10. 10.0.0.27
  11. [root@centos8 ~]#curl mobile.wang.org
  12. 10.0.0.17
  13. [root@centos8 ~]#curl www.wang.org
  14. 10.0.0.27
  15. [root@centos8 ~]#curl wang.org
  16. 10.0.0.27
复制代码

ACL示例:基于源地点的访问控制

  1. listen  web_host
  2.   bind 10.0.0.7:80
  3.   mode http
  4.   balance  roundrobin
  5.   log global
  6.   option httplog
  7. ###################### acl setting ###############################
  8.   acl acl_deny_src src 10.0.0.6 192.168.0.0/24
  9. #acl lan  src      10.0.0.0/24
  10. ###################### acl hosts #################################
  11.   http-request deny  if  acl_deny_src  
  12.     #http-request deny  if ! lan         #取反
  13.     #block  if  acl_deny_src            #2.1版本后,不再支持block
  14.    
  15.     #http-request allow
  16.     default_backend default_web
  17. ###################### backend hosts #############################
  18. backend wang_host
  19.   mode http
  20.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  21. backend default_web
  22.   mode http
  23.   server web1 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码
测试:
  1. [root@centos6 ~]#curl www.wang.org
  2. <html><body><h1>403 Forbidden</h1>
  3. Request forbidden by administrative rules.
  4. </body></html>
复制代码
ACL示例:匹配浏览器类型

匹配客户端浏览器,将差异类型的浏览器调动至差异的服务器
范例: 163 网易拒绝curl和wget的访问
  1. [root@ubuntu2004 ~]#curl -I www.163.com
  2. HTTP/1.1 403 Forbidden
  3. Date: Fri, 01 Jan 2021 06:57:22 GMT
  4. Content-Type: text/html
  5. Content-Length: 237
  6. Connection: keep-alive
  7. Server: web cache
  8. Expires: Fri, 01 Jan 2021 06:57:22 GMT
  9. X-Ser: BC17_lt-shandong-zaozhuang-6-cache-1
  10. Cache-Control: no-cache,no-store,private
  11. cdn-user-ip: 111.199.184.218
  12. cdn-ip: 124.132.138.17
  13. X-Cache-Remote: HIT
  14. cdn-source: baishan
  15. [root@ubuntu2004 ~]#curl -I -A "wget" www.163.com
  16. HTTP/1.1 403 Forbidden
  17. Date: Fri, 01 Jan 2021 06:58:13 GMT
  18. Content-Type: text/html
  19. Content-Length: 237
  20. Connection: keep-alive
  21. Server: web cache
  22. Expires: Fri, 01 Jan 2021 06:58:13 GMT
  23. X-Ser: BC17_lt-shandong-zaozhuang-6-cache-1
  24. Cache-Control: no-cache,no-store,private
  25. cdn-user-ip: 111.199.184.218
  26. cdn-ip: 124.132.138.17
  27. X-Cache-Remote: HIT
  28. cdn-source: baishan
  29. #不拒绝其它浏览器的访问
  30. [root@ubuntu2004 ~]#curl -I -A "ApacheBench" www.163.com
  31. HTTP/1.1 301 Moved Permanently
  32. Date: Fri, 01 Jan 2021 06:58:41 GMT
  33. Content-Length: 0
  34. Connection: keep-alive
  35. Server: web cache
  36. Location: https://www.163.com/
  37. Cache-Control: no-cache,no-store,private
  38. cdn-user-ip: 111.199.184.218
  39. cdn-ip: 124.132.138.11
  40. X-Cache-Remote: MISS
  41. cdn-source: baishan
复制代码
范例:
  1. [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
  2. frontend  wang_http_port
  3.   bind 10.0.0.7:80
  4.   mode http
  5.   balance  roundrobin
  6.   log global
  7.   option httplog
  8. ###################### acl setting ###############################
  9.   acl acl_user_agent    hdr_sub(User-Agent)  -i curl wget        #基于浏览器的ACL
  10.   acl acl_user_agent_ab hdr_sub(User-Agent)  -i ApacheBench
  11. ###################### acl hosts #################################
  12.   redirect prefix   http://www.baidu.com if acl_user_agent          #302 临时重定向至新URL
  13.   http-request deny                      if acl_user_agent_ab   #拒绝ab        
  14.   default_backend pc_hosts
  15. ###################### backend hosts #############################
  16. backend mobile_hosts
  17.   mode http
  18.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  19. backend pc_hosts
  20.   mode http
  21.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码
范例:
  1. [root@centos6 ~]#curl -I 10.0.0.7
  2. HTTP/1.1 302 Found
  3. content-length: 0
  4. location: http://10.0.0.8/
  5. cache-control: no-cache
  6. [root@centos6 ~]#curl -L 10.0.0.7
  7. 10.0.0.8
  8. [root@centos6 ~]#wget -O -  -q http://10.0.0.7
  9. 10.0.0.8
  10. [root@centos6 ~]#curl -A chrome http://10.0.0.7
  11. 10.0.0.27
  12. #模拟ab
  13. [root@centos6 ~]#curl -A ApacheBench 10.0.0.7
  14. <html><body><h1>403 Forbidden</h1>
  15. Request forbidden by administrative rules.
  16. </body></html>
  17. [root@centos6 ~]#ab  -n1 -c 1 http://10.0.0.7/
  18. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  19. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  20. Licensed to The Apache Software Foundation, http://www.apache.org/
  21. Benchmarking 10.0.0.7 (be patient).....done
复制代码
ACL示例:基于文件后缀名实现动静分离


  1. [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
  2. frontend  wang_http_port
  3.   bind 10.0.0.7:80
  4.   mode http
  5.   balance  roundrobin
  6.   log global
  7.   option httplog
  8. ###################### acl setting ###############################
  9.   acl acl_static path_end -i  .jpg .jpeg .png .gif .css .js .html  #基于文件后缀名的ACL
  10.   acl acl_php    path_end -i  .php
  11. ###################### acl hosts #################################
  12.   use_backend  mobile_hosts if acl_static
  13.   use_backend  app_hosts if acl_php
  14.   default_backend pc_hosts
  15. ###################### backend hosts #############################
  16. backend mobile_hosts
  17.   mode http
  18.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  19. backend pc_hosts
  20.   mode http
  21.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
  22. backend app_hosts
  23.   mode http
  24.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
  25. #分别在后端两台主机准备相关文件
  26. [root@centos17 ~]#ls /var/www/html
  27. index.html  wang.jpg
  28. [root@centos27 ~]#cat /var/www/html/test.php
  29. <?php
  30. echo "<h1>http://10.0.0.27/test.php</h1>\n";
  31. ?>       
复制代码
ACL示例:匹配访问路径实现动静分离

  1. [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
  2. frontend  wang_http_port
  3.   bind 10.0.0.7:80
  4.   mode http
  5.   balance  roundrobin
  6.   log global
  7.   option httplog
  8. ###################### acl setting ###############################
  9.   acl  acl_static  path_beg  -i  /static /images /javascript        #基于路径的ACL
  10.   acl  acl_static  path_end  -i .jpg .jpeg .png .gif .css .js .html .htm  #ACL同名为或关系
  11.   acl  acl_app  path_beg  -i  /api
  12. ###################### acl hosts #################################
  13.   use_backend static_hosts if acl_static
  14.   use_backend app_hosts    if acl_app
  15.   default_backend app_hosts
  16. ###################### backend hosts #############################
  17. backend static_hosts
  18.   mode http
  19.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  20. backend app_hosts
  21.   mode http
  22.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
  23. #创建相关文件
  24. [root@centos17 ~]#mkdir /var/www/html/static
  25. [root@centos17 ~]#echo 10.0.0.17 >  /var/www/html/static/test.html
  26. #测试访问
  27. [root@centos6 ~]#curl 10.0.0.7/static/test.html
  28. 10.0.0.17
复制代码
ACL示例:预定义ACL使用

官方资助文档: HAProxy version 2.1.12 - Configuration Manual
预定义ACL使用

范例: 禁止TRACE方法和HTTP/1.1协议

  1. [root@centos6 ~]#curl -I -XTRACE 10.0.0.7/static/test.html
  2. HTTP/1.1 200 OK
  3. date: Sat, 04 Apr 2020 02:04:01 GMT
  4. server: Apache/2.4.6 (CentOS) PHP/5.4.16
  5. transfer-encoding: chunked
  6. content-type: message/http
  7. [root@centos7 ~]#cat  /etc/haproxy/conf.d/test.cfg
  8. frontend  wang_http_port
  9.   bind 10.0.0.7:80
  10.   mode http
  11.   balance  roundrobin
  12. log global
  13.   option httplog
  14. ###################### acl setting ###############################
  15.   acl  acl_static_path  path_beg  -i  /static /images /javascript
  16. ###################### acl hosts #################################
  17.   use_backend static_path_hosts if acl_static_path
  18.   http-request deny if METH_TRACE  HTTP_1.1  #引用预定义的ACL,多个ACL默认为与关系
  19.   default_backend pc_hosts
  20. ################### backend hosts ################################
  21. backend static_path_hosts
  22.   mode http
  23.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  24. backend mobile_hosts
  25.   mode http
  26.   server web1 10.0.0.17 check inter 2000 fall 3 rise 5
  27. backend pc_hosts
  28.   mode http
  29.   server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
  30. [root@centos6 ~]#curl -I -XTRACE 10.0.0.7/static/test.html
  31. HTTP/1.1 403 Forbidden
  32. content-length: 93
  33. cache-control: no-cache
  34. content-type: text/html
  35. connection: close
  36. [root@centos6 ~]#curl -I -0 -XTRACE 10.0.0.7/static/test.html
  37. HTTP/1.1 200 OK
  38. date: Sat, 04 Apr 2020 02:10:13 GMT
  39. server: Apache/2.4.6 (CentOS) PHP/5.4.16
  40. content-type: message/http
  41. connection: close
  42. #查看日志日志,观察协议版本
  43. [root@centos17 ~]#tail /var/log/httpd/access_log
  44. 10.0.0.7 - - [04/Apr/2020:10:11:41 +0800] "TRACE /static/test.html HTTP/1.0" 200
  45. 230 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1
  46. zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
  47. [root@centos6 ~]#curl  -i 10.0.0.7/static/test.html
  48. HTTP/1.1 200 OK
  49. date: Sat, 04 Apr 2020 02:07:58 GMT
  50. server: Apache/2.4.6 (CentOS) PHP/5.4.16
  51. last-modified: Sat, 04 Apr 2020 01:27:45 GMT
  52. etag: "a-5a26cf0ed4913"
  53. accept-ranges: bytes
  54. content-length: 10
  55. content-type: text/html; charset=UTF-8
  56. 10.0.0.17
复制代码










免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5

GMT+8, 2025-7-12 05:49 , Processed in 0.256487 second(s), 32 queries 手机版|qidao123.com技术社区-IT企服评测▪应用市场 ( 浙ICP备20004199 )|网站地图

快速回复 返回顶部 返回列表