Apache: Forwarding Requests to Another Server

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:

Code:
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):

Code:
<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):

Code:
<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:

Code:
192.168.2.10    xyz.com
192.168.2.10    www.xyz.com

Now enable the the new site and restart Apache:

Code:
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:

Code:
<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.

Related Articles:

7 Responsesto “Apache: Forwarding Requests to Another Server”

  1. j1nn says:

    thanks for the tips.
    however, I need a small addition in order to make this work:
    a2enmod proxy_http after all the above.

    hope it will help someone

  2. Tim says:

    Yep, mod_proxy_http is also required. mod_proxy is the dependency module, but mod_proxy_http is what lets this magic happen. Otherwise I get an internal server error for the configuration, that there were no valid proxies defined for the match.

    Also, the directive isn’t going to let anything through with that “Deny from all”. With the configuration given above, all I got was denial of access messages for the paths I was requesting. Instead, you’ve got to open it up to allow stuff. “Allow from all” seems to be safe since this is a “reverse” proxy, which is the desired behavior of a front-end to access multiple servers behind the magic curtain.

    After that, everything comes together nicely.

  3. Tim says:

    “Also, the directive* isn’t going…”

    Oops! I meant that the “” directory isn’t going to let anything through.

  4. Tim says:

    Screw auto HTML stripping. The “[Proxy *]” directive.

  5. Simon says:

    Awsome! Got this working with the guide.
    Only, does anyone know how to make this work with https on Server1 as well?

  6. Randy says:

    Success. :) Hey man, thank you for this post. Things change so fast but with a decent older reference like this it’s gets the job done. :)

    I will add 2 more cents worth if you’ll kindly allow.

    I did not need the local host set in /etc/hosts. Instead it works to change a vhosts line all follows ..

    RewriteRule ^(.*)$ http://192.168.2.10:81$1 [P] # instead of this ..
    RewriteRule ^(.*)$ http://xyz.com$1 [P]

    Use the :81 (or your choice port) to specify the port number. Routers I have used can only open a port to one local computer. So then open port 81 to the Server1 in this example.

    One other thing a bit different. I followed the examples at http://httpd.apache.org/docs/2.0/mod/mod_proxy.html and this is what is working for the proxy.conf ..

    ProxyRequests Off

    Order deny,allow
    Allow from all

    Don’t be fooled by “ProxyRequest Off”. For reverse proxy things work anyway as per apache.org examples.

    Oh, one more, remember to enable one companion module or things won’t work ..

    a2enmod proxy_http

  7. private proxy says:

    Fantastic information. I liked that article.

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>