1
|
mv /usr/local/haproxy/conf/option-http_proxy.cfg /usr/local/haproxy/conf/option-http_proxy.cfg_bak |
1
|
vim /usr/local/haproxy/conf/option-http_proxy.cfg
|
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 |
global maxconn 20000 log 127.0.0.1 local0 info uid 0 gid 0 chroot /usr/local/haproxy nbproc 4 daemon defaults mode http retries 3 timeout connect 10s timeout client 20s timeout server 30s timeout check 2s frontend test-proxy bind *:80 mode http log global default_backend test-proxy-srv backend test-proxy-srv balance roundrobin option http-server-close option httpchk GET /index.html http-check expect status 200 server web1 IP1:PORT1 weight 3 server web2 IP2:PORT2 weight 3 |
配置文件示例参数说明见表1。
参数 |
说明 |
---|---|
global |
- |
maxconn 20000 |
默认最大连接数。 |
log 127.0.0.1 local0 info |
定义日志输出设备,info表示日志级别。 |
uid 0 |
运行HAProxy的用户id。 |
gid 0 |
运行HAProxy的用户组id。 |
chroot /usr/local/haproxy |
chroot运行路径。 |
nbproc 4 |
设置进程数量。 |
daemon |
以后台形式运行HAProxy。 |
defaults |
- |
mode http |
所处理的类别(7层代理http,4层代理tcp)。 |
retries 3 |
设置连接后端服务器的失败重试次数,超过此值标记后端服务器为不可用。 |
timeout connect 10s |
HAProxy与后端服务器建立连接的最长等待时间。 |
timeout client 20s |
和客户端保持空闲连接的超时时间。 |
timeout server 30s |
和服务端保持空闲连接的超时时间。 |
timeout check 2s |
对服务端的检测超时时间。 |
frontend test-proxy |
- |
bind *:80 |
定义一个或几个监测的套接字,*表示当前所有的ipv4地址。 |
mode http |
所处理的类别(7层代理http,4层代理tcp)。 |
log global |
继承global中log的定义。 |
default_backend test-proxy-srv |
指定默认的后端服务器池。 |
backend test-proxy-srv |
- |
balance roundrobin |
指定负载均衡算法roundrobin是基于权重进行轮询的算法,适用于服务器性能均匀时。 |
option http-server-close |
当开启长连接时,应该开启此项。 |
option httpchk GET /index.html http-check expect status 200 |
启用HTTP的服务状态检测(健康检查)。检查返回的状态码,接受不到200就不给后端server调度。 |
server web1 IP1:PORT1 weight 3 server web2 IP2:PORT2 weight 3 |
定义多个后端真实服务器。 格式:server <name> <address>:[port] [param*]
说明:
IP1:PORT1和IP2:PORT2为后端服务器IP及端口号。 weight表示权重。 |
1
|
taskset -c 0-3 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/option-http_proxy.cfg |
如果启动报错“cannot bind socket [0.0.0.0:80]”,查看占用80端口的进程,杀死该进程后再启动HAProxy。
1 2 |
netstat -anpt | grep 80 kill -9 136508 |
1
|
ps -ef | grep haproxy |
可以看到HAProxy进程。
打开浏览器在url处输入:http://IP:80,(IP为HAproxy所在服务器IP)可以看到后端服务器的页面,则说明HAProxy运行成功。刷新页面,显示页面在后端服务器间来回切换。
1
|
pkill haproxy
|