This caused me a headache tonight. I’ll explain below how to forward requests from one Apache HTTP server to another on your network. Here are the main characters:
Server0: Your primary HTTP webserver that all external requests come to initially.
Server1: A secondary server you have setup (with Apache) to handle requests for xyz.com. The (internal) server IP is 192.168.2.10.
At the heart of making this possible is the mod_rewrite module available with Apache. This module seems very powerful and I only understand a small portion of it. First off you need to enable mod_rewrite and mod_proxy on Server0:
a2enmod rewrite a2enmod proxy /etc/init.d/apache2 force-reload
mod_proxy needs to be enabled and configured (proxy.conf), otherwise Apache won’t allow mod_rewrite to forward the request. Here is the relevant portion of proxy.conf (found in /etc/apache2/mods-enabled):
<Proxy *> Order deny,allow Deny from all </Proxy> <Proxy http://xyz.com> Order deny,allow Deny from all Allow from all </Proxy>
Now that you have the prerequisites ready, you can create the site definition on Server0 (/etc/apache2/sites-available/xyz.com):
<VirtualHost *> ServerName www.xyz.com # RewriteLog "/var/log/apache2/rewrite.log" # RewriteLogLevel 9 RewriteEngine On RewriteRule ^(.*)$ http://xyz.com$1 [P] ServerAlias xyz.com </VirtualHost>
Notice the rewrite rule, which will forward the request. Since xyz.com (on server0) will now point to server1, you need to update your /etc/hosts file accordingly:
192.168.2.10 xyz.com 192.168.2.10 www.xyz.com
Now enable the the new site and restart Apache:
a2ensite xyz.com /etc/init.d/apache2 restart
Now your done with Server0. You’ll be glad to know there is nothing out of the ordinary for you to do on Server1. All you have to do is create the site definition and enable (like you normally would). So in /etc/apache2/sites-available create a site definition called xyz.com:
<VirtualHost *> ServerName www.xyz.com DocumentRoot /var/www/xyz ServerAlias xyz.com </VirtualHost>
Then enable the site, restart Apache and everything should work. Odds have it something will go wrong and if it does, make sure to look in the Apache logs (/var/log/apache2/). Also, enable the rewrite log (see above). The logs will most likely always tell you exactly what went wrong.