设为首页 收藏本站
查看: 2169|回复: 0

[经验分享] HAProxy 高级应用(一)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-23 08:52:20 | 显示全部楼层 |阅读模式
概述:
  本章将继续上章的内容介绍haprosy代理配置段的相关参数,具体如下:
  • ACL控制访问列表;

  • 4层检测机制:dst,dst_port,src,src_port
  • 7层检查机制:path、req.hdr、res.hdr;

  • http层访问控制相关的参数:

  • block,http-request

  • TCP层的访问控制参数


================================================================================
HAProxy配置参数---代理配置段:
10.ACL控制访问列表
★语法格式:
  • acl <aclname> <criterion> [flags] [operator] [<value>] ...

⊙<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) : 域匹配

⊙条件的逻辑连接
  • - AND (implicit)
  • - OR  (explicit with the "or" keyword or the "||" operator)
  • - Negation with the exclamation mark ("!")

⊙<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(浏览器地址)请求,编辑配置文件,如下:
wKiom1gzvL6BLVZhAABKZXCaccs215.jpg
重载haproxy服务,在浏览器中访问可以发现,拒绝访问
wKiom1gzvR6xeltMAABLwPQcD2A541.jpg
在本机使用curl命令可以正常访问,说明仅拒绝了来自10.1.250.25的ip的请求。
1
2
3
4
5
6
[iyunv@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 1</h1>
[iyunv@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 1</h1>
[iyunv@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 2</h1>



-------------------------------------------------------------------------------------------
2.仅允许本浏览器(10.1.250.25)可以访问8080端口,编辑配置文件如下:
wKiom1gzwWeCZ4QyAABPvj6oV-c773.jpg

重载haproxy服务,在浏览器中访问可以发现,可以正常访问
wKiom1gzwP3j2tkkAABGDYwVJ-g982.jpg

在本机使用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作比较,从第一个斜杠开始和到问号之前的内容(没有主机部分)。
  • ACL derivatives :

  • path     : exact string match
  • path_beg : prefix match
  • path_dir : subdir match
  • path_dom : domain match
  • path_end : suffix match
  • path_len : length match
  • path_reg : regex match
  • path_sub : substring match

演示:
1.拒绝用户访问以.txt结尾的资源,编辑配置文件如下:
wKiom1gz1ADRc-66AABXublu30I521.jpg

重载haproxy服务,向RS1提供一个.txt结尾的文件
[iyunv@RS1 ~]# cp /etc/fstab /var/www/html/fstab.txt
在浏览器中请求此资源,发现拒绝访问,说明我们在配置文件中定义的path生效了,如下:
wKiom1gz1UHQSIkcAABNeGZks4U745.jpg

------------------------------------------------------------------------------------------
⊙req.hdr([<name>[,<occ>]]) : string (对请求报文中的内容做检查)
   This extracts the last occurrence of header <name> in an HTTP request.
  • ACL derivatives :

  • 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
对响应报文中的内容做检测
  • ACL derivatives :

  • 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浏览器访问内容,编辑配置文件,如下:
wKiom1gz8gmwY7SqAABfY0TZbnk450.jpg

  重载haproxy服务,使用Firefox浏览器访问可以发现403拒绝访问,使用chrome和其他浏览器可以正常访问,如下:
wKioL1gz8xqB6kJzAABJjA9Jsu4096.jpg

------------------------------------------------------------------------------------------
⊙url : string
   This extracts the request's URL as presented in the request.
  • ACL derivatives :

  • 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

⊙method : integer + string
  检查 请求报文中的请求方法
Example :
# only accept GET and HEAD requestsacl valid_method method GET HEADhttp-request deny if ! valid_method
注意:
  • HAProxy有众多内建的ACLs,这些ACLs可直接调用,例如LOCALHOST,TRUE,HTTP;

wKiom1gz_d-RPnT7AACAe00T2y4205.jpg
wKioL1gz_eCAQSKtAABT9EhRIrE355.jpg




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目录,编辑配置文件,如下:
wKioL1g0BD6hLwdUAABQgbHxc4E924.jpg

在后端主机创建对应的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可以正常访问,如下:
wKioL1g0BNyCvMrnAABHRXdoLNo025.jpg
[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
wKiom1g0CuaTyUexAAA58xMsXKk865.jpg

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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-304236-1-1.html 上篇帖子: HAProxy 使用进阶 下篇帖子: HAProxy 高级应用(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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