With GlassFish 3.1.2 release around the corner, I've decided to update the outdated blog to cover how to front GlassFish 3.1.2 with Apache HTTP Server.  The instructions have changed since v3 and it might lead to some confusion to the users who might be looking at the old blog post. 

GlassFish, Apache HTTP Server, and mod_jk

GlassFish 3.1.2 (as well as 3.1.1) supports AJP protocol for each network listener using the jk-enabled attribute as described below.  The instructions are the same for 3.1.1 and 3.1.2.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp worker1
# Send all webapp requests to GlassFish
JkMount /webapp/* worker1

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener either using asadmin command or the admin console.

asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector

6.  The jk-configuration-file attribute of the network listener can be used to configure non-default attributes for the Connector.  Use asadmin set to configure the jk-configuration-file attribute of the  network listener to the fully-qualified filename of the jk-configuration-file.  This step is optional.

asadmin> set server-config.network-config.network-listeners.network-listener.jk-connector.jk-configuration-file=domain-dir/config/glassfish-jk.properties   

7.  Increase the maximum threads in the http-thread-pool pool if necessary.

asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value

The same GlassFish configuration can be used for mod_proxy_ajp also.  GlassFish 3.1.2 will be using Grizzly for ajp implementation.

Load balance using GlassFish, Apache HTTP Server, and mod_jk

GlassFish, Apache HTTP Server, and mod_jk can be used for load balancing and the following describes how to load balance using two instances.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

worker.list=loadbalancer
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=1
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010
worker.worker2.lbfactor=1
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp loadbalancer
# Send all webapp requests to GlassFish
JkMount /webapp/* loadbalancer

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener targeted to the cluster either using asadmin create-network-listener command or the admin console. 

asadmin> create-network-listener --protocol http-listener-1 --target cluster1 --listenerport ${AJP_PORT} --jkenabled true jk-connector

6.  Create the jvmRoute property targeted to the cluster using the create-jvm-options command.

asadmin> create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"  

7.  Define the AJP_PORT and AJP_INSTANCE_NAME properties for each of the instances of the cluster using the create-system-properties command.  The port values need to match the values specified in the  workers.properties.

asadmin> create-system-properties --target instance1 AJP_PORT=8009
asadmin> create-system-properties --target instance1 AJP_INSTANCE_NAME=instance1
asadmin> create-system-properties --target instance2 AJP_PORT=8010
asadmin> create-system-properties --target instance2 \AJP_INSTANCE_NAME=instance2
asadmin> create-system-properties --target instance3 AJP_PORT=8011

That's it!  Send questions to users@glassfish.java.net.

FAQ -

1.  "No class name for worker.worker1 worker" in the server.log. 

Make sure you have the workers.properties file and configured correctly as JkWorkersFile in httpd.conf

2.  "threadpool.max_threads_too_low" in the server.log.

Try increasing maximum threads in the http-thread-pool pool via asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value or use jk-configuration-file for Connector.maxThreads.

              3.  "WARNING JK properties file ...               does not exist"

This is a warning message that you have jk-configuration-file defined     but the file does not exist.

3.  How should I write the jk-configuration-file?

See Properties.load for key/element examples.

4.  How do I create a jk-enabled connector with a new thread pool with a minimum size of 5 and mazimum size of 150 (Apache MaxClients) for example?

asadmin> create-threadpool --minthreadpoolsize 5 --maxthreadppplsize 150  jk-thread-pool
asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true --threadpool jk-thread-pool jk-connector

 

5.  Truncated downloads

See http://java.net/jira/browse/GLASSFISH-18446