Posts tagged ‘Glassfish’

Load balancing with Apache 2.2 mod_proxy_ajp

The Apache 2.2 webserver has a module for proxiing AJP requests (mod_proxy_ajp). This module is delivered with the Apache webserver by default.

Activating modules

The following modules must be enabled to use the AJP proxy functionallity:

  • mod_proxy
  • mod_proxy_ajp
  • mod_proxy_balancer

To activate the modules uncomment the following lines in your httpd.conf configuration file (e.g. /opt/apache/conf/httpd.conf):

LoadModule proxy_module modules/
LoadModule proxy_ajp_module modules/
LoadModule proxy_balancer_module modules/

Configuring modules

To configure the modules we create a new configuration file conf/ajp_proxy.conf in the apache directory and add the following line to our httpd.conf file:

Include conf/ajp_proxy.conf

First of all we put all configuration directives in <IfModule/> blocks to ensure that all needed modules are loaded:

<IfModule mod_proxy>
  <IfModule mod_proxy_ajp>
    <IfModule mod_proxy_balancer>
      # configuration of AJP proxy

Creating Load Balancer Cluster

The load balancer cluster is created with the ProxyPass directive. The syntax for this directive is

ProxyPass <path> balancer://<name-of-cluster> <options>

The <path> argument stays for the logical path on the apache server, <name-of-cluster> for the name of your cluster and <options> for the options for this load balacer cluster (see documentation for description of options).


ProxyPass /myapp balancer://mycluster/myapp stickysession=JSESSIONID nofailover=On

In the next step we must define the workers for our cluster and your application server must support the JServ AJP protocol, e.g. Tomcat. For glassfish aka Sun Java System Application Server see my mod_jk tutorial for implementing the JServ protocol into the server.

The workers are definded into a <Proxy/> directive. The syntax for this directive is

<Proxy balancer://<name-of-cluster>
  BalancerMember ajp:<hostname>:<port> <options>

You can define multiple workers in one proxy directive.

Example for two worker nodes:

<Proxy balancer://mycluster>
  BalancerMember ajp:// route=node1
  BalancerMember ajp:// route=node2