Apache with mod_proxy and Tomcat

I’m putting this here, more as a reminder to myself, but also to perhaps help other people who want a simple no-frills guide to using Apache with mod_proxy to deal with your Tomcat content.

I wanted a URL like this from tomcat, http://myurl:8080/myapp to be http://myurl/myapp – nice and clean you see 🙂

The typical way to do this is to use Apache to handle all the http requests to http://myurl/myapp  and to use a proxy to forward those requests to say http://myurl:8080/myapp – meanwhile all the user sees is the lovely clean URLs. I’ll give a brief account of how to do this here.

1. If you’re using the latest release of Apache (2), mod_proxy should be installed already. If not, do this:

  • sudo apt-get install libapache2-mod-proxy-html
  • apt-get install libxml2-dev
  • add this to httpd.conf

LoadModule  proxy_module         /usr/lib/apache2/modules/mod_proxy.so
LoadModule  proxy_http_module    /usr/lib/apache2/modules/mod_proxy_http.so
LoadModule  headers_module       /usr/lib/apache2/modules/mod_headers.so
LoadModule  deflate_module       /usr/lib/apache2/modules/mod_deflate.so

  • Restart Apache to ensure there are no issues.

2. In your Apache httpd.conf file, you need to add two lines for each application you wish to have mod_proxy handle.

ProxyPass    /myapp http://myurl:8080/myapp

ProxyPassReverse    /myapp http://myurl:8080/myapp

This is now telling Apache to forward any requests to myapp to Tomcat essentially which is listening on port 8080.

3. Next, you need to modify server.xml to tell Tomcat about the forwarding that’s going on. For that, you add the following.

<Connector port=”8080″ proxyPort=”80″ proxyName=”myurl.proxy”/>

The proxyName is only required to help you in the logs.

Apparently mod_jk is better in terms of performance than mod_proxy, I can’t confirm or deny this from my tests, but mod_proxy works pretty well! I believe previous issues with mod_proxy were in older versions of Apache.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s