Thatbytes

Mostly Interesting

Tomcat and Mod Proxy

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:

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

So i added the correct items into my apache config:

1
2
3
4
5
6
7
8
9
10
11
12
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:

1
http://localhost/page1

when really the link should have been

1
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.

1
2
3
4
5
6
7
appname.war  
tomcat1#appname.war  
tomcat2#appname.war

/appname/  
/tomcat1/appname/  
/tomcat2/appname/ 
1
2
3
4
5
6
7
8
9
10
11
12
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.

Comments