Haproxy网关设置——客户端和服务器在同一个子网

Haproxy gateway settings - client and server are on the same subnetwork

我正在尝试在服务器和客户端之间设置一个 haproxy 网关以实现完全透明的代理,如下图所示。我的主要目的是提供负载平衡。

有一个简单的应用程序在服务器端侦听端口 25。客户端尝试连接网关机器上的端口 25,网关上的 haproxy 选择一个可用的服务器,然后将连接重定向到服务器。

这种方法的网络分析产生如图所示的 tcp 流:客户端在最后重置连接,因为它没有向服务器发送 syn 数据包。

这个 haproxy 用法是否正确以及我的问题相关配置?或者客户端应该直接连接到服务器(这对我来说没有多大意义,但我实际上不确定。如果这是真的那么haproxy将如何干预连接并进行负载平衡)?

编辑:

我开始认为这个问题与网关上的路由和 NAT 有关。所有这三台机器都在同一个子网中,但我已经为客户端和服务器添加了到网关的路由。网关上的规则还有:

iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 0x01/0x01
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A PREROUTING -p tcp --dport 25 -j TPROXY \
         --tproxy-mark 0x1/0x1 --on-port 10025

ip route flush table 100
ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100

现在的问题是我应该在网关中如何将"syn-ack (src: S, dst: C)"更改为"syn-ack (src: GW, dst: C)"

Here是我的情况定义。

Here comes the transparent proxy mode: HAProxy can be configured to spoof the client IP address when establishing the TCP connection to the server. That way, the server thinks the connection comes from the client directly (of course, the server must answer back to HAProxy and not to the client, otherwise it can’t work: the client will get an acknowledge from the server IP while it has established the connection on HAProxy‘s IP).

答案是设置ip_nonlocal_bind系统控制。