注意:在默认配置段的参数,有些在frontend,backend,listen段中也可以配置,如果在这些都配置了,该值会覆盖defaults段的
~~~
defaults
log global #引入global定义的日志格式
mode http #所处理的类别(7层代理http,4层代理tcp)
maxconn 50000 #最大连接数
option httplog #日志类别为http日志格式
option httpclose #每次请求完毕后主动关闭http通道
option dontlognull #不记录健康检查日志信息
option forwardfor #如果后端服务器需要获得客户端的真实ip,需要配置的参数,
可以从http header 中获取客户端的IP
retries 3 #3次连接失败就认为服务器不可用,也可以通过后面设置
option redispatch
#《---上述选项意思是指serverID 对应的服务器挂掉后,强制定向到其他健康的服务器, 当使用了 cookie时,
haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果
后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个
后端server上,以保证服务的正常---》
stats refresh 30 #设置统计页面刷新时间间隔
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
timeout http-request 10s #默认http请求超时时间
timeout queue 1m #默认队列超时时间
timeout connect 10s #默认连接超时时间
timeout client 1m #默认客户端超时时间
timeout server 1m #默认服务器超时时间
timeout http-keep-alive 10s #默认持久连接超时时间
timeout check 10s #设置心跳检查超时时间
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 404 /etc/haproxy/errorfiles/404.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http
~~~
说明:
1、mode http
设置haproxy的运行模式
注意:如果haproxy是用来代理四层(比如mysql),这里就要使用
mode tcp
tcp模式:在此模式下,客户端和服务器端之前将建立一个全双工的连接,不会对七层报文做任何检查,默认为tcp模式,经常用于SSL、SSH、SMTP等应用。
http模式:在此模式下,客户端请求在转发至后端服务器之前将会被深度分板,所有不与RFC格式兼容的请求都会被拒绝。
health:已基本不用了。
2、log global
设置日志继承全局配置段的设置
3、option httplog
表示打开记录http请求的日志功能
4、option dontlognull
如果产生了一个空连接,那这个空连接的日志将不会记录
5、option http-server-close
打开http协议中服务器端关闭功能,使得支持长连接,使得会话可以被重用
6、option forwardfor except 127.0.0.1/8
如果上游服务器上的应用程序想记录客户端的真实IP地址,haproxy会把客户端的IP信息发送给上游服务器,在HTTP请求中添加”X-Forwarded-For”字段,但当是haproxy自身的健康检测机制去访问上游服务器时是不应该把这样的访问日志记录到日志中的,所以用except来排除127.0.0.0,即haproxy自身
7、option redispatch
当与上游服务器的会话失败(服务器故障或其他原因),把会话重新分发到其他健康的服务器上,当原来故障的服务器恢复,会话又被定向到已恢复的服务器上
8、retries 3
向上游服务器尝试连接的最大次数,超过此值就认为后端服务器不可用
9、option abortonclose
当haproxy负载很高时,自动结束掉当前队列处理比较久的连接
10、timeout http-requests 10s
客户端发送http请求的超时时间
11、timeout queue 1m
当上游服务器在高负载响应haproxy时,会把haproxy发送来的请求放进一个队列中,timeout queue定义放入这个队列的超时时间
12、timeout connect 5s
haproxy 与 后端服务器连接超时时间,如果在同一个局域网可设置较小的时间
13、timeout client 1m
定义客户端和haproxy连接,非活动连接的超时时间
14、timeout server 1m
定义haproxy与上游服务器非活动连接的超时时间
15、timeout http-keep-alive 10s
定义新的http请求连接建立的最大超时时间(持久连接,多长时间不活动,就释放)
16、timeout check 10s
健康检测的最大超时时间
17 maxconn
最大并发连接数
18 spread-check 《0-50,in percent》
在haproxy后端有众多服务器的场景中,在精确的时间间隔后统一对众服务器进行健康检查可能会带来意外问题。此选项用于将其检查的时间间隔长度上增加或减少一定的随机时长
19、compression
开启HTTP压缩功能
compression algo gzip
#compression offload
compression type text/html text/plain text/css application/javascript application/json application/xml application/x-shockwave-flash application/pdf
20 errorfile
当客户访问一个不存在的页面或者服务器端响应错误,haproxy会根据不同的响应码回应不同的页面,这些页面可以根据公司业务,提交定制
案例:
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 404 /etc/haproxy/errorfiles/404.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http