Use Apache Proxy For a Site Launch Without a 24 Hour Lag

April 8th, 2009 by Benjamin Sweet

One of the most rewarding activities when working on a website project is “pressing the button” to make the site go live. Here at ForeSite we have a virtual “gong” that we bang whenever a site launches, usually followed by high-fives and handshakes.

However this activity doesn’t work as well when the launch requires a DNS change, as is often required when taking over the hosting of a new site. Most DNS changes take about 24 hours, though some networks may get the change earlier than others.

This makes pinpointing the actual “launch time” of a new site difficult. If the site uses a database that is being migrated this issue is compounded by not knowing when, exactly, the database can be cut over.

One solution: Putting an “Under construction” or other placeholder page on the old site while waiting for the DNS change to take effect.

The problem with this solution is that you may lose some visitors who may choose not to come back, and, quite frankly, it seems unprofessional.

Better solution: Use Apache proxy to “fetch” content from the old site, allowing you to cut over the DNS entries early. Then, at launch time, simply remove the proxy.

This solution will allow you to know exactly when the site is live because you have complete control over the cutover.

To do this, using Apache, do the following:

Ensure Apache has mod_proxy enabled

Using your favorite text editor (I like vim), open the httpd.conf file and look for mod_proxy.so. Ensure that the line LoadModule proxy_module modules/mod_proxy.so is enabled. If it is not, enable it.

Setup your hosts file

Again using your favorite text editor edit your /etc/hosts file on the server the new site will be on. Add an entry with the website domain (www.mydomain.com) with the IP address of the existing (old) server.

Add your virtual host

If you do not yet have a virtual host setup for the new website, create it as you normally would, however you will need to add two lines to the virtual host to add proxy support.

Your full virtual host will look something like this:

<VirtualHost 192.168.1.10:80>
ServerAdmin webmaster@mydomain.com
ServerName www.mydomain.com
ProxyPass / http://www.mydomain.com/
ProxyPassReverse / http://www.mydomain.com/

DocumentRoot /app/production/mydocument/www
ErrorLog /app/logs/production/mydomain/www/error_log
CustomLog /app/logs/production/mydomain/www/ssl_access_log combined
</VirtualHost>

Restart apache

Restart apache to pick up the virtual host change (using apachectl graceful).

Testing the proxy

Testing requires a little more effort on your local machine. If you are using Windows, open c:\Windows\System32\Drivers\etc\hosts in notepad.

Add an entry at the bottom for the domain with the IP address of the new server.

Using your web browser, visit the domain. If you see the old site everything is configured properly. If you see the new site, or have an error, something is wrong.

Remember to remove the entry from your own hosts file so you don’t get confused later.

Making the change

Now that you know the proxy is working, you can now change the DNS entry for the domain. Change the A record for the domain to be the new server.

After about 24 hours the DNS change should take effect everywhere.

You now have control

You now have complete control over what a website visitor will see when visiting the domain name. Though the user sees the old site, all of the traffic is going through the new server. You can disable the proxy at any time to make the new site “live” at exactly the moment you specify. To do that, remove the two proxy lines from the virtual host entry, and then restart apache.

Downsides to this method

There are two downsides to this method:

  1. Your servers are taking on the load of all requests to the old site, and you probably haven’t started billing for hosting yet. This may not be a huge problem if the site doesn’t get a lot of traffic.
  2. Logs (and log based analytics) will show the wrong data on the old server. This is because all of the “requests” will appear to be coming from the new server. If analytics from the old site is important, ensure that Google Analytics is on the site (or another javascript based analytics system), as this type of analytics is unaffected by this process.

Leave a Reply