Sunday, May 01, 2011

Apache Configs for AJP Load Balancing

Ok this configuration is going to save you hours of noodling if you just follow it to the T. lets say you have a WAR you want to deploy wars, and you want it to be the ROOT level. Well then take that foobar.war and deploy it as ROOT.war on your tomcat instance. Don't argue about this unless you want to spend many many hours on rewrite rules.

Then just follow this example below for your sites available in /etc/apache2, make sure rewrite and proxy are enabled in mods enabled... You can have more tomcat instances in your balancer if you wish.

<VirtualHost *:80>
 ServerName webtier.mydomain.com
 ServerAlias webtier.mydomain.com

 <Proxy balancer://mydomainCluster>
  BalancerMember ajp://10.211.79.225:8009 route=node1 loadfactor=1 
  ProxySet lbmethod=byrequests
    ProxySet stickysession=JSESSIONID
 </Proxy>
     ProxyPass / balancer://mydomainCluster stickysession=JSESSIONID
     ProxyPassReverse / balancer://mydomainCluster
     ProxyPreserveHost On

 # DON'T TURN ProxyRequests ON!  Bad things will happen
 # http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#access
 # http://www.akadia.com/services/prevent_abuse_proxy.html
 ProxyRequests off

 # Necessary to have mod_proxy_html do the rewriting
 RequestHeader      unset  Accept-Encoding

 # Rewrite the URLs to proxy ("[P]") into the Tomcat server
 RewriteEngine     on
 RewriteRule ^(/.*)      balancer://mydomainCluster$1    [P]

 # Be prepared to rewrite the HTML/CSS files as they come back
 # from Tomcat
 SetOutputFilter proxy-html

 # Rewrite JavaScript and CSS files in addition to HTML files
 ProxyHTMLExtended on

 # Output Strict XHTML (add "Legacy" to the end of the line below
 # to output Transitional XHTML)
 ProxyHTMLDoctype XHTML

 # alert, emerg.
 LogLevel warn
 ErrorLog /var/log/apache2/error.log
 CustomLog /var/log/apache2/access.log combined
 ServerSignature On
 Header unset Cache-Control
 Header set Cache-Control "max-age=3600, public"
</VirtualHost>

This came a full day of apache research, but there was one article that helped me make the final leap with the rewrite stuff.

http://dltj.org/article/apache-httpd-and-tomcat/

No comments: