Tuesday, September 23, 2008

Reader Response: proxy_ajp is better than mod_jk

Wow, so not only do I have readers, but I have readers willing to contribute their time to improving the information available on this blog! Special thanks to Emerson for this post.



This is a complementary post to Ubuntu Dev Server - Hudson[1] with a simpler way (IMO) to integrate Tomcat with Apache.

This can be achieved by enabling mod_proxy and mod_proxy_ajp in Apache. If you also use ubuntu-server, like I do, just link them symbolically:

$ sudo ln -s /etc/apache2/mods-available/mod_proxy /etc/apache2/mods-enabled/mod_proxy
$ sudo ln -s /etc/apache2/mods-available/mod_proxy_ajp /etc/apache2/mods-enabled/mod_proxy_ajp


Now edit apache2.conf like this:
<Location /your_tomcat_app>
ProxyPass ajp://your_host:8009/your_tomcat_app
Order allow,deny
allow from all
</Location>

Then restart Apache and it's done. To add another application, just repeat the same setup above mutatis mutandis[2].

One additional setup for my application was to enable access to URLs, like <http://my-server/my-tomcat-app/servlet/my-servlet-name>. To do this, edit /etc/tomcat5.5/web.xml and find the string "invoker". You will find the following commented servlet element:
<!--
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
-->


Just strip the comment tags and restart Tomcat. Integrating Tomcat with Apache should be that easy since the beginning.

[1] http://suereth.blogspot.com/2008/08/ubuntu-dev-server-hudson.html
[2] http://en.wikipedia.org/wiki/Mutatis_mutandis

3 comments:

Rostislav said...

With mod_jk it is possible to map only specific folders and extensions. This option allows you to distinguish the static resources in the application and serve them directly with Apache rather than Tomcat.

Is something like this possible with this proxy approach?

Emerson said...

I'm not an Apache expert but I think you can do what you want through <Location> and/or <Directory> directive.
To map folders I think you can map them adequately with these directives. To map extensions maybe you'll need to use regular expressions.

Take a look at: http://httpd.apache.org/docs/2.0/mod/core.html#location

J. Suereth said...

I don't think ajp has the same ability as mod_jk to "JkAutoAlias" (see here for documentation on mod_jk. In my experience we didn't use JkAutoAlias because the way we did security required a valid login through tomcat before you could access certain static documentation files. JkAutoAlias would have broken this, therefore I don't have much experience with it.

However, if you have your static content in a separate directory, I'd recommend using Emerson's approach.

A quick google search reveals mod_ajp/mod_proxy do support load balancing similar to mod_jk. Here's the link on how to set it up.