Monday, April 20, 2015

How to Setup Apache as Reverse Proxy for Tomcat Server


Setup Apache as Reverse Proxy for Tomcat Server


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/
Apache as Reverse Proxy
Let’s start 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

2. Configure Apache Virtual Hosts

Now will start working with virtual host. We are creating three virtual hosts as below. You create only which is required with needed modifications. Edit Apache main configuration file and start with the configuration.

Setup VirtualHost-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>

Setup VirtualHost-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>

Setup VirtualHost-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 Apache service using following command and access your sites in web browser. Make sure you are getting proper pages from tomcat.
# service httpd restart

Source : http://tecadmin.net

No comments:

Post a Comment