Sunday, January 28, 2007

Using Apache as a Proxy for Tomcat Applications

I spent the day trying some configurations to make the integration between Apache HTTP Server and Tomcat.

The configuration that worked best for me basically was to setup a VirtualHost in Apache as a proxy with the same name as the application in Tomcat engine. As a resume, below tou will find some of the steps I went through and what I changed in the configuration files (I am using Apache 2.2 and Tomcat 5.5).

In Apache it is necessary to enable the mod_proxy Module and setup a NameVirtualHost. The httpd.conf (found in %APACHE_HOME%/conf) configuration file should be changed as below.

#Uncomment or add the lines below into the general session of your httpd.conf file.
LoadModule proxy_module modules/
LoadModule proxy_http_module modules/

# Add the Virtual Host for your application at the end of the file
NameVirtualHost *

<VirtualHost *>
  DocumentRoot "C:/java/Tomcat5.5/webapps/"
  ServerAlias *

  ProxyPass /foo-app http://localhost:8080/foo-app
  ProxyPassReverse /foo-app http://localhost:8080/foo-app

After setting this file in your apache server, restart it and proceed to configuring the Tomcat Server, by editing the server.xml file (found in %CATALINA_HOME%/conf). Simply add the lines below at the end of the file, but inside the <server> tag.

<Service name="Tomcat-Apache">
    <Engine name="Apache" defaultHost="" debug="0">
    <Host name="" debug="0"
            unpackWARs="true" autoDeploy="true">

        <Context path="/foo-app" docBase="c:/java/Tomcat5.5/webapps/foo-app" debug="1" reloadable="true"/>



Now, open your browser and type the URL and you should be redirected to your application after passing by Apache and Tomcat.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.