platinor
platinor

Reputation: 857

Load balancing with Apache and Tomcat combined with URL redirect

I now have one Apache server and two Tomcat servers. They are connected using mod_jk module. And the load balancing is configured. All request will be redirected to the loadbalancer, in httpd.conf:

JKMount /* controller

The controller is the loadbalancer, and the working tomcat servers are worker1, worker2.

The problem is that, in addition to the automatic load dispatch, I also need a url matching redirection. Speicifically, the request for http://www.example.com/test1/index.html should go to worker1 (Tomcat), and http://www.example.com/test2/index.html go to worker2. However, in both worker1 and worker2, the application structure is webapps/test/ structure.

I can use the mod_jk url mapping to dispatch /test1/ to worker1 and /test2/ to worker2, but the PATH will be /test1/ and /test2/ not /test/. Meanwhile, if I use the apache redirectMatch or url rewrite to change the /test1/(/test2/) to /test/, the mod_jk will not dispatch the url to the different worker now, since they have the same PATH.

How can I deal with this situation?

Upvotes: 3

Views: 3859

Answers (2)

Dave Oxley
Dave Oxley

Reputation: 31

You need to make the application a root application in Tomcat. You can do this by adding a META-INF/context.xml to your app with the following:

<Context path="/"/>

I'd suggest you remove other apps from the webapps directory. Then you need to alter your apps web.xml so the servlet(s) are now mapped to the appropriate url with the appropriate contexts:

<servlet-mapping>
    <servlet-name>TestApp</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>TestApp</servlet-name>
    <url-pattern>/test1</url-pattern>
</servlet-mapping>

The app in the second JVM would need the url-pattern /test2 instead. For Apache/Tomcat connection I use mod_ajp rather than mod_jk. Here is what you'd need in Apache for mod_ajp:

<Proxy balancer://cluster>
    BalancerMember ajp://127.0.0.1:8015 route=ajp13_node1
    BalancerMember ajp://127.0.0.1:8016 route=ajp13_node2
</Proxy>
<Location "/test">
    ProxyPass balancer://cluster/test stickysession=JSESSIONID
</Location>
<Location "/test1">
    ProxyPass ajp://127.0.0.1:8015/test1
</Location>
<Location "/test2">
    ProxyPass ajp://127.0.0.1:8016/test2
</Location>

This is assuming the AJP connector is listening on 8015 for the first JVM and 8016 for the second.

Upvotes: 3

Miguel Silva
Miguel Silva

Reputation: 633

Perhaps a simple way to do it is to use urlrewrite filter on tomcat workers. According to documantation you should have the following rule on your urlrewrite.xml file:

    <rule>
       <from>^/test[0-9]*/(.*)$</from>
       <to type="redirect">/$1</to>
    </rule>

So workers would ignore the test1 or test2 URI part. And apache could work just the way you planned with mod_jk.

Upvotes: 2

Related Questions