概述:
本章将继续上章的内容介绍haprosy代理配置段的相关参数,具体如下:
- 4层检测机制:dst,dst_port,src,src_port
- 7层检查机制:path、req.hdr、res.hdr;
================================================================================
HAProxy配置参数---代理配置段:10.ACL控制访问列表
★语法格式: ⊙<aclname>: ACL names must be formed from upper and lower case letters, digits, '-' (dash), '_' (underscore) , '.' (dot) and ':' (colon). ACL names are case-sensitive. ACL名称可由,大小写字母,数字,'-','_','.'和':' 并且区分大小写。
⊙<value>的类型: - boolean //布尔型值 - integer or integer range //整数或整数范围 - IP address / network //ip地址 - string (exact, substring, suffix, prefix, subdir, domain) //字符串 - regular expression //正则表达式 - hex block
⊙[flags] -i : 被模式匹配时忽略字符大小写,比较常用 -f : load patterns from a file. -m : use a specific pattern matching method -n : forbid the DNS resolutions -M : load the file pointed by -f like a map file. -u : force the unique id of the ACL -- : force end of flags. Useful when a string looks like one of the flags.//转义
⊙[operator] ①数值匹配: eq : true if the tested value equals at least one value ge : true if the tested value is greater than or equal to at least one value gt : true if the tested value is greater than at least one value le : true if the tested value is less than or equal to at least one value lt : true if the tested value is less than at least one value
②字符串匹配: - exact match (-m str) : 字符串精确匹配 - substring match (-m sub) : 子串匹配 - prefix match (-m beg) : 前缀匹配 - suffix match (-m end) : 后缀匹配 - subdir match (-m dir) : 子目录匹配 - domain match (-m dom) : 域匹配
⊙条件的逻辑连接
⊙<creterion>:
★<creterion>: ⊙4层检测机制: dst : ip dst_port : integer src : ip src_port : integer
※block { if | unless } <condition>
作用:条件匹配就阻断一个7层请求 Example: acl invalid_src src 0.0.0.0/7 224.0.0.0/3acl invalid_src src_port 0:1023acl local_dst hdr(host) -i localhostblock if invalid_src || local_dst
演示: 1.阻断来自非 10.1.250.25 的ip(浏览器地址)请求,编辑配置文件,如下: 重载haproxy服务,在浏览器中访问可以发现,拒绝访问 在本机使用curl命令可以正常访问,说明仅拒绝了来自10.1.250.25的ip的请求。
------------------------------------------------------------------------------------------- 2.仅允许本浏览器(10.1.250.25)可以访问8080端口,编辑配置文件如下:
重载haproxy服务,在浏览器中访问可以发现,可以正常访问
在本机使用curl命令访问8080端口,拒绝访问,如下: 1
2
3
4
| [iyunv@centos7 haproxy]# curl http://10.1.252.153:8080
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
|
★7层检查机制:path、req.hdr、res.hdr ⊙path : string This extracts the request's URL path, which starts at the first slash and ends before the question mark (without the host part). //提取用户请求的URL路径与对应的请求报文的url作比较,从第一个斜杠开始和到问号之前的内容(没有主机部分)。
演示: 1.拒绝用户访问以.txt结尾的资源,编辑配置文件如下:
重载haproxy服务,向RS1提供一个.txt结尾的文件 [iyunv@RS1 ~]# cp /etc/fstab /var/www/html/fstab.txt 在浏览器中请求此资源,发现拒绝访问,说明我们在配置文件中定义的path生效了,如下:
------------------------------------------------------------------------------------------ ⊙req.hdr([<name>[,<occ>]]) : string (对请求报文中的内容做检查) This extracts the last occurrence of header <name> in an HTTP request. hdr([<name>[,<occ>]]) : exact string match hdr_beg([<name>[,<occ>]]) : prefix match hdr_dir([<name>[,<occ>]]) : subdir match hdr_dom([<name>[,<occ>]]) : domain match hdr_end([<name>[,<occ>]]) : suffix match hdr_len([<name>[,<occ>]]) : length match hdr_reg([<name>[,<occ>]]) : regex match hdr_sub([<name>[,<occ>]]) : substring match
⊙res.hdr([<name>[,<occ>]]) : string 对响应报文中的内容做检测 shdr([<name>[,<occ>]]) : exact string match shdr_beg([<name>[,<occ>]]) : prefix match shdr_dir([<name>[,<occ>]]) : subdir match shdr_dom([<name>[,<occ>]]) : domain match shdr_end([<name>[,<occ>]]) : suffix match shdr_len([<name>[,<occ>]]) : length match shdr_reg([<name>[,<occ>]]) : regex match shdr_sub([<name>[,<occ>]]) : substring match
演示: 禁止使用Firefox浏览器访问内容,编辑配置文件,如下:
重载haproxy服务,使用Firefox浏览器访问可以发现403拒绝访问,使用chrome和其他浏览器可以正常访问,如下:
------------------------------------------------------------------------------------------ ⊙url : string This extracts the request's URL as presented in the request. ⊙method : integer + string 检查 请求报文中的请求方法 Example : # only accept GET and HEAD requestsacl valid_method method GET HEADhttp-request deny if ! valid_method注意:
11.访问控制相关的参数:
★http层的访问控制参数 ※block { if | unless } <condition> ---Block a layer 7 request if/unless a condition is matched 作用:阻止符合指定acl的访问请求; 范围:frontend、listen、backend Example: acl invalid_src src 0.0.0.0/7 224.0.0.0/3acl invalid_src src_port 0:1023acl local_dst hdr(host) -i localhostblock if invalid_src || local_dst※http-request { allow | deny} [ { if | unless } <condition> ] ---Access control for Layer 7 requests(7层访问控制) Example: acl nagios src 192.168.129.3acl local_net src 192.168.0.0/16acl auth_ok http_auth(L1)http-request allow if nagioshttp-request allow if local_net auth_okhttp-request auth realm Gimme if local_net auth_okhttp-request deny
演示: 1.仅允许本地主机访问admin目录,编辑配置文件,如下:
在后端主机创建对应的admin目录,并提供其测试页面,如下: [iyunv@RS1 ~]# mkdir /var/www/html/admin[iyunv@RS1 ~]# echo "<h1>Admin</h1>" > /var/www/html/admin/index.html[iyunv@RS1 ~]# cat /var/www/html/admin/index.html<h1>Admin</h1> 在浏览器中访问(10.1.250.25)提示403没有访问权限,在本机使用curl可以正常访问,如下: [iyunv@centos7 haproxy]# curl http://10.1.252.153/admin/<h1>Admin</h1>-------------------------------------------------------------------------------- ★TCP层的访问控制参数 ※tcp-request connection <action> [{if | unless} <condition>] ---Perform an action on an incoming connection depending on a layer 4 condition ※tcp-request content <action> [{if | unless} <condition>] ---Perform an action on a new session depending on a layer 4-7 condition
Example: tcp-request connection accept if { src -f /etc/haproxy/whitelist.lst }tcp-request connection track-sc0 srctcp-request connection reject if { sc0_conn_rate gt 10 }Example: # Accept HTTP requests containing a Host header saying "example.com"# and reject everything else.acl is_host_com hdr(Host) -i example.comtcp-request inspect-delay 30stcp-request content accept if is_host_comtcp-request content reject
|