Redirect WWW traffic while nameservers details are propagating

While I'm transferring the sites from one server to another I'm changing DNS settings to reflect the new IPs. After that, I usually wait 24-48 hours, until DNS will be served off.

These days I found best way to redirect traffic from one server to another, while the nameservers details are propagating.


Solution is simple - I used iptables (DNAT and SNAT).


iptables -t nat -A POSTROUTING -d new_server_ip \
-p tcp --dport 80 -j SNAT --to old_server_ip
iptables -t nat -A POSTROUTING -d new_server_ip \
-p tcp --dport 443 -j SNAT --to old_server_ip
iptables -t nat -A PREROUTING -d old_server_ip \
-p tcp --dport 80 -j DNAT --to new_server_ip
iptables -t nat -A PREROUTING -d old_server_ip \
-p tcp --dport 443 -j DNAT --to new_server_ip

To simplify the redirect process and to do the job quickly I wrote a small script. How to use it:

1) Download script:


2) Change OLD_IP and NEW_IP variables to match your new and old server IPs.

3) Make script executable and run it as root:

chmod u+x firewall.dns && ./firewall.dns