目录
一、ACL配置选项
#用acl来定义或声明一个acl
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
二、ACL-Name 名称
acl test path_end -m sub /a
#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大 小写,比如:my_acl和My_Acl就是两个完全不同的acl5.8.1.2 ACL-criterion
三、ACL-criterion 匹配规范
hdr string,提取在一个HTTP请求报文的首部
hdr([ [,]]):完全匹配字符串,header的指定信息, 表示在多值中使用的值的出 现次数 hdr_beg([ [,]]):前缀匹配,header中指定匹配内容的begin
hdr_end([ [,]]):后缀匹配,header中指定匹配内容end
hdr_dom([ [,]]):域匹配,header中的dom(host)
hdr_dir([ [,]]):路径匹配,header的uri路径
hdr_len([ [,]]):长度匹配,header的长度匹配
hdr_reg([ [,]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([ [,]]):子串匹配,header中的uri模糊匹配 模糊匹配c 报文中a/b/c也会匹 配
#示例:
hdr() 用于测试请求头部首部指定内容
hdr_dom(host) 请求的host名称,如 www.timinglee.org
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
http-request deny if bad_agent
四、ACL-operator 具体操作符
比较类型 | 比较方式 | 描述 | 示例 |
---|---|---|---|
整数比较 | eq | 等于 | value eq 5 |
整数比较 | ge | 大于等于 | value ge 10 |
整数比较 | gt | 大于 | value gt 15 |
整数比较 | le | 小于等于 | value le 20 |
整数比较 | lt | 小于 | value lt 25 |
字符比较 | exact match (-m str) | 字符串必须完全匹配模式 | string -m str "exact_pattern" |
字符比较 | substring match (-m sub) | 在提取的字符串中查找模式,若有发现则匹配 | string -m sub "sub_pattern" |
字符比较 | prefix match (-m beg) | 在提取的字符串首部查找模式,若有发现则匹配 | string -m beg "prefix_pattern" |
字符比较 | suffix match (-m end) | 将模式与提取字符串的尾部比较,若匹配则匹配 | string -m end "suffix_pattern" |
字符比较 | subdir match (-m dir) | 查看提取出来的用斜线分隔(“/")的字符串,若有匹配则匹配 | string -m dir "dir_pattern" |
字符比较 | domain match (-m dom) | 查找提取的用点(“.")分隔字符串,若有匹配则匹配 | string -m dom "domain_pattern" |
五、ACL-value 操作对象
匹配类型 | 描述 | 示例 |
---|---|---|
Boolean | 布尔值 | true 或 false |
integer or integer range | 整数或整数范围,如用于匹配端口范围 | 10 - 20 |
IP address / network | IP 地址或 IP 范围,如 192.168.0.1 ,192.168.0.1/24 | 10.0.0.1 或 10.0.0.0/8 |
string | 字符串 exact:精确比较 substring:子串 suffix:后缀比较 prefix:前缀比较 subdir:路径 domain:域名 | www.timinglee.org (精确)substring_of_string (子串)suffix_string (后缀)prefix_string (前缀)/wp-includes/js/jquery/jquery.js (路径)www.timinglee.org (域名) |
regular expression | 正则表达式 | ^[a-z]+$ |
hex block | 16 进制 | 0x1A |
六、多个ACL的组合调用方式
6.1多个ACL的逻辑处理
与:隐式(默认)使用
或:使用“or" 或 “||"表示
否定:使用 "!" 表示
6.2多个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示例-域名匹配实验
7.1实验环境:
实验环境与之前haproxy博客相同。
7.2实验步骤:
1.haproxy主机编写配置文件
# 基于域名的访问控制
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl domain hdr_dom(host) -i www.haha.org
use_backend webcluster-host if domain
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
2.测试结果
记得给测试的主机做本地域名解析,这一步可以参考以下链接:
http://t.csdnimg.cn/viQiBhttp://t.csdnimg.cn/viQiB
# 只有访问www.haha.org域名的时候才会去访问webserver1,否则访问webserver2
[C:\~]$ curl www.haha.org
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21 100 21 0 0 3558 0 --:--:-- --:--:-- --:--:-- 7000
web1 - 172.25.254.10
[C:\~]$ curl www.haha.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25 100 25 0 0 2763 0 --:--:-- --:--:-- --:--:-- 3571
webserver2:172.25.254.20
八、ACL示例-基于源IP或子网调度访问实验
8.1实验环境:
实验环境与之前相同。
8.2实验步骤:
1.haproxy主机编写配置文件
# 基于IP的访问控制
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24 # 这里可以写具体的IP地址也可以写网段
use_backend webcluster-host if ctrl_ip
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
2.测试结果
# 符合条件的访问webserver1,不符合的访问webserver2
[C:\~]$ curl www.abc.com #本机的IP为172.25.254.1,符合
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21 100 21 0 0 2151 0 --:--:-- --:--:-- --:--:-- 3000
web1 - 172.25.254.10
[root@webserver2 ~]# curl 172.25.254.100 #这台为17.25.254.20,符合
web1 - 172.25.254.10
[root@test ~]# curl 172.25.254.100 #这台主机为172.25.254.50,不符合
webserver2:172.25.254.20
九、ACL示例-匹配浏览器类型实验
9.1实验环境:
实验环境与之前相同。
9.2实验步骤:
1.haproxy主机编写配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl badwebbrowers hdr_sub(User-Agent) -i curl wget
http-request deny if badwebbrowers
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
2.测试结果
[C:\~]$ curl 172.25.254.100
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 93 100 93 0 0 62207 0 --:--:-- --:--:-- --:--:-- 93000
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
十、基于后缀名实现动静分离实验
10.1实验环境:
实验环境与之前相同。
10.2实验步骤:
1.webserver1下载php模块,写动态网页
[root@webserver1 ~]# yum install php -y
[root@webserver1 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
2.haproxy主机编写配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl static path_end -i .html .jpg .png .css .js # 静态
acl php path_end -i .php # 动态
use_backend webcluster-host if php
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
解释:只有webserver1上做了php,所以访问到以.php结尾的就会到php的页面,访问其他的就会默认去访问webserver2,以此来实现动静分离
3.浏览器访问
十一、基于访问路径实现动静分离
10.1实验环境:
实验环境与之前相同。
10.2实验步骤:
1.服务器上创建路径
#webserver2是静态
[root@webserver2 ~]# mkdir /usr/share/nginx/html/static -p
[root@webserver2 ~]# echo static - 172.25.254.20 > /usr/share/nginx/html/static/index.html
[root@webserver2 ~]#
[root@webserver2 ~]# curl 172.25.254.20/static/
static - 172.25.254.20
#动态
[root@webserver1 ~]# mkdir -p /var/www/html/php
[root@webserver1 ~]# cp /var/www/html/index.php /var/www/html/php/
2.haproxy主机编写配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl static path_sub -m sub static
acl php path_sub -m sub php
use_backend webcluster-host if php
default_backend default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5