Redirecting a domain to another local server

One of the reasons I decided to start a blog again (after giving up on my old one due to nothing to write about) was to document the various problems and solutions I have run into.  A lot of the time, system administration can be overwhelmingly frustrating, but there’s likely a simple solution.

In this case, it’s hard to explain the problem in brief other than “redirecting a domain to another local server”.  Basically, here was the scenario.  I have one IP.  Both my room mate and I have our own web server boxes, and we both want outside access to our respective websites through port 80 (the standard, default port).  With only a standard wireless router, the solution wasn’t obvious.  This is going to assume you know the basics of VirtualHosts (and that you have set the NameVirtualHost *:80 directive).

We decided to make one box the “master” (let’s give it an IP of 192.168.0.1) and the other the “slave” (192.168.0.2).  So, on the router, forward port 80 to 192.168.0.1.  Now, on the master box, we can make use of VirtualHosts.  In knowing that the first VirtualHost that is defined will be the default (if no other VirtualHost matches, the first defined VHost is used), we can allow all domain names that aren’t applicable to the master to forward on to the slave.


<VirtualHost *:80>
 ProxyPass / http://192.168.0.2/
 ProxyPassReverse / http://192.168.0.2/
 ProxyPreserveHost On
</VirtualHost>

Notice the “ProxyPreserveHost On” directive.  This is important if your slave box needs to support multiple domains.  This was where I struggled.  If this directive is not set to “on” (default is “off”), then the slave server will receive “192.168.0.2″ as the host and will be unable to differentiate between domains (such as “OtherDomain.com” versus “OtherDomain.ca”).

If you set this as the first “VirtualHost” on your master server, then you can follow it with all the VirtualHosts corresponding to domains on the master server.  All domains that aren’t defined on the master server (in the VirtualHost blocks after the first one) will be forwarded to the slave server.

On the slave server, you can just set up the VirtualHosts as your normally would.  Below is an example.


<VirtualHost *:80>
 ServerName SomeSlaveDomain.com
 ServerAlias www.SomeSlaveDomain.com
 DocumentRoot /srv/www/htdocs/SomeSlaveDomain
 ServerAdmin me@myemail.com
 <Directory /srv/www/htdocs/SomeSlaveDomain>
  AllowOverride None
  Order allow,deny
  Allow from all
 </Directory>
</VirtualHost>

Leave a Comment