How do I smoothly migrate a web server's DNS from one IP address to another?

I currently have a domain name registered for a Linux/Apache server that I am going to replace with another on a new IP address.

Migrating the data will be relatively quick and a 5 minute disruption during that process is acceptable.

The DNS record has a TTL from 6-12 hours apparently, which I can't speed up.

What are the likely consequences of this change? Presumably users who are still looking at the old address will continue to hit the old server, while users who's dns cache has expired or is empty will see the new domain.

Is it possible to do some kind of redirect from the old server (with Apache or iptables) to the new IP? The old server can continue to run as long as necessary.


Solution 1:

You can use a Reverse Proxy on the old web server. It might be a bit of work to set up, but just as long as ITS DNS is up to date you will be OK.

What will happen is:

  1. Old web server is configured to be Reverse Proxy
  2. DNS Switchover
  3. New web server serves hits from up-to-date DNS records
  4. Old web when it gets a hit, forwards the request to the correct DNS, and then outputs the content verbatim.

If you're running Apache, look into mod_proxy. If you're running IIS, look into ISAPI Rewrite to get this sort of functionality.

(note that the DNS on the old web server needs to be up to date if you want to proxy using the domain name. Otherwise, proxy it directly to the IP address and make sure that the host is listening on the IP without a hostname)

Solution 2:

My company just did this with several largish Web sites. The basic procedure we followed was:

  1. Lower the domain's TTL as much as possible. Do this in advance by at least as much time as the current TTL.
  2. Set up the Web site on the new server exactly how you want the "final product" to be
  3. Add an aliased name to the site on the new server, such as www2.domain.com or www-new.domain.com. With Apache, you would use the ServerAlias directive. If the site has any dynamic code at all (PHP, mod_perl, RubyOnRails, etc.), make certain that the site will behave and respond correctly with this new name.
  4. At cutover time, set up a redirect on the old server pointing to the new server
  5. Change DNS for www to go to the new IP.

For Apache, you should probably use mod_rewrite for the redirect so you can preserve the URIs requested by the client. A simple implementation would be:

# old server
<VirtualHost 1.1.1.1:80>
    ServerName www.domain.com
    RewriteEngine on
    RewriteRule ^(.*)$ http://www-new.domain.com$1 [L]
</VirtualHost>

# new server
<VirtualHost 1.1.1.2:80>
    ServerName www.domain.com
    ServerAlias www-new.domain.com
</VirtualHost>

This will do a 302 temporary redirect for www.domain.com/anything to www-new.domain.com/anything. You want it to be temporary because you probably want search engines to only index www.domain.com, not www-new.domain.com.

Once the DNS change for www.domain.com has propagated to your satisfaction, you can either dump www-new altogether, or gently ease anyone using it back over to www with another redirect. It's almost the same process as above; set up the old server to handle www-new, change DNS for www-new to point to the old server, and set up a redirect on the old server sending www-new traffic to www:

# old server
<VirtualHost 1.1.1.1:80>
   ServerName www-new.domain.com
   RewriteEngine on
   RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
</VirtualHost>

# new server
<VirtualHost 1.1.1.2:80>
    ServerName www.domain.com
    # ServerAlias removed, no longer needed
</VirtualHost>

This time you want to do a permanent 301 redirect, again to clue in search engine crawlers that www.domain.com is the site you want them to index.

Solution 3:

Ok, based on what @Farseeker recommended, I set up the following config on the old Apache server to forward requests onto the new server:

<VirtualHost *>
  UseCanonicalName Off
  ServerAdmin  [email protected]
  ServerName domain.com
  DocumentRoot /var/www/

  ProxyRequests Off

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / http://domain.com/
  ProxyPassReverse / http://domain.com/
</VirtualHost>

To make sure the old server had the correct address, I put an entry in /etc/hosts:

1.2.3.4 domain.com

I also had to enable the Apache mod_proxy and mod_proxy_http modules, and reload the config:

a2enmod proxy
a2enmod proxy_http
/etc/init.d/apache force-reload