Skip to content

核心配置语法

HAProxy 配置文件采用声明式语法,分为四大区块:globaldefaultsfrontendbackend。此外还有 listen 可将 frontend + backend 合并简写。

配置结构

global
    # 全局进程级配置

defaults
    # 默认配置,被后面的 frontend/backend 继承

frontend <name>
    # 监听入口,定义 VIP 和端口

backend <name>
    # 后端服务器池定义

listen <name>
    # 简写形式,等同于 frontend + backend 合并

global 区

全局配置,控制进程级别行为:

haproxy
global
    log stdout local0          # 日志输出
    maxconn 4096              # 单进程最大并发连接数
    user haproxy              # 运行用户
    group haproxy            # 运行组
    daemon                    # 后台运行
    pidfile /var/run/haproxy.pid

    # SSL 相关
    ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets

    # 性能调优
    tune.bufsize 16384        # 缓冲区大小
    tune.maxrewrite 1024      # 重写缓冲区大小
    spread-checks 5           # 健康检查时间抖动(%)

defaults 区

定义前端和后端的默认行为:

haproxy
defaults
    log      global
    mode     http            # 模式:http 或 tcp
    option   httplog         # 详细 HTTP 日志
    option   dontlognull     # 不记录空连接
    option   redispatch      # 服务器故障时重新分发
    retries  3               # 失败重试次数
    timeout  connect 5000   # 连接超时 (ms)
    timeout  client  50000  # 客户端超时 (ms)
    timeout  server  50000  # 服务端超时 (ms)
    timeout  check   5000   # 健康检查超时
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 503 /etc/haproxy/errors/503.http

frontend 区

定义监听端口和路由规则:

haproxy
frontend http_front
    bind *:80
    bind *:443 ssl crt /etc/ssl/certs/mycert.pem
    mode http

    # ACL 定义
    acl is_api      path_beg    /api
    acl is_static   path_beg    /static /images /css
    acl is_websocket hdr(Upgrade) -i websocket
    acl bad_robots  src -f /etc/haproxy/blocked_ips.lst

    # 动作
    http-request deny if bad_robots                   # 拒绝
    http-request redirect code 301 location https://%[hdr(host)]%[uri] if !{ ssl_fc }

    # 路由到后端
    default_backend             web_pool
    use_backend api_pool        if is_api
    use_backend static_pool     if is_static

backend 区

定义后端服务器池:

haproxy
backend web_pool
    mode http
    balance roundrobin              # 负载均衡算法
    option  httpchk GET /health     # 健康检查
    option  forwardfor except 127.0.0.0/8   # 传递客户端 IP

    # Cookie 会话保持
    cookie SERVERID insert indirect nocache

    # 后端服务器
    server web1 192.168.1.101:8080 cookie web1 check inter 2000ms rise 2 fall 3 weight 100
    server web2 192.168.1.102:8080 cookie web2 check inter 2000ms rise 2 fall 3 weight 100
    server web3 192.168.1.103:8080 cookie web3 check inter 2000ms rise 2 fall 3 weight 80 disabled

server 指令详解

server <name> <address>:<port> [params]

常用参数:

参数说明
check启用健康检查
inter <ms>健康检查间隔
rise <n>连续 n 次成功视为上线
fall <n>连续 n 次失败视为下线
weight <n>权重 (0-256)
cookie <value>Cookie 值,用于会话保持
disabled暂时禁用
backup备用服务器,只有其他都下线才启用
maxconn <n>该服务器最大并发连接数
slowstart <n>s启动时逐渐增加连接数

listen 简写

listen 将 frontend + backend 合并:

haproxy
listen web
    bind *:80
    balance roundrobin
    server s1 192.168.1.101:8080 check
    server s2 192.168.1.102:8080 check

等价于上面分写的 frontend + backend。

配置语法检查

bash
# 检查配置是否有效
haproxy -c -f /etc/haproxy/haproxy.cfg

# 输出类似:
# Configuration file is valid

⚠️ 生产环境修改配置前务必先做语法检查!