Apache reverse proxy to forward requests on a custom port to a local system

By | 16th December 2020

This configuration of apache is a little specific to forwarding all requests coming to a custom port on the host system to a local system which maybe running a service on custom port.

Let us say we have 2 systems. System A and B where A can be accessed from the internet on lets say port 8084 and system B which is in the local system is hosting a service on port 8700. Now if someone from outside wants to access B:8700 then they can not do it directly unless we find a way to redirect requests to it from the public facing system A. Apache reverse proxy comes handy to do the job and it is relatively easy to configure.

The following setup is tested on debian bullseye with apache 2.4. The same may work on other systems to as it is fairly simple to setup.

On system A

First make sure apache is installed and running if not install it with apt-get install apache2. Then follow these steps.

  1. Enable these modules for reverse proxy. use the command a2enmod. Some of the modules may not be needed I just enabled them following a guide.
    1. proxy
    2. proxy_http
    3. proxy_ajp
    4. rewrite
    5. deflate
    6. headers
    7. proxy_balancer
    8. proxy_connect
    9. proxy_html

Restart apache systemctl restart apache2

2. Enable custom port in /etc/apache2/ports.conf

Listen 80
Listen 8084 <This is added in the file>

3. Now create a file for virtualhost in /etc/apache2/sites-available/proxy.conf and add these lines. You may change the ports and local ip according to your setup.

<VirtualHost *:8084>
       ProxyPreserveHost On
       ProxyPass / http://192.168.1.3:8700/
       ProxyPassReverse / http://192.168.1.3:8700/

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Restart apache and test with public IP:8084. the traffic should be going to system B which is at 192.168.3:8700.

I took help from these sources

https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

https://www.mirantis.com/blog/quick-tip-use-apache-as-a-proxy-server-to-access-internal-ips-from-an-external-machine/