码迷,mamicode.com
首页 > 其他好文 > 详细

HAProxy 高级应用(一)

时间:2016-11-22 23:59:03      阅读:1269      评论:0      收藏:0      [点我收藏+]

标签:haproxy

HAProxy 高级应用

================================================================================

概述:

  本章将继续上章的内容介绍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/3
acl invalid_src  src_port     0:1023
acl local_dst    hdr(host) -i localhost
block if invalid_src || local_dst

演示:

  1.阻断来自非 10.1.250.25 的ip(浏览器地址)请求,编辑配置文件,如下:

技术分享

 重载haproxy服务,在浏览器中访问可以发现,拒绝访问

技术分享

 在本机使用curl命令可以正常访问,说明仅拒绝了来自10.1.250.25的ip的请求。

[root@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 1</h1>
[root@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 1</h1>
[root@centos7 haproxy]# curl http://10.1.252.153
<h1>Backend Server 2</h1>

-------------------------------------------------------------------------------------------

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

技术分享


 重载haproxy服务,在浏览器中访问可以发现,可以正常访问

技术分享


 在本机使用curl命令访问8080端口,拒绝访问,如下:

[root@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结尾的资源,编辑配置文件如下:

技术分享

 

 重载haproxy服务,向RS1提供一个.txt结尾的文件

[root@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.

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

技术分享

 

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

技术分享


------------------------------------------------------------------------------------------

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 requests
acl valid_method method GET HEAD
http-request deny if ! valid_method

注意:

  • HAProxy有众多内建的ACLs,这些ACLs可直接调用,例如LOCALHOST,TRUE,HTTP;

技术分享

技术分享





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/3
acl invalid_src  src_port     0:1023
acl local_dst    hdr(host) -i localhost
block 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.3
acl local_net src 192.168.0.0/16
acl auth_ok http_auth(L1)

http-request allow if nagios
http-request allow if local_net auth_ok
http-request auth realm Gimme if local_net auth_ok
http-request deny


演示:

  1.仅允许本地主机访问admin目录,编辑配置文件,如下:

技术分享


 在后端主机创建对应的admin目录,并提供其测试页面,如下:

[root@RS1 ~]# mkdir /var/www/html/admin
[root@RS1 ~]# echo "<h1>Admin</h1>" > /var/www/html/admin/index.html
[root@RS1 ~]# cat /var/www/html/admin/index.html
<h1>Admin</h1>

 在浏览器中访问(10.1.250.25)提示403没有访问权限,在本机使用curl可以正常访问,如下:

技术分享

[root@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 src
tcp-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.com
tcp-request inspect-delay 30s
tcp-request content accept if is_host_com
tcp-request content reject







本文出自 “逐梦小涛” 博客,请务必保留此出处http://1992tao.blog.51cto.com/11606804/1875534

HAProxy 高级应用(一)

标签:haproxy

原文地址:http://1992tao.blog.51cto.com/11606804/1875534

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!