1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
| [iyunv@nod0 haproxy]# cat haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
#设置把haproxy的日志发往本地的rsyslog服务
log 127.0.0.1 local2
#把haproxy进程限定在chroot指定的目录运行,此目录权限需要设定为除了haproxy用户有写权限限外其他的用户没有,并且是一个空目录
chroot /var/lib/haproxy
#pid文件的路径
pidfile /var/run/haproxy.pid
#设置proxy允许的最大并发连接数,如果在backend中没有指定server的maxconn的值,则可继承此值
maxconn 4000
#运行haproxy的用户
user haproxy
#运行haproxy的组
group haproxy
#让haproxy以守护进程运行在后台
daemon
# turn on stats unix socket
#定义统计信息保存的位置
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
#默认设置
defaults
#设置haproxy的运行模式,有三种{http|tcp|health}
mode http
#设置日志继承全局配置段的设置
log global
#表示开始打开记录http请求的日志功能,详细的应查看官方文档的的关键字矩阵部分
option httplog
#如果产生了一个空连接,那这个空连接的日志将不会记录
option dontlognull
#打开http协议中服务器端关闭功能,使得支持长连接,使得会话可以被重用,使得每一个日志记录都会被记录
option http-server-close
#如果上游服务器上的应用程序想记录发起请求的客户端的IP地址,HAProxy会把客户端的IP信息发送给上游服务器,在HTTP请求中添加"X-Forwarded-For"字段,但当是haproxy自身的健康检测机制去访问上游服务器时是不应该把这样的访问日志记录到日志中的,所以用except来排除127.0.0.0,即haproxy身。
option forwardfor except 127.0.0.0/8
#当与上游服务器的会话失败(服务器故障或其他原因)时,把会话重新分发到其他健康的服务器上,当原来故障的服务器恢复时,会话又被定向到已恢复的服务器上。还可以用"retries"关键字来设定在判定会话失败时的尝试连接的次数,
option redispatch
#当haproxy负载很高时,自动结束掉当前队列处理比较久的链接
option abortonclose
#向上游服务器尝试连接的最大次数,超过此值就认为后端服务器不可用
retries 3
#客户端发送http请求的超时时间
timeout http-request 10s
#当上游服务器在高负载响应haproxy时,会把haproxy发送来的请求放进一个队列中,timeout queue定义放入这个队列的超时时间。
timeout queue 1m
#haproxy与后端服务器连接超时时间,如果在同一个局域网可设置较小的时间
timeout connect 5s
#定义客户端与haproxy连接后,数据传输完毕,不再有数据传输,即非活动连接的超时时间
timeout client 1m
#定义haproxy与上游服务器非活动连接的超时时间
timeout server 1m
#设置新的http请求连接建立的最大超时时间,时间较短时可以尽快释放出资源,节约资源
timeout http-keep-alive 10s
#健康检测的时间的最大超时时间
timeout check 10s
#最大并发连接数
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
#
# use_backend static if url_static
# default_backend app
#######监控统计页面设置############
listen stats
#监听端口
bind 0.0.0.0:1088
#日志设置,级别有[emerg alert crit err warning notice info debug]
log 127.0.0.1 local3 err
#启用统计报告,是一个默认的参数
stats enable
#隐藏统计页面上haproxy的版本信息
stats hide-version
#统计页面刷新时间
stats refresh 10s
#进入统计页面密码框上的提示文字
stats realm Haproxy\ Stats
#进入统计页面输入的用户名及密码
stats auth admin:admin
#统计页面的登陆uri地址
stats uri /admin?admin
#当统计页面打开后再打开对后端服务器的管理功能
stats admin if TRUE
####frontend配置######
frontend server1_80_in
#绑定任何地址的80端口
bind *:80
#haproxy以http的方式运行
mode http
#定义两个能实现匹配静态文件的acl
acl is_static path_beg -i /static /image /javascript
acl is_static path_end -i .html .png .jpg .gif .jpeg .css .js
#定义当匹配到is_static的acl后转发到static_servers这个后端服务器组
use_backend static_servers if is_static
#默认的转发规则
default_backend app_servers
####backend配置#######
backend static_servers
#静态服务器组采用roundrobin算法
balance roundrobin
#定义上游服务器及各个属性,对服务器每隔1500毫秒进行一次健康检测,如果连续3次无法联系服务器那就认为此服务器故障,如果连接连续检测到2次务都在线,那就认为此服务可用
server nod2_static_192.168.0.202 192.168.0.202:80 minconn 10 maxconn 2000 check inter 1500 fastinter 1000 downinter 1000 rise 2 fall 3 weight 1
backend app_servers
#定义动态服务器的调度算法为source,并提供了hash-type参数,使source成为一个动态算法
balance source
hash-type consistent
server nod1_appserver_192.168.0.201 192.168.0.201:80 minconn 10 maxconn 200 check inter 1500 rise 3 fall 3 weight 1
|