您的位置:首页 > 其它

haproxy实现动静分离机制

2015-10-09 11:42 405 查看
ACL的简介:
haproxy的ACL用于实现基于请求报文的首部、响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性。其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满足时执行某特定的动作,如阻止请求或转发至某特定的后端。
syntax:
acl <aclname> <criterion> [flags] [operator] <value> ...

Description:
<aclname>:ACL名称,区分字符大小写,且其只能包含大小写字母、数字、-(连接线)、_(下划线)、.(点号)和:(冒号);haproxy中,acl可以重名,这可以把多个测试条件定义为一个共同的acl;
<criterion>:测试标准,即对什么信息发起测试;测试方式可以由[flags]指定的标志进行调整;而有些测试标准也可以需要为其在<value>之前指定一个操作符[operator];
[flags]:目前haproxy的acl支持的标志位有3个:
-i:不区分<value>中模式字符的大小写;
-f:从指定的文件中加载模式;
--:标志符的强制结束标记,在模式中的字符串像标记符时使用;
<value>:acl测试条件支持的值有以下四类:
1,整数:如1024:65535表示从1024至65535;仅支持使用正整数(如果出现类似小数的标识,其为通常为版本测试),且支持使用的操作符有5个,分别为eq、ge、gt、le和lt;
2,字符串:支持使用“-i”以忽略字符大小写,支持使用“\”进行转义;如果在模式首部出现了-i,可以在其之前使用“--”标志位;
3,正则表达式;
4,IP地址及网络地址;
PS:同一个acl中可以指定多个测试条件,这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种:“与”(默认即为与操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。

ACL常用的检测标准:
syntax:dst  <ip address>
syntax:dst_port  <integer>
syntax:src  <ip address>
syntax:src_port  <integer>
Description:对源地址,源端口,目标地址,目标端口进行检测

example:
acl  goodguys src  192.168.0.0/16
acl  goodport  src_port  80

syntax:tcp-request content <action> [{if | unless} <condition>]
Description:对tcp协议请求内容进行过滤 (4层)

example:
acl  goodguys src  127.0.0.1
acl  badguys  src  172.10.0.0/16
tcp-request content reject  if badguys
tcp-request content accept if goodguys
tcp-request content reject
syntax:http-request  <action> [{if | unless} <condition>]
Description:对http协议请求内容进行过滤检测 (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
syntax:hdr(header) <string>
Description:用于测试请求报文中的所有首部或指定首部是否满足指定的条件;指定首部时,其名称不区分大小写,且在括号“()”中不能有任何多余的空白字符。

example:
acl valid_conn hdr(Connection) -i close    ##检测响应报文Connection首部的值是否为close,-i 不区分大小写。
syntax:hdr_reg(header) <regexp>
Description:用于测试请求报文中的所有首部或指定首部是否匹配正则表达式;指定首部时,其名称不区分大小写,且在括号“()”中不能有任何多余的空白字符。

example:
acl luochen hdr_reg(Host) -i .*\.luochen.com

syntax:method <string>
Description:测试HTTP请求报文中使用的方法。

example:
acl mthod  method  get

syntax:path <string>
Description:测试HTTP请求报文中使用的方法。

example:
acl index  path  /index.html
syntax:path_beg <string>
syntax:path_end <string>
Description:用于测试请求的URL是否以<string>指定的模式结尾。

example:
acl url_static       path_beg       -i /static /images /javascript /stylesheets
acl url_static       path_end       -i .jpg .gif .png .css .js
syntax:hdr_beg <string>
syntax:hdr_end <string>
Description:用于测试请求报文的指定首部的开头部分是否符合<string>指定的模式。

example:
acl host_static hdr_beg(host) -i img. video. download. ftp.
acl host_static hdr_end(host) -i .jpg .gif .png .css .js
haproxy 动静分离的相关配置(拓扑结构图如下):



在haproxy配置文件中配置:
global
log                127.0.0.1  local2
chroot          /var/lib/haproxy
pidfile          /var/run/haproxy.pid
maxconn     4000
user             haproxy
group          haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
mode        http
log             global
option        httplog
option        dontlognull
option        http-server-close
option        forwardfor          except 127.0.0.0/8
option        redispatch
retries         3
timeout http-request    10s
timeout queue              40s
timeout connect           10s
timeout client               30s
timeout server              30s
timeout http-keep-alive  10s
timeout check                  10s
maxconn                          30000
listen stats
mode http
bind   *:1080
stats   enable
stats   hide-version
stats   uri     /haproxyadmin?stats
stats   realm   Haproxy\ Statistics
stats   auth    admin:admin
stats   admin if TRUE
frontend http-in
bind   *:80
mode http
log     global
option httpclose
option logasap
option dontlognull
capture request  header Host len 20
capture request  header Referer len 60
acl url_static       path_beg       -i /static /images /javascript /stylesheets
acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js .html

acl host_static hdr_beg(host) -i img. video. download. ftp.
acl url_php path_end -i .php

use_backend static_servers if url_static or host_static
default_backend  dynamic_servers  if url_php

backend static_servers
balance roundrobin
server imgsrv1 192.168.10.112:80 check port 80 intval 2 rise 1 fall 2 maxconn 6000

backend dynamic_servers
cookie web1 insert nocache
balance roundrobin
server web1 192.168.10.113:80 check port 80 intval 2 rise 1 fall 2 maxconn 1000 cookie web1
负载均衡MySQL服务的配置示例(这里只是提供配置参数):

global
log                127.0.0.1  local2
chroot          /var/lib/haproxy
pidfile          /var/run/haproxy.pid
maxconn     4000
user             haproxy
group          haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
mode        http
log             global
option        httplog
option        dontlognull
option        http-server-close
option        forwardfor          except 127.0.0.0/8
option        redispatch
retries         3
timeout http-request    10s
timeout queue              40s
timeout connect           10s
timeout client               30s
timeout server              30s
timeout http-keep-alive  10s
timeout check                  10s
maxconn                          30000
listen stats
mode http
bind   *:1080
stats   enable
stats   hide-version
stats   uri     /haproxyadmin?stats
stats   realm   Haproxy\ Statistics
stats   auth    admin:admin
stats   admin if TRUE
frontend mysql
bind *:3306
mode tcp
log global
default_backend mysqlservers

backend mysqlservers
balance leastconn
server dbserver1 192.168.10.112:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
server dbserver2 192.168.10.113:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300


本文出自 “珞辰的博客” 博客,请务必保留此出处http://luochen2015.blog.51cto.com/9772274/1701153
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: