Reading:
Apache Reverse Proxy Setup

Apache Reverse Proxy Setup

Metamug
Apache Reverse Proxy Setup

Reverse proxy

A reverse proxy is an application or service that sits between a client and a server. It acts as a gateway to another server. A forward proxy is a web application that sits on top of a web server and intercepts requests from clients before they reach the real server. Check this article to see its difference with forward proxy.

In the previous tutorial we learnt to create virtual hosts on apache server. This article explains how we can use Apache Server for forwarding to internal server. So apache here acts as a reverse proxy.

Forwarding request with virtual host

If you are looking to forward the incoming request to example.metamug.net to an internal application, add proxy information in same conf file.

ServerName example.metamug.net 
DocumentRoot /var/www/example.metamug.net 

ProxyPreserveHost On

ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

DocumentRoot here is useless. All the requests will be forwarded to another server.

Enable Proxy Modules

Enable the following modules before restarting apache server

sudo a2enmod proxy
sudo a2enmod rewrite
sudo a2enmod proxy_http

Close Internal Port

Also, care to close the any internal port from public access so that the users cannot directly open the server. In this example we are forwarding the request to port 8080 of the same machine.

So in your server configuration do close incoming traffic for port 8080 to avoid direct access.

Test Before Apache Restart

The below command helps you to test if everything is configured correctly with apache. Restarting the server without this step will cause your running site to stop. You may have other hosts running on apache. They will be affected due to this mis-configuration.

apachectl configtest

If the above command returns Syntax OK, restart the apache server.

sudo service apache2 restart

Proxy 502 Error

If the backend service example.metamug.net is down the proxy server will throw 502 proxy error. This issue can be fixed by restarting the backend service. 502 Proxy error Apache Server

Adding SSL on the reverse proxy

If you are further looking to add SSL on apache server, LetEncrypt offers free SSL. This helps you avoid adding ssl on the internal server. Checkout our SSL guide over Tomcat using apache reverse proxy



Icon For Arrow-up
Comments

Post a comment