How to Setup Apache as Reverse Proxy for Tomcat

Channel: Linux
Abstract: //domain.com/demo2/ >> http//domain.com/demo2/ >> http

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

Ref From: tecadmin

Related articles