Tutorial: Apache 2.4 as reverse proxy

This post explains how to configure Apache 2.4 (the version that comes with Ubuntu 14.04) as a fully transparent reverse proxy. If you have a single website that has multiple paths that are actually run by different web applications then this tutorial may be for you.


The proxy will serve both web applications from their own virtual host configuration. These may be on the same machine as shown below using the loop-back addresses and or on different machines if you use their (internal) IP addresses.

Site: http://www.yourwebsite.com/
App1: http://www.yourwebsite.com/app1 =
App2: http://www.yourwebsite.com/app2 =

This is the directory structure in which I want to load the various web apps:

maurits@nuc:/var/www/html$ ll
total 28
drwxr-xr-x 4 root root  4096 Dec  1 21:43 ./
drwxr-xr-x 3 root root  4096 Apr 21  2014 ../
-rw-r--r-- 1 root root 11510 Apr 21  2014 index.html
drwxr-xr-x 2 root root  4096 Dec  1 21:45 app1/
drwxr-xr-x 2 root root  4096 Dec  1 21:45 app2/

In this tutorial we run the web applications on the same paths as on the proxy. This means that the web apps run in a subdirectory, even on the machines behind the proxy. This avoids the need of rewriting and thus keeps this setup simple and easy to debug.

Setting up the reverse proxy in Apache 2.4

What we are going to do is setup a reverse proxy. First we load the “proxy_http” module in Apache 2.4 using:

sudo a2enmod proxy_http
sudo service apache2 restart

Let’s setup the reverse proxy virtual host configuration in “/etc/apache2/sites-available/yourwebsite-proxy.conf” like this:

<VirtualHost *:80>
ServerName www.yourwebsite.com
DocumentRoot /var/www/html
ProxyPreserveHost On
ProxyPass /app1
ProxyPass /app2

The virtual host configuration of app1 in “/etc/apache2/sites-available/yourwebsite-app1.conf” looks like this:

ServerName www.yourwebsite.com
DocumentRoot /var/www/html

And the virtual host configuration of app2 in “/etc/apache2/sites-available/yourwebsite-app2.conf” looks like this:

ServerName www.yourwebsite.com
DocumentRoot /var/www/html

Lets enable all sites and reload Apache using:

sudo a2ensite yourwebsite-proxy yourwebsite-app1 yourwebsite-app2
sudo service apache2 reload

Note that this works as the virtual host configurations with a specified IP address will be matched first. The “ProxyPreserveHost” will make sure the “Host” header in the request is not rewritten. The lack of a “ProxyPassReverse” will make sure that there is no rewriting done on the response.

Showing the correct remote IP address

It is important to understand that in the above setup, the proxied web application will only see a different “REMOTE_ADDR” environment variable, since there is absolutely no rewriting going on. The real visitor address is passed along in “X-Forwarded-For” header. This is a comma separated list and the last entry holds the real client IP address.

If you are on Apache 2.4, like in Ubuntu 14.04, you can correct the reported remote address by loading the “remoteip” module like this:

sudo a2enmod remoteip
sudo service apache2 restart

Add the “RemoteIPHeader” and “RemoteIPInternalProxy” directives to the virtual host configurations:

ServerName www.yourwebsite.com
DocumentRoot /var/www/html
RemoteIPHeader X-Forwarded-For

Note that the “RemoteIPInternalProxy” you must specify the internal IP address of the proxy. To test if you did it right you can run a PHP script that calls “phpinfo()”. If you see that the “REMOTE_ADDR” value is not set to the proxy, then it is working.

Adding headers to the upstream request

We want to make Apache2 add upstream headers and therefor we need to load the “headers” module in Apache 2.4 using:

sudo a2enmod headers
sudo service apache2 restart

Next, we have to adjust the reverse proxy virtual host configuration in “/etc/apache2/sites-available/yourwebsite-proxy.conf” like this:

<VirtualHost *:80>
ServerName www.yourwebsite.com
DocumentRoot /var/www/html
ProxyPreserveHost On
RewriteEngine On
RequestHeader add X-SSL off
RewriteRule ^/app1/(.*)$1 [P,L]
RewriteRule ^/app2/(.*)$1 [P,L]

In this example we add a “X-SSL” header with the value “off” to the proxied request. If you want to add headers to the response you can use the “Header” directive.

If you have any questions, please use the comments below.


5 thoughts on “Tutorial: Apache 2.4 as reverse proxy”

  1. Hi Guys,
    Can somebody help me to setup following requirement required by my client. we have end application which only support Digest authentication and the calling application only support basic authentication, I am planning to set apache proxy between this two server.

    if you have some sample to cater above request on SSL layer.

    Nagsen Dambhare

  2. Hi.

    Can I have an example with different servers (that means with different internal IP addresses)?
    I have one public ip address (and a domain), which points to my front end. I want to have a videoconference app at http://www.blablabla.com/videoconf

    With ProxyPass I can reach the login ui for the videoconference, but when I try to login it says “/home is not in this server” refering as this server to the first server that I want to jump off.

    How can I make this work?


  3. @Miguel: There is no apparent reason why this should not work. I need more information about your specific setup to understand why this is not working. Maybe you can try analyzing the browser network connections and compare those with the log files. Check which URLs are requested and whether or not they end up on the reverse proxy. It may shed some light on things. 🙂 Hope it helps!

  4. Hi,

    I have following structure:
    Apache1 2.4 ->WLS 12
    ->Apache2 2.4->Glassfish
    Apache1 directs /AAA to WLS12 and /aaa to Apache2->GF.
    I configured ProxyPass and ProxyPassReverse but it doesn’t work.
    I don’t think i am in the same situation as you wrote in the article… Do you know what would be the approach for this structure?

    Thank you

  5. @alex: So you are doing double forwarding? You may also need to do double rewriting in that case. Where do you ProxyPass and ProxyPassReverse?

Leave a Reply

Your email address will not be published. Required fields are marked *