Forwarding traffic from one server to another

Jacob Allred
#linux

Yesterday I spent a good deal of time configuring a VPS to host my new German temporary email site, Wegwerf-eMail-Adresse. I really enjoy playing with servers so I had fun doing it, but I was still quite frustrated when my VPS host emailed me this morning letting me know that the server would be down for several hours next week for maintenance. Egh.

So I decided to move to a new host that is, hopefully, more reliable. But how to move the server without downtime?

Well the first step was to configure a new server. I have the process fine-tuned, so I was good to go with a fully updated server with all my required software, files, and databases within an hour.

The next step was to update my IP address for my domains. Again, fast and easy. Within a day or two everyone in the world should be getting directed to the new server from the existing domains.

The last step was to forward traffic from the old server to the new server. This part was vital. This site is data heavy and handles receiving mail. All that mail needed to go to the new server, and all that data needed saved in the new database. Waiting a day or two for that to happen would have caused lots of issues for my users. Luckily, a quick Google search gave the solution:

echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 88.198.87.248
iptables -t nat -A POSTROUTING -p tcp -d 88.198.87.248 --dport 80 -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp --dport 25 -j DNAT --to-destination 88.198.87.248
iptables -t nat -A POSTROUTING -p tcp -d 88.198.87.248 --dport 25 -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp --dport 6969 -j DNAT --to-destination 88.198.87.248
iptables -t nat -A POSTROUTING -p tcp -d 88.198.87.248 --dport 6969 -j MASQUERADE

The first line enables forwarding for ipv4, and the next lines turns it on for ports 80, 25, and 6969 (the ports I care about) to IP 88.198.87.248 (my new server).

Voila! Seamless transition with virtually no downtime.