Configure Haproxy
1. Unzip Haproxy to D:\haproxy
2. Placing haproxy.cfg Files
Global log 127.0.0.1 local0 maxconn daemon defaults log 127.0.0.1 local3 mode http option httplog option dontlognull option redispatch Retries 3 contimeout clitimeout 50000 srvtimeout 50000 Listen Private_ monitoring:18100 #监听端口 mode http option Httplog stats enable stats uri/stats stats Refresh 5s
View Code
3. Build a run.bat, which reads as follows: haproxy.exe-f haproxy.cfg-d. Where-D is run in the window and-D is a daemon, only in Task Manager
4. Run Haproy-f haproxy.cfg.
5. Check if startup is successful.
Haproxy configuration file Options parameters
Global
Log 127.0.0.1 local0 Info
#全局的日志配置, use native syslog to log log, use/dev/log, where the log level is [Err warning info debug].local0 is a log device (defined in/etc/syslog/conf),
#必须为如下24种标准syslog设备的一种:
#kern User Mail daemon auth syslog LPR News
#uucp cron auth2 FTP NTP audit alert Cron2
#local0 local1 local2 local3 local4 local5 local6 local7
Maxconn 65536 #最大连接数
Ulimit-n 131087 #必须是maxconn的两倍以上
Chroot/usr/local/haproxy #适用于chroot环境, for security.
UID #haproxy运行的用户和组
GID 99
Daemon #后台运行
Nbproc 4 #设置4个并发进程, recommended for debug when set to 1
Pidfile/usr/local/haproxy/logs/haproxy.pid #PID文件所在地
Option Nolinger #在连接关闭时立即清理连接 to reduce the connection in Fin_wait1 state
#debug
#quiet
Defaults
Log Global
Mode http #默认的模式mode {tcp|http|health},tcp is Layer 4, HTTP is 7 layer, health will only return OK
Option Httplog #启用http请求log
Option Dontlognull
Option Http-server-close
Option Abortonclose #当服务器负载很高的时候 to automatically end the link that is being processed for the current queue
Option Allbackups #但设置了backup的时候, the default first backup takes precedence, and all backup server weights are set after option Allbackups
Option Forwardfor #如果后端服务器需要获得客户端的真实IP需要配置次参数, the client IP will be available from the HTTP header
Option Redispatch #当serverId对应的服务器挂掉后, Force-directed to other healthy servers
Stats Uri/admin/status #监控haproxy状态的页面, you can use Http://IP/admin/status to view
Stats auth Admin:[email protected] #登录监控页面的用户名密码
Stats Refresh 60s #监控页面的刷新时间
Retries 3 #三次连接失败就认为是服务器不可用, can also be set by the following
Maxconn 65536 #默认的最大连接数
Contimeout #连接超时
Clitimeout 50000 #客户端超时
Srvtimeout 50000 #服务器超时
Frontend Web_nginx #定义前端服务器, equivalent to the concept of VIP in F5
Mode http
Maxconn 65536
Bind 192.168.32.30:80 #设定vip的监听端口
Bind-process 4 #针对多核处理, number of processors enabled
ACL Php_nginx path_end. php #判断文件后缀名是不是. PHP, if it returns true
ACL Html_nginx path_end. html #判断文件后缀名是不是. html, if True is returned
Use_backend Php_backend if Php_nginx #当满足php_nginx策略时使用php_backend的backend
Use_backend Html_backend if Html_nginx #当满足html_nginx策略时使用html_backend的backend
Backend Php_backend
Mode http
Fullconn 4096 #设置最大连接
Balance Roundrobin #负载均衡的方式, roundrobin average Way
Option Httpchk head/check.php http/1.0
#心跳检查Haproxy会判断你的后端web的根上存在check. txt does not, as the Haproxy-status monitoring state basis, it can be lost;
#如果是生产环境, you can change the check.php to index.jsp or index.php.
#一般的健康检查可以采用HEAD方法来做, instead of using the Get method, the head method has no data to return, just check that response's head is not 200. Relatively, faster, and simpler
Server Node1 192.168.32.31:80 check inter 2s minconn maxconn 2048 slowstart 10s weight 100
Server node2 192.168.32.32:80 check inter 2s minconn maxconn 2048 slowstart 10s weight 100
#服务器定义/ip/check Inter enable health Check to detect heartbeat frequency/minconn minimum number of connections/maxconn maximum connections/slowstart to full speed buffer time/weight
Backend Html_backend
Mode http
Fullconn 4096
Balance Roundrobin
Cookie ServerID #允许插入serverid到cookie中, ServerID can be defined later
Option Httpchk head/check.html http/1.0
Server Node2 192.168.32.32:80 cookie 2 check Inter 2s rise 3 Fall 3 Weight 100
Server Node3 192.168.32.33:80 cookie 3 check Inter 2s rise 3 Fall 3 Weight 100
#服务器定义, the cookie 1 indicates that the ServerID is 1,check inter 2s is the heartbeat rate detected
#rise 3 is 3 times correct to think that the server is available, Fall 3 is 3 times failed to think the server is unavailable, weight represents the weight
Haproxy------Configuring load Balancing under Windows