I am trying to forward traffic from one server to another, while keeping the origin requestor IP. Therefor I cannot use SNAT or MASQUERADE.
SERVER A:
Public IP: 111.111.111.111
Private IP: 10.0.0.1
SERVER B:
Public IP: 222.222.222.222
Private IP: 10.0.0.2
I want to forward traffic från Server A (111.111.111.111) to Server B (10.0.0.2).
This works fine:
iptables -t nat -A PREROUTING -d 111.111.111.111 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.2:80
iptables -t nat -A POSTROUTING -j MASQUERADE
However, because I am using MASQUERADE in this case, the destination server (10.0.0.2) sees all traffic as it would be comming from 111.111.111.111, i.e apache-logs and others are showing all requests as they are comming from 111.111.111.111
How can I setup this instead, so that the origin source IP-address is kept, like a home-router is doing it when using port forwarding.
I assume I need to setup a "route" somehow, so that the outgoing traffic from 10.0.0.2 goes out through Server A and not trying to respond on Server B's public IP?
proxy_set_header X-Forwarded-For $remote_addr;, see http://wiki.nginx.org/HttpProxyModule for more detail) and if what-ever is doing the logging is (or can be made) aware of this information (for Apache, configure the rpaf module to be aware of the new header, see http://stderr.net/apache/rpaf/). Using nginx as a proxy will add more latency than arranging this via NAT though, but the difference may be as small that you don't care. – David Spillett Jul 18 '13 at 10:23