A reverse proxy server is a web server that accepts the client requests and routes them to the appropriate backend server. In that case, your reverse proxy server is directly connected to the internet and the backend services can run on a private network.
Apache and Nginx both are generally used for the reverse proxy server. This tutorial will help you to set up an Apache web server as Reverse Proxy for the Tomcat-hosted applications.
Setup Scenario
Tomcat is running on port 8080 and I have configured two sample applications running with following urls.
- http://localhost:8080/sample
- http://localhost:8080/calendar
Now I have installed Apache server on same host running on port 80. I will use Apache server to get users requests and transfer these requests to corresponding applications running on back-end Tomcat server on port 8080. I need to configure Apache to transfer requests to tomcat like below:
- http://example.com
>> http://localhost:8080/demo1/ - http://example.net
>> http://localhost:8080/demo2/ - http://domain.com/demo1/
>> http://localhost:8080/demo1/ - http://domain.com/demo2/
>> http://localhost:8080/demo2/
Let’s start the configuration
1. Enable Mod Proxy Apache Module
By default this module is enabled in Apache for users who installed using rpm packages. If you don’t have enabled edit your Apache configuration /etc/httpd/conf/httpd.conf or for Apache 2.4 /etc/httpd/conf.modules.d/00-proxy.conf file and uncomment following lines or put in file.
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so
For the Debian-based systems use the following command to enable the Proxy module with Apache.
sudo a2enmod proxy
2. Configure Apache Virtual Hosts
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.
VirtualHost Example 1 :-
To forward all requests sent to example.com to backend tomcat server corresponding application like:
- http://example.com
>> http://localhost:8080/demo1/
Configure virtual host like this.
<VirtualHost *:80> ServerName example.com ProxyRequests On ProxyPass / http://localhost:8080/demo1/ ProxyPassReverse / http://localhost:8080/demo1/ <Location "/sample"> Order allow,deny Allow from all </Location> </VirtualHost>
VirtualHost Example 2 :-
To forward all requests sent to example.net to backend tomcat server corresponding application like:
- http://example.net
>> http://localhost:8080/demo2/
Configure virtual host like this.
<VirtualHost *:80> ServerName example.net ProxyRequests On ProxyPass / http://localhost:8080/demo2/ ProxyPassReverse / http://localhost:8080/demo2/ <Location "/"> Order allow,deny Allow from all </Location> </VirtualHost>
VirtualHost Example 3 :-
To forward all requests sent to sub directory /demo1/ or /demo2 on http://domain.com to back-end tomcat corresponding applications like:
- http://domain.com/demo1/
>> http://localhost:8080/demo1/ - http://domain.com/demo2/
>> http://localhost:8080/demo2/
Configure virtual host like this.
<VirtualHost *:80> ServerName domain.com ProxyRequests On ProxyPass /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"> Order allow,deny Allow from all </Location> <Location "/demo2"> Order allow,deny Allow from all </Location> </VirtualHost>
3. Restart Apache and Test
After making all necessary changes restart the Apache service using the following command and access your sites in a web browser. Make sure you are getting proper pages from tomcat.
service httpd restart
4 Comments
Do NOT set “ProxyRequests On”
As already said by Wesse Van Norel, this means forward proxying is enabled. As there is no specific restriction on that, all web bots will have fun through your web server, attacking third parties. The next thing you know, your IP address will be on an abuse list.
https://httpd.apache.org/docs/current/mod/mod_proxy.html#proxyrequests
Hi Rahul,
I have separate web and tomcat server. Revers proxy is configured on Web server i.e Apache to tomcat. Now Tomcat has one application which has to hit some URL . Now the request has to from Web server .
Please suggest where I have to configure reverse proxy which will pass request to web server.
I just found this tutorial and I’m amazed that you set ProxyRequests to On.
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxyrequests
“This allows or prevents Apache httpd from functioning as a forward proxy server. (Setting ProxyRequests to Off does not disable use of the ProxyPass directive.)”
With this setting you allow anyone to use your server as an open proxy server, to be (ab)used by anyone on the internet. Are you sure you intent this?
Hi Rahul,
Your blog is useful…..but still now facing problem …I am working azure cloud vm and am installed tomcat and Apache 2.4 ….As per above setup all ..But browser when hitting site always open Apache Tomcat index page.When am hitting localhost open my war file cannot open outside access…Can u help regarding problem..Thanx.