Apache to Tomcat Connection Via AJP

When you front a Tomcat/TomEE with Apache web server, you have a choice of using http or ajp protocol. In most cases ajp is recommended. It has a few additional features. For me, more importantly, ajp performs better.

First, I tested my setup with http communication. My Apache configuration file was:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /etc/apache2/vhosts.d/example
ProxyPassReverse / http://localhost:8080/
ProxyPass / http://localhost:8080/
HostnameLookups Off
UseCanonicalName Off

<Directory "/etc/apache2/vhosts.d/example">
AllowOverride None
Options +ExecCGI -Includes
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

I then stress tested two pages using Jmeter. Average response times were:

  • Static JSP page: 83ms
  • Database driven JSP page: 213ms

Next, I migrated the setup to use ajp. First, I had to add this to my /etc/apache2/httpd.conf file to load the ajp module.

LoadModule proxy_ajp_module /usr/lib64/apache2/mod_proxy_ajp.so

Then, I changed the proxy directives in my virtual host setup:

ProxyPassReverse / ajp://localhost:8009/
ProxyPass / ajp://localhost:8009/

I repeated the stress test, and got this result.

  • Static JSP page: 55ms
  • Database driven JSP page: 151ms

This is a distinct improvement in performance.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.