Haproxy 之常用配置详解

HAProxy 配置中有三个重要部分

  • global
  • defaults
  • listen or (frontend + backend)

常用配置详解

全局配置(global)

参数名 值 参数含义
maxconn 20480 默认最大连接数
daemon 以后台形式运行haproxy
nbproc 1 进程数量(可以设置多个进程提高性能)
pidfile /var/run/haproxy.pid haproxy 的 pid 存放路径,启动进程的用户必须有权限访问此文件

默认全局配置(defaults)

这些参数可以被利用配置到frontend,backend,listen组件

option 参数可以同时配置多个

balance 只可配置一个*

参数名 值 参数含义
log global
mode http 所处理的类别 (#七层:http;四层:tcp )
maxconn 20480 最大连接数
option httplog 日志类别为 http 日志格式
option httpclose 每次请求完毕后主动关闭 http 通道
option dontlognull 不记录健康检查的日志信息
option forwardfor 记录客户端 IP 在 X-Forwarded-For 头域中,用以获取客户端真实 ip
option redispatch serverId 对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose 当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
stats refresh 30 统计页面刷新间隔
retries 3 3次连接失败就认为服务不可用,也可以通过后面设置
balance roundrobin 默认的负载均衡的方式,轮询方式
balance source 默认的负载均衡的方式,类似 nginx 的 ip_hash
balance leastconn 默认的负载均衡的方式,最小连接
timeout connect 5000ms 连接 server 端超时 5s
timeout client 50000ms 客户端响应超时 50s
timeout server 50000ms server 端响应超时 50s
timeout check 2000 心跳检测超时时间

监控页面的设置

listen 的配置参数

参数名 值 参数含义
listen listen_demo 监控组名称,是 Frontend 和 Backend 的组合体
bind 0.0.0.0:65532 供外部访问的地址及端口
mode http 代理模式
log 127.0.0.1 local3 err 错误日志记录

HTTP 模式的相关设置

参数名 值 参数含义
stats refresh 5s 监控页自动刷新时间
stats uri /admin?stats 监控页的地址
stats realm /info 监控面的提示信息
stats auth admin:admin 监控页鉴权,可以同时设置多个用户
stats hide-version 隐藏统计页面上的 HAproxy 版本信息
errorfile 403 /etc/haproxy/errorfiles/403.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

frontend 的配置参数

参数名 值 参数含义
frontend frontend_name
bind 0.0.0.0:1080 供外部访问的地址及端口
mode http
log global 应用全局的日志配置
option httplog 启用 http 的 log
option httpclose 每次请求完毕后主动关闭 http 通道,HA-Proxy 不支持 keep-alive 模式
option forwardfor 如果后端服务器需要获得客户端的真实 IP 需要配置次参数,将可以从 Http Header 中获得客户端 IP
default_backend servers 请求转发至名为 servers 的后端服务

backend 的配置参数

参数名 值 参数含义
backend servers 后端服务名,可自定义
mode http
balance roundrobin 负载均衡,轮询方式
option httpchk GET /index.html 启动心跳检测的地址
server

backend 中 server 的参数

  • server web1 192.168.11.2:80 check inter 1500 rise 3 fall 3 weight 1
参数名 值 参数含义
server server1 server 名
check inter 1500 心跳检测频率
rise 3 3 次正确认为服务器可用
fall 3 3 次失败认为服务器不可用
weight 2 代表权重如果检查失败会自动踢掉该服务器

配置自由的时间单位

一些参数涉及表示时间的值,例如超时。这些值通常以毫秒表示(除非有特殊说明),但可以通过改变后缀单位来改变这些值。

支持的单位是:

1
2
3
4
5
6
- us : microseconds. 1 microsecond = 1/1000000 second
- ms : milliseconds. 1 millisecond = 1/1000 second. This is the default.
- s : seconds. 1s = 1000ms
- m : minutes. 1m = 60s = 60000ms
- h : hours. 1h = 60m = 3600s = 3600000ms
- d : days. 1d = 24h = 1440m = 86400s = 86400000ms

相关命令

1
2
3
4
5
6
7
8
9
10
# 修改配置并重启
sudo vi /etc/haproxy/haproxy.cfg
haproxy -c -f /etc/haproxy/haproxy.cfg
sudo service haproxy restart

# 设置权重
echo 'set weight read_only-back/slave1 0' | sudo socat stdio /run/haproxy/admin.sock

echo 'set server read_only-back/slave1 agent up' | sudo socat stdio /run/haproxy/admin.sock


了解更多

HAProxy 各版本官方文档