JavaRanch Home    
 
This page:         last edited 15 September 2007         What's Changed?         Edit

Proxy Apache Httpd And Tomcat   

This describes a way to proxy Tomcat requests through Apache Httd Web Server. I found mod_jk way overly complicated for development purposes. Whether or not proxies are a good idea in production, I do not know.



************************* Warning !!!! **************************************************

I found this article while doing a search and noticed some very bad code, specifically this:

<IfModule mod_proxy.c>
    ProxyRequests On
   
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

The above code will allow anyone to forward stuff like spam through your Apache server!!

I think that the original author really wanted to set up a reverse proxy, not a forward proxy. Forward proxies are used for things like providing Intranet members with a connection to the Internet. Reverse proxies are used for clustering, load balancing, etc.

Unless you know what you are doing a good rule of thumb is to NEVER include this directive in your httpd.conf file:

    ProxyRequests On <-- NO!

In fact, unless you are truly creating a forward proxy, you should ALWAYS include this directive:

   ProxyRequests Off 

I have not changed anything in this article other than to add this warning.

H. Hall



I am assuming the following versions of Apache Httpd and tomcat

Apache Httpd 2.0.59

Tomcat 5.5.20

I am currently running Xubuntu Edgy and have installed Apache2 using apt-get.

First, you need to enable proxies in Apache. You can do the following commands from anywhere on the filesystem.

sudo ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf

sudo ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load

sudo ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load

Next, edit /etc/apache2/mods-available/proxy.conf with your favorite editor (like VIM) and turn proxies on for All domains. (You can also restrict to specific domains, but since this is local machine development, this is just as easy)


<IfModule mod_proxy.c>
    ProxyRequests On
   
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <!-- More configuration follows.  Change at your own risk -->
</IfModule mod_proxy.c>

Now that proxies are enabled, we need to define one. Create a file with the same name as your web application context in the following location:

/etc/apache2/sites-available/

For this example, our app is called testApp. Using your favorite editor (like VIM) use the following configuration:


<VirtualHost *>
    ServerAdmin youremail@address.com
    DocumentRoot "Path/to/static/content"
    DirectoryIndex index.html
    ServerName local.somedomain.com
    ErrorLog /var/log/apache2/local.somedomain.com-error_log
    CustomLog /var/log/apache2/local.somedomain.com-access_log common
    ProxyPass /testApp http://localhost:8080/testApp
    ProxyPassReverse /testApp http://localhost:8080/testApp
    ProxyPreserveHost On
    <Location /testApp>
      Order deny,allow
      Allow from all
    </Location>
</VirtualHost>

DocumentRoot? points to the location where you are serving static content from, like images, styles, scripts, etc. The ProxyPass? and ProxyPassReverse? tells apache httpd to send all dynamic requests to Tomcat.

Now, provide a sym link for this site in /etc/apache2/sites-enabled

sudo ln -s /etc/apache2/sites-available/testApp /etc/apache2/sites-enabled/testApp

The last thing is to edit your hosts file (/etc/hosts) to include the ServerName? you have specified:

127.0.0.1 local.somedomain.com

Now, just restart Apache (sudo apache2ctl restart) and you should be good to go. So now, rather than using port 8080, you would access your web app using the following:

http://local.somedomain.com/testApp


JavaRanchContact us — Copyright © 1998-2012 Paul Wheaton