How to configure an example apache reverse proxy server
Apache as a reverse proxy
Web clients should never access an application server directly.
Instead, all web-based requests must first be handled by a web server configured as a load balancer or reverse proxy.
The most popular open source software used to configure a server as a reverse proxy is Apache HTTP Server. Unfortunately, Apache is not pre-configured to forward requests from the application server to back-end origin servers waiting to process them.
To behave as a load balancer or reverse proxy, Apache must be configured to do so. Here’s how.
Apache Reverse Proxy Setup Steps
To configure Apache as a reverse proxy, follow these steps:
- Install Apache web server
- Install and configure the backend origin servers
- Activate it mod_proxy and mod_http mods in apache httpd.conf case
- Configure Apache ProxyPass and ProxyPassReverse settings
- Restart the Apache web server
Install Apache and Origin Servers
For this example, we will assume that you have already installed the Apache web server and the origin server.
The term origin server is the technical name of any application server that handles requests passed to it by the Apache reverse proxy server. In the Java ecosystem, it would probably be a Tomcat, Jetty, JBoss, Wildfly, or WebSphere application server that manages the applications. But it can also be any non-Java resource such as an Nginx or Express server that hosts Node.
Deploying the Apache Tomcat application
For this example, we will configure Apache Reverse Proxy to forward requests to Tomcat Sample war.
The sample application is provided by Apache Tomcat to demonstrate how to deploy applications to the server. Tomcat 10 version can be downloaded here.
When the sample application is deployed, the URL to access the application on the Tomcat origin server is:
With the Apache reverse proxy configured, users will be able to invoke this URL on port 80 of the Apache web server, but their request will be forwarded through the Apache reverse proxy to Tomcat.
The URL that clients would use to access the sample application after setting up the Apache reverse proxy will be:
Apache mod_proxy library
Apache’s reverse proxy functionality is provided through a shared library named mod_proxy.so
This module is not enabled by default, so you need to modify the Apache one httpd.conf file to activate it. You will also need to enable Apache mod_proxy_http.so shared library as well.
Enable Apache mod_proxy
Uncomment the appropriate line in Apache httpd.conf file to activate apache mod_proxy and mod_proxy_http mods:
### 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 proxy password and an apache proxypassreverse entry at the end of httpd.conf case. 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, we have mapped /sample path that users will provide to the Apache web server, to the path of the application as hosted on the Tomcat origin server. This completes the configuration of Apache as a reverse proxy for web requests.
Apache ProxyPassReverse example
With apache mod_proxy enabled library and apache ProxyPassReverse configured parameter, just restart the httpd process that supports the web server and invoke the sample application on port 80 of the web server.
The Apache reverse proxy will handle the incoming request, recognize that an Apache ProxyPassReverse setting exists, and then forward the request to Tomcat for processing.
Tomcat will process the request, send a response back to the Apache reverse proxy, then Apache will send the response back to the client. If the sample appears in the client browser, the Apache ProxyPassReverse sample can be considered a success.
Using a reverse proxy is a common configuration in modern enterprise architectures. The simplicity and ease with which we can configure Apache proxy servers is a testament to the benefits of open source software and the excellent software provided by the Apache Software Foundation.