Deploy Multiple Web Servers with ProxyPass on Ubuntu 12.04

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Deprecated

This guide has been deprecated and is no longer being maintained.

Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

In some cases, administrators find that while Apache meets most of their general-purpose web serving needs, other web or application servers are better suited for certain tasks. Fortunately, it’s easy to configure Apache to pass certain requests to other web server processes. These secondary (or tertiary) web servers may be running on the same Linode or separate nodes (perhaps via private networking). Our examples use lighttpd as a secondary web server, but they apply to any web server or application you’d like to proxy HTTP requests to.

We assume you already have Apache running on your Linode. If you don’t, you may wish to review our Apache on Ubuntu 12.04 (Precise Pangolin) guide before proceeding. These steps should be performed as root via a shell session.

Enable the Proxy Module

You must first edit the file /etc/apache2/mods-available/proxy.conf as follows:

File: /etc/apache2/mods-available/proxy.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<IfModule mod_proxy.c>
        #turning ProxyRequests on and allowing proxying from all may allow
        #spammers to use your proxy to send email.

        ProxyRequests Off

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

        # Enable/disable the handling of HTTP/1.1 "Via:" headers.
        # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
        # Set to one of: Off | On | Full | Block

        ProxyVia On
</IfModule>

This turns on proxy support in the module configuration.

Note
The warning regarding the ProxyRequests directive. It should be “off” in your configuration.

Next, we’ll issue the following commands:

a2enmod proxy
a2enmod proxy_http
service apache2 restart

Apache should restart cleanly. If you encounter any issues, you may wish to inspect the logs available under /var/log/apache2/ for more information.

Proxy a Domain to Lighttpd

We already have a site called “ www.firstsite.org” running under Apache as a normal virtual host. We’ll use Apache to send requests for the site “ www.secondsite.org” to lighttpd, which we’ve configured to run on port 8080 on localhost. Here’s the configuration file for “ www.secondsite.org”:

File: /etc/apache2/sites-available/www.secondsite.org
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<VirtualHost *:80>
     ServerAdmin support@secondsite.org
     ServerName secondsite.org
     ServerAlias www.secondsite.org

     ProxyPass / http://localhost:8080/

     # Uncomment the line below if your site uses SSL.
     #SSLProxyEngine On
</VirtualHost>

The ProxyPass directive tells Apache to forward all requests for this domain to a web server running on port 8080. If our target server was running on another Linode (as with a server that only answers on the backend private network), we could just specify that address instead. We’ll enable the site with the following commands:

a2ensite www.secondsite.org
service apache2 reload

Let’s do some testing. Here’s the normal Apache-served site “ www.firstsite.org” in our browser:

Website running under Apache on Ubuntu 10.04 (Lucid).

Here’s the site “ www.secondsite.org” being served by lighttpd via ProxyPass:

Website running under Lighttpd on Ubuntu 10.04 (Lucid).

Proxy a Specific URL to Lighttpd

If we wanted to have http://www.firstsite.org/myapp/ served by a web application running under lighttpd, we’d simply modify its configuration file to look like this:

File: /apache2/sites-available/www.firstsite.org
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<VirtualHost *:80>
     ServerAdmin support@firstsite.org
     ServerName firstsite.org
     ServerAlias www.firstsite.org
     DocumentRoot /srv/www/firstsite.org/public_html/
     ErrorLog /srv/www/firstsite.org/logs/error.log
     CustomLog /srv/www/firstsite.org/logs/access.log combined

     ProxyPass /myapp http://localhost:8080/
</VirtualHost>

Now the location “/myapp” will be served by lighttpd instead of Apache. After reloading the Apache configuration with /etc/init.d/apache2 reload, we can see that it’s functioning correctly:

Web application running under a directory via lighttpd on Ubuntu 10.04 (Lucid).

This is an easy method for hosting multiple application servers (with different web server requirements) under a single domain.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.