I don’t know why, even if there is a lot of documentation on the subject the mod_jk installation still seems a bit of mystic elements. This is why I decided to write a very short mod_jk configuration guide oriented for debian, ubuntu linux systems.

First, mod_jk is an apache module which allows a more customized communication to an apache tomcat server using the AJP 1.3 protocol. This usually happens using the TCP port 8009 so yes, there must be a connection from the apache server to the tomcat one. The alternative for mod_jk is mod_proxy which can be used for simple cases.

Most systems should have a mod_jk package by now:

apt-get install libapache2-mod-jk
a2enmod jk

This should install and enable mod_jk so now it’s time to configure it. There are 3 parts. First the jk-workers.properties file which can be created in /etc/apache2

# workers.properties -

# The list of Tomcat workers
worker.list=ajp13

# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
worker.ajp13.lbfactor=1

#
# Specify the size of the open connection cache.
worker.ajp13.cachesize=10

This file contains the workers which can be referenced by name later. Note that this is the point where the tomcat server is configured.

Second this file and other configurations have to be tied to mod_jk. This means usually editing the /etc/apache2/modules-enables/jk.load file:

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
JkWorkersFile   /etc/apache2/jk-workers.properties
JkLogFile       /var/log/apache2/mod_jk.log
JkLogLevel      info
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories -ForwardLocalAddress
JkRequestLogFormat "%w %V %T"
JkMount /xContext/* ajp13

This file first specifies the jk-workers file and then configures the mod_jk logging mechanism. It also adds some options which I use frequently. Of course you can read the full documentation but this is a document for the quick install.

The actual connection to a tomcat context is done by the JkMount directive. You can either put it here to have a global association in your server or just add it to your virtual server configuration which is the 3’rd place where you might need to edit something. Just add a JkMount directive to make your tomcat context accessible from apache. One great thing about mod_jk is that you can do that even from a ssl context and thus adding ssl to your application without any tomcat configuration whatsoever.

Comments:

Chetan -

Can you please let me know how to use the variable JkRemoteAddrIndicator My intention is to instruct Apache to put the value in X_Forwarded_for header if available so that all my calls in tomcat layer where i do request.getRemoteAddr() give me the actual client ip and not the ip of the proxy that they are coming through.