Reverse Proxy (with Apache)

I did a little write-up on how I achieved reverse proxy nirvana with IIS a while ago now. It made things a lot nicer for me. Like, a lot.

I have a few friends/colleagues/acquaintances who run Apache (I run IIS in my homelab environment, that’s the only reason I went with it), so I figured I’d look into how I can help them do the same thing (only a little different).

Firstly, you need to install the components to allow Apache to actually route things. It’s fairly straightforward to do, and simply requires you have root access to the shell on your server.

There are two main components that you need:
libapache2-mod-proxy-html and libxml2-dev.
Install them with:

sudo apt-get install -y libapache2-mod-proxy-html libxml2-dev

You’ll then need to activate them. ‘a2enmod’ comes into play here

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_ajp
sudo a2enmod rewrite
sudo a2enmod deflate
sudo a2enmod headers
sudo a2enmod proxy_balancer
sudo a2enmod proxy_connect
sudo a2enmod proxy_html

We can then actually configure the reverse proxy settings.
These are done in the config file for your virtualhost. Usually these are in /etc/apache2/sites-enabled – the files in there should simply be symlinks to their counterparts in /etc/apache2/sites-available.
Pick the config file for the site you want to set up sub-proxy for. Edit it using:
/code sudo nano -w /etc/apache2/sites-enabled/%site-config.conf%

All the config needs to be put inside the virtualhost section of the config – inside the section enclosed with <VirtualHost /> tags.
Add this line below the DocumentRoot entry (this tells it to pass the actual source through to the proxied application):

ProxyPreserveHost On

I tend to add comments to things I edit so I can remember why I edited it. Remember to put a “#” in front of it to mark it as a comment instead of actual code.
Near the bottom of the config, I put the actual ‘proxypass’ entries into place (just before the final virtualhost close tag). They work elsewhere in the config – I just find it easier to put custom config near the bottom of config files where possible, so it’s consistently easy to find.
Add in entries for each thing you want to proxy. Make sure the item you are proxying has a baseurl configured – that way it doesn’t panic when the url passed to it is ‘application/base/argument’, rather than the expected ‘application/argument’.

# Sonarr
ProxyPass /sonarr
ProxyPassReverse /sonarr
# Radarr
ProxyPass /radarr
ProxyPassReverse /radarr

Save the config using CTRL+O (‘o’ for ‘overwrite’ with nano)
Answer ‘yes’ if it prompts.
Restart apache to apply the changes.

service apache2 restart

This should allow you to access the software on “http://server/sonarr”, “http://server/radarr” etc – rather than remembering the port for each application, and forwarding ports for each as well. It also means you can set up an SSL connection to secure it – but that’s a topic for another writeup.

Permanent link to this article: