|
6 | 6 | - [https://segmentfault.com/a/1190000007532860](https://segmentfault.com/a/1190000007532860)
|
7 | 7 | - [HAProxy用法详解 全网最详细中文文档](http://www.ttlsa.com/linux/haproxy-study-tutorial/)
|
8 | 8 | - [HAproxy功能配置](https://www.jianshu.com/p/8af373981cfe)
|
9 |
| - |
10 |
| -- 下载 |
11 |
| - |
12 |
| - wget http://www.haproxy.org/download/2.0/src/haproxy-2.1.0.tar.gz |
13 |
| -- 解压 |
14 |
| - |
15 |
| - tar -zxvf haproxy-2.1.0.tar.gz |
16 |
| - cd haproxy-2.1.0 |
17 |
| -- 安装 |
18 |
| - |
19 |
| - make TARGET=linux-glibc ARCH=x86_64 USE_NS= PREFIX=/usr/local/haproxy |
20 |
| - make install PREFIX=/usr/local/haproxy |
21 |
| - |
22 |
| - // 参数说明 |
23 |
| - |
24 |
| - TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628 |
25 |
| - ARCH=x86_64 #系统位数 |
26 |
| - USE_NS= #在centos6下会报错,`undefined reference to setns`,原因:setns() is supported since linux kernel 3.0,所以centos6(kernel 2.6)安装时需要禁用它 |
27 |
| - PREFIX=/usr/local/haprpxy # /usr/local/haprpxy为haprpxy安装路径 |
28 |
| - |
29 |
| -- 配置文件haproxy.cfg |
30 |
| - - 应用转发 |
31 |
| - |
32 |
| - global |
33 |
| - daemon |
34 |
| - maxconn 65535 |
35 |
| - defaults |
36 |
| - mode http |
37 |
| - timeout connect 5000ms |
38 |
| - timeout client 5000ms |
39 |
| - timeout server 5000ms |
40 |
| - frontend http-in |
41 |
| - bind *:80 |
42 |
| - default_backend servers |
43 |
| - backend servers |
44 |
| - server server1 192.168.1.2:80 cookie ser1 weight 10 check inter 2000 rise 2 fall 3 |
45 |
| - server server2 192.168.1.3:80 cookie ser2 weight 10 check inter 2000 rise 2 fall 3 |
46 |
| - server server3 192.168.1.4:80 cookie ser3 weight 10 check inter 2000 rise 2 fall 3 |
47 |
| - - 根据uri转发 |
48 |
| - |
49 |
| - global |
50 |
| - daemon |
51 |
| - maxconn 65535 |
52 |
| - defaults |
53 |
| - mode http |
54 |
| - timeout connect 5000ms |
55 |
| - timeout client 5000ms |
56 |
| - timeout server 5000ms |
57 |
| - frontend http-in |
58 |
| - bind *:80 |
59 |
| - acl location_app path_beg -i /app/v1/location/report /app/v1/ruim/ |
60 |
| - acl bigdata_app path_beg -i /admin/bigdata |
61 |
| - use_backend location_server if location_app |
62 |
| - use_backend bigdata_server if bigdata_app #if后面可写多个条件,空格隔开 |
63 |
| - default_backend default_server |
64 |
| - backend default_server |
65 |
| - server server1 127.0.0.1:8080 cookie ser1 weight 100 check inter 2000 rise 2 fall 3 |
66 |
| - backend location_server |
67 |
| - #mode http |
68 |
| - balance roundrobin |
69 |
| - server location01 192.168.124.25:8080 |
70 |
| - server location02 192.168.124.27:8080 |
71 |
| - backend bigdata_server |
72 |
| - server bigdata 192.168.124.26:8080 |
73 |
| - |
74 |
| - - 数据库转发 |
75 |
| - |
76 |
| - global |
77 |
| - daemon |
78 |
| - maxconn 65535 |
79 |
| - defaults |
80 |
| - mode tcp |
81 |
| - timeout connect 5000ms |
82 |
| - timeout client 5000ms |
83 |
| - timeout server 5000ms |
84 |
| - listen mysql_server |
85 |
| - bind 0.0.0.0:13306 |
86 |
| - server s1 192.168.124.21:3306 |
87 |
| -- 启动 |
88 |
| - |
89 |
| - /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg |
90 |
| -- ACL详解 |
91 |
| - - HAProxy 通过ACL 规则完成两种主要的功能,分别是: |
92 |
| - - 通过设置的ACL 规则检查客户端请求是否合法。如果符合ACL 规则要求,那么就将放行,反正,如果不符合规则,则直接中断请求。 |
93 |
| - - 符合ACL 规则要求的请求将被提交到后端的backend 服务器集群,进而实现基于ACL 规则的负载均衡。 |
94 |
| - - HAProxy 中的ACL 规则经常使用在frontend 段中,使用方法如下: |
95 |
| - |
96 |
| - acl 自定义的acl名称 acl方法 -i [匹配的路径或文件] |
97 |
| - - 参数说明,其中: |
98 |
| - - acl:是一个关键字,表示定义ACL 规则的开始。后面需要跟上自定义的ACL 名称 。 |
99 |
| - - acl 方法 : 这个字段用来定义实现ACL 的方法,HAProxy 定义了很多ACL 方法,经常使用的方法有hdr_reg(host)、hdr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end 等。 |
100 |
| - - hdr_beg(host) #精确匹配主机, 表示以什么开头的域名 |
101 |
| - - hdr_reg(host) #正则匹配主机,表示以什么开头的域名 |
102 |
| - - path_beg #匹配路径,表示以什么路径开头 |
103 |
| - - path_end #匹配路径结尾,表示以什么路径结尾 |
104 |
| - - url_sub : 表示请求url 中包含什么字符串,例如:acl file_req url_sub -i killall=,表示在请求url 中包含killall=,则此控制策略返回true |
105 |
| - - url_dir : 表示请求url 中存在哪些字符串作为部分地址路径,例如 acl dir_req url_dir -i allow,表示在请求url 中存在allow作为部分地址路径,则此控制策略返回true,否则返回false |
106 |
| - - -i:表示忽略大小写,后面需要跟上匹配的路径或文件或正则表达式。 |
107 |
| - - 与ACL 规则一起使用的HAProxy 参数还有use_backend,use_backend 后面需要跟上一个backend 实例名,表示在满足ACL 规则后去请求哪个backend实例,与use_backend 对应的还有default_backend 参数,它表示在没有满足ACL 条件的时候默认使用哪个后端backend。 |
| 9 | +- [源码编译安装](install.md) |
| 10 | +- [常用配置示例](config.md) |
| 11 | +- [ACL配置](ACL.md) |
| 12 | +- [HAProxy Nginx获取客户端真实IP](real_ip.md) |
0 commit comments