Forwarding traffic from one server to another
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 188.8.131.52 iptables -t nat -A POSTROUTING -p tcp -d 184.108.40.206 –-dport 80 -j MASQUERADE iptables -t nat -A PREROUTING -p tcp –-dport 25 -j DNAT –-to-destination 220.127.116.11 iptables -t nat -A POSTROUTING -p tcp -d 18.104.22.168 –-dport 25 -j MASQUERADE iptables -t nat -A PREROUTING -p tcp –-dport 6969 -j DNAT –-to-destination 22.214.171.124 iptables -t nat -A POSTROUTING -p tcp -d 126.96.36.199 –-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 188.8.131.52 (my new server).
Voila! Seamless transition with virtually no downtime.