HOWTO: Proxy your internal websites via a single apache2 server

So, you have multiple web applications running on multiple computers and don't want to configure NAT on your router? Well, this is your lucky day! Let Apache's reverse proxy come to the rescue!

First, you need to enable couple of modules

sudo a2enmod proxy
sudo a2enmod proxy_html

Once your modules have been enabled, restart your apache2 server

sudo service apache2 restart

Next, you'll need to modify the proxy.conf file by running:

sudo nano /etc/apache2/mods-enabled/proxy.conf

and make it look somewhat similar to this:

<IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        SSLProxyEngine On
        ProxyVia Off
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>
</IfModule>

When you're done editing proxy.conf file, press Ctrl+O to save the file and Ctrl+X to exit nano editor.

Now, you're ready to specify your reverse proxy settings.
First create a new file in /etc/apache2/sites-available and call it something like reverseProxy

sudo nano /etc/apache2/sites-available/reverseProxy

and place your VirtualHost directives there like so:

<VirtualHost *:80>
  ServerName hostname1.mydomain.com
  DocumentRoot /var/www
 
  ProxyPass / http://192.168.1.25/MyAppName1/
  ProxyPassReverse / http://192.168.1.25/MyAppName1/
</VirtualHost>
 
<VirtualHost *:80>
  ServerName hostname2.mydomain.com
  DocumentRoot /var/www
 
  ProxyPass / http://localhost:8092/MyAppName2/
  ProxyPassReverse / http://localhost:8092/MyAppName2/
</VirtualHost>

Now that you've configured your virtual hosts, you have to enable your new reverse proxy site by running the following commands:

sudo a2ensite reverseProxy
sudo service apache2 reload

After this, your configuration is complete, from Apache's side. Next, you'll need to configure your DNS to allow connections to your hostnames. In my case, I have a Custom DNS Service with DynDNS. To configure your DynDNS, follow these simple steps:
1) Log in to your DynDNS account
2) Navigate to 'My Zones/Domains'
3) Click on 'Custom DNS Service'
4) Click on 'Add Alias (CNAME)' button at the bottom of the screen
5) Here you have a choice. You can either create a wildcard hostname that would look like *.mydomain.com or you can create a specific hostname such as hostname1.mydomain.com
From what I understand, when you have wildcard hostname, anything you type in before your domain name will get forwarded to your apache server. Then, apache will take the hostname and match it to one of its Location or ServerName directives.

Once you're done with DNS configuration, you are ready to point your browser to http://hostname1.mydomain.com and http://hostname2.mydomain.com to view websites hosted on your machines inside your local network.

Please note that I'm no Apache expert and some of the information might not be 100% correct. What I've described here works for me, but it could be not as secure as one might hope for. I welcome your comments and suggestions :)

Comments