Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save PiBa-NL/d826e0d6b35bbe4a5fc3 to your computer and use it in GitHub Desktop.
Save PiBa-NL/d826e0d6b35bbe4a5fc3 to your computer and use it in GitHub Desktop.
haproxy, sending the source ip to the webserver.
To send the ip addres of the client/webbrowser to the server/webserver behind it there are a few options:
1- option forwardfor
2- send-proxy
3- source 0.0.0.0 usesrc clientip
1- option forwardfor
This is an easy option to configure in haproxy, it does require that http layer7 processing is used 'mode http' and the webserver/ webapplication that wants to log or use the ip of the client must use the http-header 'X-Forwarded-For' to read the clientip.
2- send-proxy / send-proxy-v2 / send-proxy-*
This is can be used both with mode tcp and http, it does however require that the server also understands the proxyprotocol. Some applications have added support for this protocol which adds a few bytes with ip information before the actual request.
3- source 0.0.0.0 usesrc clientip
This allows any application and any protocol to be used and see the actual client ip as the origin from the incomming connection.
It does however require to configure IPTABLES or IPFW or other firewall rules to capture reply-traffic, also the haproxy machine must be the defaultroute for the return traffic from the (web-)server.
@PiBa-NL
Copy link
Author

PiBa-NL commented Jun 16, 2018

For IPFW the example rule would be something like this:
add <rulenumber> fwd localhost tcp from <webserverIP> <webserverPORT> to any in recv <webserver-facing-nic>
(I use that on my pfSense haproxy package..)

With IPTABLES:
Something like this perhaps?? i dont use that..: http://www.loadbalancer.org/blog/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy/

#!/bin/bash
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment