Access a local Tomcat through Apache

Heard a nice ‘trick’ some time ago I need to write about (so I do not forget it ;-).

I wanted to access a tomcat web application on a server that was mainly running a Drupal CMS.

As I do not like shooting holes in firewalls I dislike the use of port 8080 too. The solution I heard was both simple and elegant.

Basically one installs and configures a Tomcat server and deploy a web application on this Tomcat server listening top a port that can only be reached on the server itself (8081 in the example below).

In the Apache ‘httpd.conf’ file you enable mod_proxy by uncommenting the line:

   1: LoadModule proxy_module modules/mod_proxy.so

Then you define a mapping of the tomcat URL to a URL inside the apache servers web space.

   1:  

   2: # http://<ip>:8081//?">http://<ip>:8081/<Webapp>/<Webappurl>?<parameters>

   3: # is mapped mapped to: 

   4: # http://<ip>/<Webappurl>?<parameters> 

   5:  

   6: ProxyPass /OAIHandler http://127.0.0.1:8081/LiLiTarget/OAIHandler

   7: ProxyPassReverse /OAIHandler http://127.0.0.1:8081/LiLiTarget/OAIHandler

In above example:

  • 8081 is the local port to the Tomcat server, not reachable from the outside.
  • 127.0.0.1 is the local address of the server, not reachable from the outside.
  • OAIHandler is a URL inside the web application that does the work (and I re-used it as URL inside the Drupal webspace). URL parameters are nicely appended.
  • LiLiTarget is the name of the web application inside Tomcat’s ‘webapps’ directory.
  • The name of the web application is not used as part of the URL.

Nice side effect of this approach is that you easily swap Tomcat servers or have multiple Tomcat servers running, each with their own port number and each running only a single webapp deployed. This way you can nicely restart/maintain a single Tomcat without affecting the reset of the instances or have multiple Tomcat versions running on a single server.

Advertisements
This entry was posted in Tomcat. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s