TPROXY不适用于HAProxy,Ubuntu 14.04

我试图在Ubuntu 14.04中使用HAProxy作为使用TPROXY的完全透明代理。 HAProxy将在第一台服务器上111.111.250.250 ,其中eth0 111.111.250.250eth1 10.111.128.134 。 单个平衡服务器也有eth1eth0eth0是面向公众的networking接口,而eth1是两个服务器所在的专用networking。

问题:我可以直接(通过eth1 )连接到平衡服务器的端口1234,但无法通过Haproxy端口1234(通过eth0redirect到1234)到达平衡服务器。 我错过了这个configuration中的东西吗?

/etc/haproxy/haproxy.cfg删除线source 0.0.0.0 usesrc clientip ,haproxy工作但不透明。


在HAProxy服务器上

目前的内核是3.13.0-24-genericiptables v1.4.21 ,我认为它支持iptables v1.4.21 ,但不知道如何检查。

内核似乎有TPROXY支持:

 # grep TPROXY /boot/config-3.13.0-24-generic CONFIG_NETFILTER_XT_TARGET_TPROXY=m 

HAProxy编译与TPROXY支持:

 haproxy -vv HA-Proxy version 1.5.3 2014/07/25 Copyright 2000-2014 Willy Tarreau <[email protected]> Build options : TARGET = linux26 CPU = x86_64 CC = gcc CFLAGS = -g -fno-strict-aliasing OPTIONS = USE_LINUX_TPROXY=1 USE_LIBCRYPT=1 USE_STATIC_PCRE=1 Default settings : maxconn = 2000, bufsize = 16384, maxrewrite = 8192, maxpollevents = 200 Encrypted password support via crypt(3): yes Built without zlib support (USE_ZLIB not set) Compression algorithms supported : identity Built without OpenSSL support (USE_OPENSSL not set) Built with PCRE version : 8.31 2012-07-06 PCRE library supports JIT : no (USE_PCRE_JIT not set) Built with transparent proxy support using: IP_TRANSPARENT IPV6_TRANSPARENT IP_FREEBIND Available polling systems : epoll : pref=300, test result OK poll : pref=200, test result OK select : pref=150, test result OK Total: 3 (3 usable), will use epoll. 

/etc/haproxy/haproxy.cfg ,我已经configuration了一个端口来拥有以下选项:

 listen test1235 :1234 mode tcp option tcplog balance leastconn source 0.0.0.0 usesrc clientip server balanced1 10.111.163.76:1234 check inter 5s rise 2 fall 4 weight 4 

下面的iptables规则被添加了

 iptables -t mangle -N DIVERT iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT iptables -t mangle -A DIVERT -j MARK --set-mark 111 iptables -t mangle -A DIVERT -j ACCEPT ip rule add fwmark 111 lookup 100 ip route add local 0.0.0.0/0 dev lo table 100 sudo ip route flush cache 

 echo 1 >/proc/sys/net/ipv4/ip_forward 

在平衡的服务器上

/etc/networking/interfaces我已经将eth1的网关设置为HAProxy框10.111.128.134并重新启动了networking连接。

 auto eth0 eth1 iface eth0 inet static address 111.111.250.250 netmask 255.255.224.0 gateway 111.131.224.1 dns-nameservers 8.8.4.4 8.8.8.8 209.244.0.3 iface eth1 inet static address 10.111.163.76 netmask 255.255.0.0 gateway 10.111.128.134 

ip route给出:

 default via 111.111.224.1 dev eth0 10.111.0.0/16 dev eth1 proto kernel scope link src 10.111.163.76 111.111.224.0/19 dev eth0 proto kernel scope link src 111.111.250.250 

我相信你的问题可能与没有转发或NAT规则有关,但你需要转发eth0 < – > eth1之间的stream量的规则。

即:

 Chain FORWARD (policy ACCEPT 7601 packets, 661K bytes) pkts bytes target prot opt in out source destination 18M 47G ACCEPT all -- eth1 eth0 0.0.0.0/0 0.0.0.0/0 17M 2922M ACCEPT all -- eth0 eth1 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 

另外有一件事情,在我使用多个设备时,在前面指定了mangle表上的接口设备:

 Chain PREROUTING (policy ACCEPT 35M packets, 50G bytes) pkts bytes target prot opt in out source destination 59M 52G divert tcp -- eth1 * 0.0.0.0/0 0.0.0.0/0 socket 

在eth1不应该指定的地​​方,我们使用eth0 / eth1甚至bond0和eth1。

最后,你要确保你有自己的规则,即:

 Chain POSTROUTING (policy ACCEPT 1420K packets, 85M bytes) pkts bytes target prot opt in out source destination 1K 494M PUBLICSNAT all -- * eth1 0.0.0.0/0 0.0.0.0/0 Chain PUBLICSNAT (1 references) pkts bytes target prot opt in out source destination 1K 494M SNAT all -- * eth1 10.1.1.0/24 0.0.0.0/0 to:8.8.8.8-8.8.8.10 1K 336K RETURN all -- * * 0.0.0.0/0 0.0.0.0/0