#安装了高版本的haproxy允许多进程模式输出到同一个admin.sock
global
log 127.0.0.1 local5 notice #将haproxy对应notice级别的日志发送给本机的默认rsyslog日志服务对应的自定义服务"local5"类别上。
chroot /var/lib/haproxy #锁定haproxy的运行目录
pidfile /run/haproxy.pid #haproxy运行是产生pid文件;haproxy.service文件也会指定pid文件产生路径
stats socket /run/haproxy/admin.sock mode 660 level admin #让HAProxy产生出一个sock出来,可以用于socket监控,比如socat;注意目录必须提前存在;默认路径应该就在/run/haproxy/下
stats timeout 30s
user nobody #haproxy运行时的用户身份
group nobody #haproxy运行时的用户组身份
daemon #在后台运行
nbproc 8 #开启的haproxy进程数,与CPU保持一致:8U16G
cpu-map 1 0
cpu-map 2 1
cpu-map 3 2
cpu-map 4 3
cpu-map 5 4
cpu-map 6 5
cpu-map 7 6
cpu-map 8 7
maxconn 30000 #每个haproxy进程的最大并发连接数
defaults
log global
mode http
option tcplog #开启tcplog,HAProxy将会在日志中记录数据包在传输层的更多属性
option dontlognull #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包。
#option redispatch #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,生产环境推荐添加该参数进行调优。
timeout connect 60s #转发客户端请求到后端server的最长连接时间(TCP之前),如"timeout connect 60s"则表示客户端请求到haproxy服务器之后,由haproxy将请求转发到后端服务器,若超过60s(即1分钟)后端服务器依旧没有响应客户端则超时(说白了就是客户端和后端服务器建立连接的超时时间)。
timeout client 120s #与客户端的最长空闲时间,表示客户端和服务器端已经建立连接,若在指定的规定的时间内发起了新的请求则无需重新建立连接,直接使用上一次的连接即可。
timeout server 120s #转发客户端请求到后端服务端的超时超时时长(TCP之后),如"timeout server 600s"则表示客户端请求到haproxy服务器之后,由haproxy将请求转发到后端的服务器,若后端服务器(比如数据库查询操作)在600s(10分钟)内依旧没有响应则超时(说白了就是客户端已经和后端服务器建立连接后,定义执行查询或写入操作的超时时间)。
timeout client-fin 30s #Set the inactivity timeout on the client side for half-closed connections
timeout server-fin 30s #Set the inactivity timeout on the server side for half-closed connections.
timeout tunnel 1h #websockets超时设置
maxconn 30000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
listen admin_stats
bind-process 1 #HAProxy多线程模式下stats需要配置绑定哪一个进程
bind 0.0.0.0:8089 #监听端口
stats enable #启用状态监控
mode http
log global
stats uri /stats #统计页面URI,默认值:/haproxy?stats
stats realm Welcome to the haproxy load balancer status page. you can contact XXX #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE #当通过认证才可管理
stats refresh 30s #统计页面自动刷新时间
frontend front_8088
bind *:8088
mode http
maxconn 20000
option forwardfor
default_backend check_back
backend check_back
mode http
balance leastconn
fullconn 20001
server 127.0.0.1_8080 127.0.0.1:8080 send-proxy check inter 600s
server 127.0.0.1_8081 127.0.0.1:8081 send-proxy check inter 600s