2010/02/10

Apache-Tomcat Communication via mod_jk

Ever wondered why to use mod_jk and how to set it up? The reasons why are simple - Tomcat guys recommend it as more mature.

It certainly has better logging then mod_ajp_proxy and that helps alot during troubleshooting or performance tuning. The ability to set max packet size above 8kB is also a good point for some deployments. As for performance, some tests showed slight advantage of mod_jk to mod_ajp_proxy, but the main seemed to be the usage of Tomcat's native module regardless of AJP module used.

LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so

# points to a file that provides a mapping between a worker name and a valid worker type
JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log

# allows log levels: debug, info, error
JkLogLevel   info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkRequestLogFormat      "%w %V %T"

# JkOptions - see http://tomcat.apache.org/connectors-doc/reference/apache.html

<Location /jkstatus/>
 JkMount status
 # Order deny,allow
 # Deny from all
 # Allow from 127.0.0.1
 Allow from all
</Location>

<VirtualHost *:80>
  # which URI contexts are sent to a ASF Tomcat worker - exact, context or suffix match
  JkMount /* tomcat1
</VirtualHost>

Note: JkMount must be defined in VirtualHost to work.

worker.properites:

worker.list=tomcat1
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009

## when using mpm prefork, the pool_size should be set to "1"
worker.tomcat1.connection_pool_size=1
worker.tomcat1.connection_pool_timeout=600
worker.tomcat1.socket_keepalive=1

max_packet_size=65536


JkWorkerProperty directive allows to move content of the worker.properties file to apache config.