Forward TCP Connections with Iptables

Sometimes it is useful to forward a tcp connection to another server. For example if you want to switch a web service to another servers with no DNS delay.

You have to enable ip_forward and nat for the given port using iptables.

  1. echo 1 > /proc/sys/net/ipv4/ip_forward
  2. iptables -t nat -A PREROUTING -p tcp —dport 80 -j DNAT —to-destination someip:80
  3. iptables -t nat -A POSTROUTING -j MASQUERADE