[CentOS] redirecting web requests from localhost

Thu Aug 29 10:53:31 UTC 2013
Carl T. Miller <carl at carltm.com>

Miguel González wrote:
>     However, the Java application running in the server tries to access
> some local web content. I have changed the hosts file and some
> applications (ping, wget) they get the local IP address. However
> nslookup and maybe our Java application (I didn´t have the programmer
> available to debug it) are getting the production server IP.
>     So, how can I redirect for instance 443 traffic to a specific IP to
> the local IP address of the local server? I have tried this:
>      iptables -t nat -A PREROUTING -d XXX.XXX.XXX.XXX -p tcp --dport 80
>    XXX.XXX.XXX.XXX - IP of production server
>    YYY.YYY.YYY.YYY - local IP of the test server

I'm not sure how to manage this on the test server, but
I'm pretty sure this would work on the prod server.

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp --dport 443 -s YYY.YYY.YYY.YYY \
-m conntrack --ctstate NEW -j DNAT --to YYY.YYY.YYY.YYY:443
iptables -t nat -A PREROUTING -m conntrack --ctstate \
iptables -A POSTROUTING -t nat -j MASQUERADE