hapxory-ACL基础介绍及案例

1.简介

访问控制列表(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

2.用法

ACL配置选项

acl   <aclname>  <criterion>   [flags]     [operator]    [<value>]
acl      名称      匹配规范      匹配模式     具体操作符     操作对象类型
ACL-Name
acl   image_service hdr_dom(host)   -i   img.magedu.com

#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如Image_site和image_site就是两个完全不同的acl
ACL-criterion

定义ACL匹配规范,即:判断条件

hdr string,提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出现次数
hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name
hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配

#示例:
hdr(<string>)     用于测试请求头部首部指定内容
hdr_dom(host)   请求的host名称,如 www.magedu.com
hdr_beg(host)   请求的host开头,如 www.   img.   video.   download.   ftp.
hdr_end(host)   请求的host结尾,如 .com   .net   .cn 

#示例:
acl bad_agent hdr_sub(User-Agent) -i curl wget
block if bad_agent

#有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www:
acl short_form  hdr_beg(host)        www.
acl alternate1  hdr_beg(host) -m beg www.
acl alternate2  hdr_dom(host) -m beg www.
acl alternate3  hdr(host)     -m beg www.

base : string
#返回第一个主机头和请求的路径部分的连接,该请求从第一个斜杠开始,并在问号之前结束,对虚拟主机有用
<scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
    base     : exact string match
    base_beg : prefix match
    base_dir : subdir match
    base_dom : domain match
    base_end : suffix match
    base_len : length match
    base_reg : regex match
    base_sub : substring match

path : string
#提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
<scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
    path     : exact string match
    path_beg : prefix match  #请求的URL开头,如/static、/images、/img、/css
    path_end : suffix match  #请求的URL中资源的结尾,如 .gif  .png  .css  .js  .jpg  .jpeg
    path_dom : domain match
    path_dir : subdir match
    path_len : length match
    path_reg : regex match
    path_sub : substring match

#示例:
    path_beg -i /haproxy-status/ 
    path_end .jpg .jpeg .png .gif 
    path_reg ^/images.*\.jpeg$ 
    path_sub image  
    path_dir jpegs 
    path_dom magedu

url : string
#提取请求中的URL。一个典型的应用是具有预取能力的缓存,以及需要从数据库聚合多个信息并将它们保存在缓存中的网页门户入口,推荐使用path
    url  :exact string match
    url_beg : prefix match
    url_dir : subdir match
    url_dom : domain match
    url_end : suffix match
    url_len : length match
    url_reg : regex match
    url_sub : substring match

dst         #目标IP
dst_port    #目标PORT

src         #源IP
src_port    #源PORT

#示例:
acl invalid_src src 10.0.0.100 192.168.1.0/24
acl invalid_port src_port 0:1023

status : integer
#返回在响应报文中的状态码   

#七层协议
acl valid_method method GET HEAD
http-request deny if ! valid_method
ACL-flags

ACL匹配模式

-i 不区分大小写
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名ACL匹配或关系
ACL-operator

ACL 操作符

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

value的类型

The ACL engine can match these types against patterns of the following types :
- Boolean                       #布尔值
- integer or integer range      #整数或整数范围,比如用于匹配端口范围
- IP address / network          #IP地址或IP范围, 192.168.0.1 ,192.168.0.1/24
- string--> www.magedu.com
  exact --精确比较
  substring---子串
  suffix-后缀比较
  prefix-前缀比较
  subdir-路径, /wp-includes/js/jquery/jquery.js
  domain-域名,www.magedu.com
- regular expression            #正则表达式
- hex block                     #16进制

ACL调用方式

ACL调用方式:

与:隐式(默认)使用
或:使用"or" 或 "||"表示
否定:使用 "!" 表示  

#示例:
if valid_src valid_port         #与关系,A和B都要满足为true,默认为与
if invalid_src || invalid_port  #或,A或者B满足一个为true
if ! invalid_src                #非,取反,A和B哪个也不满足为true

3.案例

环境准备:

准备以下四台主机:

|-------------|-----------------|-------------|
| 主机名 | IP | 角色 |
| open-Euler1 | 192.168.121.150 | web-server1 |
| openEuler-2 | 192.168.121.151 | web-server2 |
| openEuler-3 | 192.168.121.152 | client |
| Rocky8 | 192.168.121.160 | haproxy |

两台web服务器安装nginx并在测试页面添加内容:

bash 复制代码
[root@open-Euler1 ~]# echo "pc `hostname -I`" > /usr/share/nginx/html/index.html
[root@open-Euler2 ~]# echo "mobile `hostname -I`" > /usr/share/nginx/html/index.html

ACL示例-域名匹配

首先配置Rocky8和测试端openEuler-3 的域名解析

Rocky8:

bash 复制代码
[root@Rocky8 conf.d]# vim /etc/hosts
192.168.121.150 www.pc.com
192.168.121.151 www.mobile.com

open_Euler3:

bash 复制代码
[root@open-Euler3 ~]# vim /etc/hosts
192.168.121.160 www.pc.com www.mobile.com

然后配置haproxy的acl规则

bash 复制代码
[root@Rocky8 conf.d]# vim /etc/haproxy/conf.d/web.cfg 

frontend http_port
  bind :80
  mode http
  balance roundrobin
  log global
  option httplog

  acl pc_domain hdr_dom(host) -i www.pc.com
  acl mobile_domain hdr_dom(host) -i www.mobile.com

  use_backend  pc_hosts         if   pc_domain
  use_backend  mobile_hosts     if   mobile_domain
  default_backend pc_hosts


backend pc_hosts
  mode http
  server web1 192.168.121.150:80 check inter 2000 fall 3 rise 5


backend mobile_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

测试:

ACL示例-基于源地址的访问控制

拒绝指定IP或者IP范围访问

bash 复制代码
[root@Rocky8 conf.d]# cat /etc/haproxy/conf.d/web.cfg 
frontend http_port
  bind :80
  mode http
  balance roundrobin
  log global
  option httplog

  acl acl_deny_src src 192.168.121.0/24

  http-request deny  if acl_deny_src 
  
backend pc_hosts
  mode http
  server web1 192.168.121.150:80 check inter 2000 fall 3 rise 5


backend mobile_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

测试:

ACL示例-匹配浏览器类型

匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组

open-Euler3下载nginx并在测试页面添加内容:

bash 复制代码
echo "redirect `hostname -I`" > /usr/share/nginx/html/index.html

Rocky8:

bash 复制代码
[root@Rocky8 conf.d]# cat /etc/haproxy/conf.d/web.cfg 
frontend http_port
  bind :80
  mode http
  balance roundrobin
  log global
  option httplog

  acl acl_user_agent    hdr_sub(User-Agent)  -i curl wget
  acl acl_user_agent_ab hdr_sub(User-Agent)  -i ApacheBench

  http-request deny  if acl_user_agent_ab            #拒绝ab
  redirect prefix   http://192.168.121.152 if acl_user_agent        #301临时重定向至新URL

backend pc_hosts
  mode http
  server web1 192.168.121.150:80 check inter 2000 fall 3 rise 5


backend mobile_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

  
  

测试:

ACL示例-基于文件后缀名实现动静分离

bash 复制代码
[root@Rocky8 conf.d]# cat /etc/haproxy/conf.d/web.cfg 
frontend http_port
  bind :80
  mode http
  balance roundrobin
  log global
  option httplog

  acl acl_static path_end -i .jpg .jpeg .png .gif .css .js
  acl acl_php   path_end -i .php 

  use_backend  mobile_hosts if acl_static
  use_backend  app_hosts if acl_php
  default_backend pc_hosts 

backend pc_hosts
  mode http
  server web1 192.168.121.150:80 check inter 2000 fall 3 rise 5


backend mobile_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

backend app_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

  

分别在后端两台主机准备相关文件

open-Euler2的/usr/share/nginx/html下添加两个文件:

1.以 .jpg .jpeg .png .gif .css .js为后缀的文件一个

2.以.php为后缀的文件一个

然后可以直接在真实主机的浏览器访问haproxy的ip+文件名即可通过haproxy来访问不同服务器的动静态资源实现动静分离

ACL-匹配访问路径实现动静分离

bash 复制代码
[root@Rocky8 conf.d]# cat /etc/haproxy/conf.d/web.cfg 
frontend http_port
  bind :80
  mode http
  balance roundrobin
  log global
  option httplog
  
  acl  acl_static  path_beg  -i  /static /images /javascript
  acl  acl_static  path_end  -i .jpg .jpeg .png .gif .css .js
  
  use_backend  pc_hosts if acl_static

backend pc_hosts
  mode http
  server web1 192.168.121.150:80 check inter 2000 fall 3 rise 5


backend mobile_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

backend app_hosts
  mode http
  server web2 192.168.121.151:80 check inter 2000 fall 3 rise 5

创建相关文件

bash 复制代码
[root@localhost html]# mkdir ./static ./images ./javascript
[root@localhost html]# ll
total 28
-rw-r--r-- 1 root root 3454 Jun 23  2024 404.html
-rw-r--r-- 1 root root 3497 Jun 23  2024 50x.html
drwxr-xr-x 2 root root 4096 Feb 15 11:46 images
-rw-r--r-- 1 root root   19 Feb 15 10:42 index.html
drwxr-xr-x 2 root root 4096 Feb 15 11:46 javascript
-rw-r--r-- 1 root root  368 Jun 23  2024 nginx-logo.png
drwxr-xr-x 2 root root 4096 Feb 15 11:46 static
[root@localhost html]# cp nginx-logo.png ./images/
[root@localhost html]# ll ./images/
total 4
-rw-r--r-- 1 root root 368 Feb 15 11:52 nginx-logo.png
[root@localhost html]# systemctl restart nginx

测试:

相关推荐
sszdzq19 分钟前
Docker
运维·docker·容器
book012123 分钟前
MySql数据库运维学习笔记
运维·数据库·mysql
bugtraq20211 小时前
XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux
linux·运维·ubuntu
xmweisi1 小时前
【华为】报文统计的技术NetStream
运维·服务器·网络·华为认证
VVVVWeiYee2 小时前
BGP配置华为——路径优选验证
运维·网络·华为·信息与通信
陆鳐LuLu2 小时前
日志管理利器:基于 ELK 的日志收集、存储与可视化实战
运维·elk·jenkins
DC_BLOG2 小时前
Linux-GlusterFS进阶分布式卷
linux·运维·服务器·分布式
yourkin6662 小时前
TCP...
服务器·网络·tcp/ip
cookies_s_s3 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
zhouwu_linux3 小时前
MT7628基于原厂的SDK包, 修改ra1网卡的MAC方法。
linux·运维·macos