HAProxy的ACL

访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

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

hdr string,提取在一个HTTP请求报文的首部

hdr(\ \[,\]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出现次数

hdr_beg(\ \[,\]):前缀匹配,header中指定匹配内容的begin

hdr_end(\ \[,\]):后缀匹配,header中指定匹配内容end

hdr_dom(\ \[,\]):域匹配,header中的domain name

hdr_dir(\ \[,\]):路径匹配,header的uri路径

hdr_len(\ \[,\]):长度匹配,header的长度匹配

hdr_reg(\ \[,\]):正则表达式匹配,自定义表达式(regex)模糊匹配

hdr_sub(\ \[,\]):子串匹配,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匹配模式

复制代码
-i 不区分大小写
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名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

ACL示例-域名匹配

#cat /etc/haproxy/conf.d/test.cfg

frontend openlab_web

bind :80

mode http

balance roundrobin

log global

option httplog

###################### acl setting ###############################

acl pc_domain hdr_dom(host) -i www.yunjisuan.com

acl mobile_domain hdr_dom(host) -i mobile.yunjisuan.com

###################### acl hosts #################################

use_backend pc_hosts if pc_domain

use_backend mobile_hosts if mobile_domain

default_backend pc_hosts

###################### backend hosts #############################

backend mobile_hosts

mode http

server web1 192.168.234.13:80 check inter 2000 fall 3 rise 5

backend pc_hosts

mode http

server web2 192.168.234.11:80 check inter 2000 fall 3 rise 5

root@centos6 \~#curl www.yunjisuan.com

192.168.234.11

root@centos6 \~#curl mobile.yunjisuan.com

192.168.234.13

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

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

测试:

listen web_host

bind :80

mode http

balance roundrobin

log global

option httplog

###################### acl setting ###############################

acl acl_deny_src src 192.168.234.0/24

###################### acl hosts #################################

#block if acl_deny_src #2.1版本后,不再支持block

http-request deny if acl_deny_src

#http-request allow

default_backend default_web

###################### backend hosts #############################

backend magedu_host

mode http

server web1 192.168.234.11:80 check inter 2000 fall 3 rise 5

backend default_web

mode http

server web1 192.168.234.13:80 check inter 2000 fall 3 rise 5

root@centos6 \~#curl www.yunjisuan.com

<html><body><h1>403 Forbidden</h1>Request forbidden by administrative rules.</body</html>

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

复制代码
#cat /etc/haproxy/conf.d/test.cfg

frontend magedu_http_port

bind :80

mode http

balance roundrobin

log global

option httplog

###################### acl setting ###############################

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

acl acl_php path_end -i .php

###################### acl hosts #################################

use_backend mobile_hosts if acl_static

use_backend app_hosts if acl_php

###################### backend hosts #############################

backend mobile_hosts

mode http

server web1 192.168.234.13:80 check inter 2000 fall 3 rise 5

backend app_hosts

mode http

server web2 192.168.234.11:80 check inter 2000 fall 3 rise 5

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

root@centos17 \~#ls /usr/share/nginx/html

index.html wang.jpg

root@centos27 \~#cat /usr/share/nginx/html/test.php

<?php phpinfo(); ?>

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

复制代码
#cat /etc/haproxy/conf.d/test.cfg

frontend magedu_http_port

bind :80

mode http

balance roundrobin

log global

option httplog

###################### acl setting ###############################

acl acl_static path_beg -i /static /images /javascript

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

###################### acl hosts #################################

use_backend static_hosts if acl_static

default_backend app_hosts

###################### backend hosts #############################

backend static_hosts

mode http

server web1 192.168.234.13:80 check inter 2000 fall 3 rise 5

backend app_hosts

mode http

server web2 192.168.234.11:80 check inter 2000 fall 3 rise 5

#创建相关文件

root@centos17 \~#mkdir /usr/share/nginx/html/static

root@centos17 \~#echo 192.168.234.13 > /usr/share/nginx/html/static/test.html

#测试访问

root@centos6 \~#curl 192.168.234.13/static/test.html

192.168.234.13

相关推荐
蒸蒸yyyyzwd5 小时前
cpp选手秋招学习笔记day24-cs144项目总结
运维·服务器·网络
ang7846 小时前
日常高频使用的 Ansible 命令,按使用频率和场景整理。
linux·运维·服务器
翼龙云_cloud6 小时前
腾讯云国际代理商:如何用云服务器快速部署WorkBuddy AI助手?
服务器·人工智能·云计算·腾讯云
数据知道6 小时前
静态分析入门——PE 结构、字符串、导入表快速定性
网络·安全·web安全·网络安全
CCYe、6 小时前
新模型上线、旧模型下线:企业AI网关如何管住模型版本
java·网络·数据库·人工智能
葡萄城技术团队6 小时前
从设备协议数据到完整OEE:机加工车间设备效率分析的技术实现路径
网络
赵庆明老师6 小时前
用同一个用户登录宝塔面板的ftp与Linux的sftp
linux·运维·服务器
山君爱摸鱼7 小时前
堡垒机远程服务器故障收集
运维·服务器
码农小麦7 小时前
LangChain 1.3.18 + DeepSeek-v4-flash 工具调用踩坑实录
网络·数据库·langchain
不会就选b7 小时前
Linux之socket编程(六)----TCP-->模拟shell命令
linux·运维·tcp/ip