Tomcat And Mod Proxy

Page content

I was asked today to setup a server that will run multiple instances of tomcat, running the same application. That would sit behind a apache server that would forward the requests on the tomcat server.

An example is below:

http://localhost/tomcat1/appname ->  tomcat1 :8180
http://localhost/tomcat2/appname -> tomcat2 :8280

So i added the correct items into my apache config:

ProxyPreserveHost On
ProxyRequests Off

<Location /tomcat1/appname/>
ProxyPass  http://127.0.0.1:8180
ProxyPassReverse  http://127.0.0.1:8180
</Location>

<Locatin /tomcat2/appname/>
ProxyPass http://127.0.0.1:8280
ProxyPassReverse http://127.0.0.1:8280
</Location>

As the application has no idea of its context it was generating links that where incorrect for example:

http://localhost/page1

when really the link should have been

http://localhost/tomcat/appname/page1.
In order to get around that i simple renamed the war file to tomcat1#appname.war and tomcat2#appname.war deployed them to tomcat1 and tomcat2 and updated the apache config.

The ‘#’ hash character is used to add a / to the application context path.

appname.war  
tomcat1#appname.war  
tomcat2#appname.war

/appname/  
/tomcat1/appname/  
/tomcat2/appname/ 

ProxyPreserveHost On
ProxyRequests Off

<Location /tomcat1/appname/>
ProxyPass  http://127.0.0.1:8180/tomcat1/appname
ProxyPassReverse  http://127.0.0.1:8180/tomcat1/appname
</Location>

<Location /tomcat2/appname/>
ProxyPass http://127.0.0.1:8280/tomcat2/appname
ProxyPassReverse http://127.0.0.1:8280/tomcat2/appname
</Location>
Now the links generated correct as the application knew the correct context path.