haproxy 透传客户端IP

发布时间 2023-05-25 10:50:03作者: 月月月月

[root@localhost ~]# yum -y install haproxy

[root@localhost ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info
defaults
log global
option tcplog
option httplog
option forwardfor
timeout client 30s
timeout server 30s
timeout connect 30s

frontend MyFrontend
bind *:80
default_backend TransparentBack_http

backend TransparentBack_http
mode http
source 0.0.0.0 usesrc client
server 192.168.174.139:80 192.168.174.139:80
server 192.168.174.139:90 192.168.174.139:90
listen stats
bind 192.168.3.68:9000
mode http
stats enable
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth 123:123

 

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 1
iptables -t mangle -A DIVERT -j ACCEPT
  1. ip rule add fwmark 1 lookup 100
  2. ip route add local 0.0.0.0/0 dev lo table 100

 

  1. [...]
  2. frontend ft_application
  3. bind 1.1.1.1:80 transparent
  4. [...]
  1. [...]
  2. backend bk_application
  3. source 0.0.0.0 usesrc clientip
  4. [...]