Forwarding traffic from Apache2 to Tomcat on Ubuntu

I have several Java webapps running under Tomcat that I wanted to be accessible through Apache2 running on port 80 on my home server. It turns out that it's pretty easy to do, though I didn't find an example anywhere that included all the steps I needed to take to get it working. Most of what I need to know was at this blog post.

First of all, here are the exact versions I'm running. I wouldn't be surprised if there are newer major versions; I need to upgrade the server.
  • Apache2: 2.2.11
  • Tomcat: 6.0.18
We're going to proxy traffic back and forth between Apache2 and Tomcat using Apache JServ Protocol. Apache ships with support through the module proxy_ajp, and Tomcat has a built-in connector.
  1. First we need to enable proxy_ajp on Apache2. The proxy and proxy_balancer modules are required, but they'll be enabled as needed by a2enmod.
    $ sudo a2enmod proxy_ajp
    Considering dependency proxy for proxy_ajp:
    Enabling module proxy.
    Enabling module proxy_ajp.
    Run '/etc/init.d/apache2 restart' to activate new configuration!
  2. Next we need to add the proxy rule for Apache2 in /etc/apache2/conf.d/proxy_ajp, which I had to create. You can add as many of these blocks as you need, but the contexts must be unique, i.e. you can't create two proxies to talk to the same webapp. Paste the following into the proxy_ajp conf file, replacing WEBAPP with the context of the webapp for which the proxy is being setup.
    ProxyPass /WEBAPP ajp://localhost:8009/WEBAPP
    ProxyPassReverse /WEBAPP ajp://localhost:8009/WEBAPP
    <Proxy /WEBAPP>
      Order deny,allow
      Allow from all
  3. Finally, we need to enable the AJP connector in Tomcat. To do so, simply uncomment the following line in /etc/tomcat6/server.xml. Of course you can change the port as desired, but make sure the change is applied to the proxy_ajp conf file as well.
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  4. Restart Tomcat.
    sudo /etc/init.d/tomcat6 restart
  5. Restart Apache2.
    sudo /etc/init.d/apache2 restart
At one point I was seeing the following error in Tomcat's log.
Jun 26, 2011 9:35:17 PM org.apache.jk.common.MsgAjp processHeader
SEVERE: BAD packet signature 18245
Jun 26, 2011 9:35:17 PM org.apache.jk.common.ChannelSocket processConnection
SEVERE: Error, processing connection
        at java.io.BufferedInputStream.read(BufferedInputStream.java:327)
        at org.apache.jk.common.ChannelSocket.read(ChannelSocket.java:621)
        at org.apache.jk.common.ChannelSocket.receive(ChannelSocket.java:578)
        at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:686)
        at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
        at java.lang.Thread.run(Thread.java:636)
Most likely I had a mistake in the proxy_ajp file, because all it took was to restart Apache2 again, and I haven't seen the error since.

1 comment:

  1. Thanks a lot. It's surprising how hard it is to find just this information.