Reverse Proxy setup in Apache2 Ubuntu

I am running an apache server to serve a simple landing page say example.com. I am also running a play app server at port 9000 which I want to make it accessible by URL demo.example.com. Below steps will tell what needs to be done to get this working in a Ubuntu server.

1. I use AWS Route53 for DNS. So I added a CNAME record for demo.example.com to example.com. Please note specifying port numbers in Route53 will not work.

2. In Ubuntu 12.04, Apache2 default location is under /etc/apache2/. I wanted to set up a reverse proxy – that will direct requests to example.com to apache2 itself and requests to demo.example.com to port 9000. Before setting up reverse proxy we need to enable the required modules. This can be done by using a2enmod command.
    a2enmod
then, enter below modules:
    proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html
For me proxy_html was not installed, but not having this did not cause issues. I think proxy and proxy_http are the main modules required.

3. Next step is to edit the cofig file. Apache2 doc states the config file is httpd.conf, but in Ubuntu you could simply use
    /etc/apache2/sites-available/default

4. The above file already had an entry for the default virtual host as below:
    <VirtualHost *:80>
    ServerName example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www
    …
The ServerName entry was added newly by me.

5. I added another new Virtual host entry for demo.example.com like below:
    <VirtualHost *:80>
    ServerName demo.example.com
    ProxyPreserveHost On
    ProxyRequests off
    ProxyPass / http://myurl.amazonaws.com:9000/
    ProxyPassReverse / http://myurl.amazonaws.com:9000/
    </VirtualHost>
Give a proper URL with port number for ProxyPass and ProxyPassReverse.

6. Reload Apache2 and check!
     sudo service apache2 reload

Leave a comment