Configuring Apache to Proxy Requests to Tomcat
By : Josh -
A very common task when administering apache and/or tomcat is to setup apache to proxy requests to tomcat. The primary driver to using this configuration is to get apache to handle all of the front-end requests and some caching with tomcat serving up dynamic content (which is proxied through apache). This also allows apache to handle much of the security as it gets much more exposure to the internet at large than tomcat and has a great track record in this regard. You can also benefit from standing up multiple tomcat instances behind two or more apache instances to allow you to scale more effectively and where it is needed.
Before we get started with configuration, first install apache and tomcat. This is typically done using the package manager of your distribution. Using yum, it would be as follows:
yum install httpd tomcat6
Next, set both daemons to persist (start on boot) using chkconfig or your distributions method of choice and start each daemon:
chkconfig tomcat6 on chkconfig httpd on /etc/init.d/tomcat6 start /etc/init.d/httpd start
Next, configure apache to load the appropriate modules needed to proxy requests to tomcat by modifying /etc/httpd/conf/httpd.conf (or appropriate configuration file for your distribution):
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
Next, configure the virtual host that you’ll be using to proxy requests to tomcat (be sure to replace the port and IP with entries suitable to your environment):
<Proxy balancer://localhost> BalancerMember ajp://127.0.0.1:8080 min=10 max=100 loadfactor=1 </Proxy> ProxyPass / ajp://localhost/
Once that configuration is complete, restart or reload apache to take effect:
Note that this configuration relies up on tomcat and apache being on the same server and you can easily configure apache to proxy requests to tomcat on another server or VIP by replacing the localhost/127.0.0.1 occurrences above with the VIP, IP, or hostname of the tomcat instance(s).