Skip to content

TCP 代理与协议转换

HAProxy 支持纯 TCP 层的负载均衡和协议转换。

TCP 代理基础配置

haproxy
global
    maxconn 8192

defaults
    mode tcp
    option tcplog
    timeout connect 10s
    timeout client 300s
    timeout server 300s

frontend tcp_front
    bind *:3306 name mysql
    bind *:5432 name postgres
    bind *:6379 name redis

    use_backend mysql_cluster if { dst_port 3306 }
    use_backend postgres_cluster if { dst_port 5432 }
    use_backend redis_cluster if { dst_port 6379 }

backend mysql_cluster
    mode tcp
    balance roundrobin
    option mysql-check user haproxy_check
    server mysql1 10.0.0.11:3306 maxconn 100 check

TLS Pass-through

haproxy
frontend tls_passthrough
    bind *:443
    mode tcp

    acl is_app1 req.ssl_sni -m end app1.example.com
    use_backend app1_backend if is_app1

backend app1_backend
    mode tcp
    server app1 10.0.4.11:443 check

TCP 健康检查

haproxy
    option tcp-check
    tcp-check connect port 22
    tcp-check send "PING\r\n"
    tcp-check expect string "PONG"