February 15, 2006

Goodbye mod_jk hello mod_proxy

Posted in Software at 14:18 by graham

I’ve been using Apache as a front end to a cluster of Tomcat servers for a while. I’ve always used mod_jk2 or mod_jk; I even wrote an article about it. Not any more.

The most recent Apache comes with mod_proxy, mod_proxy_balancer and mod_proxy_ajp, which do everything I used mod_jk to do.

Setup mod_proxy_balancer

First you need to make sure they are included when you compile Apache:

./configure –prefix=/usr/local/apache2.2 –enable-proxy –enable-proxy-ajp –enable-proxy-balancer –enable-proxy-http

Then in your httpd.conf you need: BalancerMember ajp://bill.darkcoding.net:8101 route=server1 BalancerMember ajp://ben.darkcoding.net:8102 route=server2

    <location /myApp>
        ProxyPass balancer://myCluster/myApp stickysession=JSESSIONID
    </location>

    <location /balancer-manager>
            SetHandler balancer-manager

        AuthType Basic
        AuthName "Cluster manager "
        AuthUserFile /usr/local/apache2.2/conf/.htpasswd
        Require valid-user

    </location>

The above defines a cluster and adds your two servers to it. Then it proxies your app location using that cluster. Finally it defines a URL for the balancer manager. This is the replacement for jkmanager. The stickysession value needs to be the name of the session url param name or cookie. The above is correct for Tomcat. Note that the session id is case sensitive.

Finally in you Tomcat server.xml you need to include a jvmRoute to match the route defined above. This is exactly the same as when using mod_jk, so you probably already have it. It should look somewhat like this:

   <engine name="Catalina" defaultHost="localhost" debug="0" jvmRoute="server1">

and similarly for server2.

Eh voila !

If it’s not working change your LogLevel in httpd.conf to debug and restart Apache. If you get this error:

Found value (null) for stickysession JSESSIONID

then Apache is not finding your route on the session id. Check the spelling and capitalisation of the stickysession entry. Using the Firefox developer toolbar to check you Cookie. It should look something like JSESSIONID=4f39cBLAH.server1. The route is appended to the session id after a period.

There is also apparently a problem with disabled servers coming back to life. See this thread

Happy proxying !

10 Comments »

  1. xyz said,

    August 31, 2010 at 06:14

    hi,

      Can you provide detailed steps for setting up a load balancer for a jboss cluster using mod_proxy_ajp module of apache HTTP Server
    
  2. c’est ca said,

    April 28, 2010 at 12:26

    et voila Not Eh voila

  3. Jim Typrowicz said,

    June 13, 2008 at 20:34

    Great write up, works as advertised, I like that :-) Thank you!

  4. hououji said,

    May 28, 2008 at 09:27

    Thx for your great job. It work !

    There are something to supp. for my case,

    My webapps contain some browser redirect (HTTP return code302), and I find that the 302 sometime fail to “stick” to the same machine. To soft this, append “nofailover=On” to the ProxyPass command, and add also the ProxyPassRequest, it work find for the 302 redirect.

    here is my example,

    ProxyPass /gateway2/ balancer://ajpCluster/gateway2/ stickysession=JSESSIONID nofailover=On ProxyPassReverse /gateway2/ balancer://ajpCluster/gateway2/

  5. Paul Malinowski said,

    July 31, 2007 at 17:29

    Hi,

    How can I implement mod_proxy_ajp with regular expresion, without using rewite rules?

  6. Dave said,

    June 25, 2007 at 15:56

    This sounds like a great idea..forgive my ignorance, but how do you get rid of the “myApp” path?

    My configuration is two Tomcat servers serving the web site on port 80. / is the application root.

    I tried this:

    BalancerMember ajp://10.10.81.154:80 route=LB1 BalancerMember ajp://10.10.81.155:80 route=LB2

    ProxyPass balancer://myCluster/myApp stickysession=JSESSIONID
    
    
    
        SetHandler balancer-manager
    

    but when I deleted “myApp” it did not work

  7. Robert Frapples said,

    October 28, 2006 at 20:06

    Does Apache’s mod_proxy have anything analagous to JK’s jkstatus page?

  8. Robert Frapples said,

    October 28, 2006 at 20:03

    Marcos,

    I had the same problem. My solution was to deploy on my production servers as ‘production’, thus ajp://prodserver01:8009/production/index.jsp and ajp://prodserver02:8009/production/index.jsp and the test servers as ‘test’, thus ajp://testserver01/test/index.jsp and ajp://testserver01/test/index.jsp.

    The only other solutions I can think of are: 1) Get Apache to rewrite the Urls in the pages that your application returns. This is probably not feasible. 2) Be very careful about creating URLs in your application. Everything should be relative to ‘app’, which means you cannot start any URLs with ‘/’.

    Robert Frapples

  9. Yan Yu said,

    August 24, 2006 at 18:12

    Hi Marcos,

    Did you have answer for your question? I am having the same issue here and very much interested in knowing the solution.

    Thanks Yan

  10. Marcos Cisneros said,

    June 9, 2006 at 21:10

    Hi

    I saw your post about using the new mod_proxy_ajp instead of mod_jk and it works great. Now I am trying to use apache for balance two java web applications (each one in diferent clusters), the two are the same but in different phases, one for developing and one for testing. the first is in devserver:5000/app/home.jsp and devserver2:5001/app/home.jsp and the otrher is in testserver:5000/app/home.jsp and testserver:5001/app/home.jsp

    and I want the apache server be the entry point for both, something like: http://balanceServer/dev/app/home.jsp and http://balanceServer/test/app/home.jsp and depending on the “dev” ot “test” prefix redirects to each cluster.

    My problem is how to rewrite the url links comming from the cluster to insert them the “dev” or “test”? because right now it is sending me to the correct cluster but when trying a submit, the browser resolves to balanceServer/app/submitionpage.jsp, do you have idea?

    Thank you, this is the first time I use Apache.

    Marcos Cisneros

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.