Skip to content

Commit e12ce11

Browse files
committed
update haproxy doc
1 parent 63f11a6 commit e12ce11

File tree

6 files changed

+144
-99
lines changed

6 files changed

+144
-99
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@
435435
35. [webpack](src/webpack/README.md)
436436
36. [vuejs](src/vuejs/README.md)
437437
37. [HAProxy](src/HAProxy/README.md)
438+
- [源码编译安装](src/HAProxy/install.md)
439+
- [常用配置示例](src/HAProxy/config.md)
440+
- [ACL配置](src/HAProxy/ACL.md)
441+
- [HAProxy Nginx获取客户端真实IP](src/HAProxy/real_ip.md)
438442
38. [phpStudy](src/phpStudy.md) phpStudy升级php&MySQL
439443
39. [windows](src/windows/README.md)
440444
- [taskkill](src/windows/taskkill.md) 杀进程

src/HAProxy/ACL.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## HAProxy ACL详解
2+
- HAProxy 通过ACL 规则完成两种主要的功能,分别是:
3+
- 通过设置的ACL 规则检查客户端请求是否合法。如果符合ACL 规则要求,那么就将放行,反正,如果不符合规则,则直接中断请求。
4+
- 符合ACL 规则要求的请求将被提交到后端的backend 服务器集群,进而实现基于ACL 规则的负载均衡。
5+
- HAProxy 中的ACL 规则经常使用在frontend 段中,使用方法如下:
6+
7+
acl 自定义的acl名称 acl方法 -i [匹配的路径或文件]
8+
- 参数说明,其中:
9+
- acl:是一个关键字,表示定义ACL 规则的开始。后面需要跟上自定义的ACL 名称 。
10+
- acl 方法 : 这个字段用来定义实现ACL 的方法,HAProxy 定义了很多ACL 方法,经常使用的方法如下:
11+
- `hdr_beg (host)` #精确匹配主机, 表示以什么开头的域名
12+
- `hdr_reg (host)` #正则匹配主机,表示以什么开头的域名
13+
- `path_beg` #匹配路径,表示以什么路径开头
14+
- `path_end` #匹配路径结尾,表示以什么路径结尾
15+
- `url_sub` : 表示请求url 中包含什么字符串,例如:`acl file_req url_sub -i killall=`,表示在请求url 中包含`killall=`,则此控制策略返回true
16+
- `url_dir` : 表示请求url 中存在哪些字符串作为部分地址路径,例如 `acl dir_req url_dir -i allow`,表示在请求url 中存在`allow`作为部分地址路径,则此控制策略返回true,否则返回false
17+
- `-i`:表示忽略大小写,后面需要跟上匹配的路径或文件或正则表达式。
18+
- 与ACL 规则一起使用的HAProxy 参数还有`use_backend``use_backend` 后面需要跟上一个backend 实例名,表示在满足ACL 规则后去请求哪个backend实例,与use_backend 对应的还有`default_backend` 参数,它表示在没有满足ACL 条件的时候默认使用哪个后端backend。
19+
- 配置示例

src/HAProxy/README.md

+4-99
Original file line numberDiff line numberDiff line change
@@ -6,102 +6,7 @@
66
- [https://segmentfault.com/a/1190000007532860](https://segmentfault.com/a/1190000007532860)
77
- [HAProxy用法详解 全网最详细中文文档](http://www.ttlsa.com/linux/haproxy-study-tutorial/)
88
- [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)

src/HAProxy/config.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## HAProxy 配置示例
2+
> http://www.haproxy.org
3+
4+
- 相关资料
5+
- [HAProxy Documentation Converter 官方文档](http://cbonte.github.io/haproxy-dconv/)
6+
- [https://segmentfault.com/a/1190000007532860](https://segmentfault.com/a/1190000007532860)
7+
- [HAProxy用法详解 全网最详细中文文档](http://www.ttlsa.com/linux/haproxy-study-tutorial/)
8+
- [HAproxy功能配置](https://www.jianshu.com/p/8af373981cfe)
9+
10+
- 应用转发
11+
12+
global
13+
daemon
14+
maxconn 65535
15+
defaults
16+
mode http
17+
timeout connect 5000ms
18+
timeout client 5000ms
19+
timeout server 5000ms
20+
frontend http-in
21+
bind *:80
22+
default_backend servers
23+
backend servers
24+
server server1 192.168.1.2:80 cookie ser1 weight 10 check inter 2000 rise 2 fall 3
25+
server server2 192.168.1.3:80 cookie ser2 weight 10 check inter 2000 rise 2 fall 3
26+
server server3 192.168.1.4:80 cookie ser3 weight 10 check inter 2000 rise 2 fall 3
27+
- 根据uri转发
28+
29+
global
30+
daemon
31+
maxconn 65535
32+
defaults
33+
mode http
34+
timeout connect 5000ms
35+
timeout client 5000ms
36+
timeout server 5000ms
37+
frontend http-in
38+
bind *:80
39+
acl location_app path_beg -i /app/v1/location/report /app/v1/ruim/
40+
acl bigdata_app path_beg -i /admin/bigdata
41+
use_backend location_server if location_app
42+
use_backend bigdata_server if bigdata_app #if后面可写多个条件,空格隔开
43+
default_backend default_server
44+
backend default_server
45+
server server1 127.0.0.1:8080 cookie ser1 weight 100 check inter 2000 rise 2 fall 3
46+
backend location_server
47+
#mode http
48+
balance roundrobin
49+
server location01 192.168.124.25:8080
50+
server location02 192.168.124.27:8080
51+
backend bigdata_server
52+
server bigdata 192.168.124.26:8080
53+
54+
- 数据库转发
55+
56+
global
57+
daemon
58+
maxconn 65535
59+
defaults
60+
mode tcp
61+
timeout connect 5000ms
62+
timeout client 5000ms
63+
timeout server 5000ms
64+
listen mysql_server
65+
bind 0.0.0.0:13306
66+
server s1 192.168.124.21:3306

src/HAProxy/install.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## HAProxy
2+
> http://www.haproxy.org
3+
4+
- 相关资料
5+
- [HAProxy Documentation Converter 官方文档](http://cbonte.github.io/haproxy-dconv/)
6+
- [https://segmentfault.com/a/1190000007532860](https://segmentfault.com/a/1190000007532860)
7+
- [HAProxy用法详解 全网最详细中文文档](http://www.ttlsa.com/linux/haproxy-study-tutorial/)
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+
- 启动
30+
31+
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg

src/HAProxy/real_ip.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## HAProxy Nginx获取客户端真实IP
2+
- HAProxy配置
3+
4+
# defaults
5+
option forwardfor
6+
- Nginx配置(需要`http_realip_module`模块已被添加)
7+
8+
# http module
9+
set_real_ip_from 192.168.1.0/24;
10+
set_real_ip_from 192.168.2.1;
11+
set_real_ip_from 2001:0db8::/32;
12+
real_ip_header X-Forwarded-For;
13+
real_ip_recursive on;
14+
15+
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
16+
'$status $body_bytes_sent "$http_referer" '
17+
'"$http_user_agent" $http_x_forwarded_for '
18+
'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"';
19+
# server
20+
access_log /path/access.log access;

0 commit comments

Comments
 (0)