A reverse proxy server is a web server that accepts client requests and routes them to the appropriate backend server. In that case, the reverse proxy is the internet-facing server, and backend applications are running on localhost or the LAN network.
Apache is the most popular web server that also can be configured as a reverse proxy server. In this tutorial, you will learn about configuring the Apache reverse proxy server on a Linux system.
We have two applications that are running with the Tomcat server at localhost port 8080. No matter whether applications are running with Tomcat or any other service like Nodejs etc. Both local applications’ URLs are:
Now I have installed the Apache server on the same host running on port 80. The Apache server accepts the requests from internet users and forwards them to corresponding applications running on the back end. We need to configure the proxy to forward requests as follows:
To get a better understanding see the below diagram:
So, let’s the configuration start:
Step 1: Setup Apache Proxy Module
By default, this module is enabled in Apache for users who installed using rpm packages. The Debian-based users need to enable modules manually.
- Redhat-based systems: Edit the proxy configuration file /etc/httpd/conf.modules.d/00-proxy.conf uncomment the following entries. If not available, then add them.
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so
- Debian-based systems: Use the following command to enable the Proxy module with Apache.
sudo a2enmod proxy proxy_http
After enabling the modules, you will need to restart Apache services to apply changes immediately.
Step 2: Configure Apache Virtual Host
Now will start working with the virtual host. We are creating three virtual hosts as below. You create only what is required with needed modifications. Edit Apache’s main configuration file and start with the configuration.
- First Virtual Host Example:
To forward all requests sent to example.com to backend tomcat server corresponding application like:
Configure the first virtual host as below:12345678<VirtualHost *:80>ServerName example.comProxyRequests OnProxyPass / http://localhost:8080/demo1/ProxyPassReverse / http://localhost:8080/demo1/</VirtualHost>
- Second Virtual Host Example:
To forward all requests sent to example.net to backend tomcat server corresponding application like:
Configure a virtual host like this.12345678<VirtualHost *:80>ServerName example.netProxyRequests OnProxyPass / http://localhost:8080/demo2/ProxyPassReverse / http://localhost:8080/demo2/</VirtualHost>
- Third Virtual Host Example:
To forward all requests sent to subdirectory /demo1/ or /demo2 on http://domain.com to back-end tomcat corresponding applications like:
Configure a virtual host like this.1234567891011121314151617<VirtualHost *:80>ServerName domain.comProxyRequests OnProxyPass /demo1 http://localhost:8080/demo1/ProxyPassReverse /demo1 http://localhost:8080/demo1/ProxyPass /demo2 http://localhost:8080/demo2/ProxyPassReverse /demo2 http://localhost:8080/demo2/<Location "/demo1">Required granted all</Location><Location "/demo2">Required granted all</Location></VirtualHost>
Step 3: Restart Apache to Apply Changes
Once you have successfully created Apache virtual host, you need to restart the Apache service. Use the following commands to restart the Apache service based on the operating system.
- Redhat-based systems:
sudo systemctl restart httpd
- Debed-based systems:
sudo systemctl restart apache2
This blog post helps you to configure Apache as a reverse proxy server on Debian-based or Redhat-based systems. The reverse proxy makes a bridge between internet facing web server and the web application running as a backend service.