How to setup an Apache reverse proxy server example

Apache reverse proxy configuration

Web clients should never hit an application server directly.

Instead, all web-based requests should first go through a web server configured as a load balancer or a reverse proxy.

Apache Reverse Proxy and Tomcat

An Apache reverse proxy plays a key role in the traditional three tier enterprise architecture.

The most popular open source software used to configure a server as a reverse proxy is the Apache HTTP Server. Unfortunately, Apache is not pre-configured to pass application server requests to the backend origin servers waiting to handle them.

That means you must configure Apache to behave as a load balancer or reverse proxy. Here’s how to do it.

Reverse proxy setup steps

To configure Apache as a reverse proxy, follow these steps:

  1. Install the Apache Web Server
  2. Install and configure the backend origin servers
  3. Enable the mod_proxy and mod_http modules in Apache’s httpd.conf file
  4. Configure Apache ProxyPass and ProxyPassReverse settings
  5. Restart the Apache Web Server

Install Apache and the origin servers

For this example, we assume you already have installed the Apache Web Server and the origin server.

The term origin server is the technical name for any application server that handles requests passed to it by the Apache reverse proxy server. In the Java ecosystem, this is likely handled by Tomcat, Jetty, JBoss, Wildfly or WebSphere Application Server. Alternatively, any non-Java resource such as an Nginx or Express server can host Node-based applications.

Apache Tomcat app deployment

For this example, we will configure the Apache Reverse Proxy to forward requests to the Tomcat Sample war file.

Apache Tomcat provides the Sample app to demonstrate how to deploy applications to the server. The Tomcat 10 version can be downloaded here.

When the Sample app is deployed, the URL to access the application on the Tomcat origin server is:

http://localhost:8080/sample

With the Apache reverse proxy configured, users can invoke this URL on port 80 of the Apache web server, but the Apache reverse proxy forwards the request to Tomcat.

The URL for clients to access the Sample app after the Apache reverse proxy is configured is:

http://localhost/sample

Apache mod_proxy library

The ability to use Apache as a reverse proxy is provided through a shared library named mod_proxy.so. This module is not enabled by default, so you must edit Apache’s httpd.conf file to enable it. You also need to enable Apache’s mod_proxy_http.so shared library.

Enable Apache mod_proxy

Uncomment the appropriate line in Apache’s httpd.conf file to enable the Apache mod_proxy and mod_proxy_http modules:

### Enable LoadModule entry for Apache mod_proxy and mod_proxy_http ###
#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

Apache proxypass configuration

With the mod_proxy and mod_proxy_http modules enabled, add an Apache proxypass and an Apache proxypassreverse entry at the end of the httpd.conf file. These proxy settings map the application path that the Apache Web Server sees, to the path used to host the application on the origin server:

ProxyPass /sample http://localhost:8080/sample
ProxyPassReverse /sample http://localhost:8080/sample

As you can see, this maps the /sample path that users provide to the Apache Web Server, to the path of the application as it is hosted on the Tomcat origin server. This completes the configuration of Apache as a reverse proxy for web-based requests.

Apache ProxyPassReverse example

With the Apache mod_proxy library enabled, and Apache’s ProxyPassReverse setting configured, simply restart the httpd process that supports the web server and invoke the Sample app on port 80 of the web server.

The Apache reverse proxy handles the incoming request, recognizes that an Apache ProxyPassReverse setting exists, and then forwards the request to Tomcat. Then Tomcat handles the request, returns a response to the Apache reverse proxy, and Apache returns the response to the client. If the sample comes up in the client browser, the Apache ProxyPassReverse example is considered a success.

The use of a reverse proxy is a common configuration in modern enterprise architectures. The simplicity and ease to set up Apache proxy servers is a testament to the benefits of open source software and the great software provided by the Apache Software Foundation.