Running Tomcat Behind Apache with mod_rewrite and mod_proxy

By December 5, 2013 HowTo 3 Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

apache-tomcat-7.1

In this article we will cover the subject of fronting a Tomcat application server with Apache.

Tomcat is a secure, fast and full featured server. It is able to reliably serve a massive volume of data. That is why it is quite comparable in performance with native web servers.

This brings up the question: Why do we need to put Tomcat behind an Apache server? Such a solution is widely used to improve the performance of high-load environments. Being a fast, secure and flexible HTTP server and having a variety of modules, Apache easily provides some additional functionality for various purposes.

Here are just a few reasons to use your Tomcat app server behind Apache:

  • High availability by providing load balancing between multiple Tomcat app servers

  • Static content can be processed and delivered much faster using Apache as front-end

  • Additional security issues can be easily configured in Apache and used for protecting your application located on Tomcat server

  • Extra functions can be added by using a wide range of Apache modules

There are different ways to interconnect Apache and Tomcat. The most popular method is to set up this connection using mod_proxy or mod_rewrite which are rather easy, in configurations. With these basic configurations, Apache starts passing on requests to your back-end Tomcat server and after that, relays the responses back to the client.

Below we’ll show you three widely-used cases of using mod_rewrite and mod_proxy for running a Tomcat server behind Apache in the Jelastic cloud:

Rewriting links

Let’s imagine that you are running two or more Java applications on separate application servers and different ports.

As an example we have deployed simple apps which show the IPs of the servers to easily differentiate them.

  • http://env-tomcat.jelastic.com/app1/

first tomcat application

  • http://second-tomcat.jelastic.com/app2/

second tomcat application

Using Apache with mod_rewrite you can have both of your apps available on one port just with different paths. For example:

  • http://env-apache.jelastic.com/application1/
  • http://env-apache.jelastic.com/application2/

This allows each application to be restarted, managed and debugged separately. At the same time, your end-users will consider them as one application.

Let’s see how to set this in Jelastic:

1. You need to have three (or more) environments of the following type:

  • two (or more) back-end Tomcat environments with your Java apps deployed
  • one front-end Apache environment

envs for rewrite

2. Click Config for your Apache application server.

3. Navigate to the conf folder and open httpd.conf file.

Make the following configurations in the < VirtualHost >  block and Restart your Apache server:

<VirtualHost *:80>
   ServerAdmin webmaster@domain.com
   DocumentRoot /var/www/webroot/ROOT
   ServerName website.jelastic.com
   ServerAlias *

   RewriteEngine On

   RewriteRule ^/application1/(.*) http://env-tomcat.jelastic.com/app1/ [P]
   ProxyPassReverse /application1/ http://env-tomcat.jelastic.com/app1/

   RewriteRule ^/application2/(.*) http://second-tomcat.jelastic.com/app2/ [P]
   ProxyPassReverse /application2/ http://second-tomcat.jelastic.com/app2/

   RewriteLog "/var/log/httpd/rewrite.log"

   ErrorLog logs/dummy-host.jelastic.com-error_log
   CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

apache httpd.conf

  • RewriteEngine On enables rewriting ability

  • RewriteRule and ProxyPassReverse specify the conditions and the result of rewriting for both applications

  • RewriteLog is optionally added to store the logs of rewriting in the specified location

4. To check the result click Open in browser for your Apache environment and add the path you have specified in the RewriteRule as a condition to open the right application.

In our case we add:

  • /application1/

rewrite first app

  • /application2/

rewrite second app

As you see, the required two applications are opened under the same port, just with a different path.

Such an approach can also be used to make clean and more descriptive links as users and search engines prefer to get useful information about page content from the URL.

Serving static content

To make your application work faster, you can distribute the activities between your Tomcat and Apache servers. Let your Tomcat perform the main functions by serving the application and Apache will be responsible for static content delivery. As a result, your application will be able to serve more concurrent users.

static-content

Let’s see how this can be configured in Jelastic using mod_proxy:

1. You need to have at least two environments:

  • one with the Tomcat server for your application running

  • one with the Apache server for storing static content

static content envs

2. Click Config for your Apache app server.

3. Upload your static files to the webroot > ROOT folder.

You can create a separate folder for your static content (e.g. static) as it is shown in the image below and upload your files to it.

static content

4. Navigate to the conf folder and open httpd.conf file.

Make the following configurations in the < VirtualHost >  block and Restart your Apache server:

<VirtualHost *:80>
   ServerAdmin webmaster@domain.com  
   DocumentRoot /var/www/webroot/ROOT
   ServerName website.jelastic.com
   ServerAlias *

   ProxyPass        /static !

   ProxyPass        / http://env-tomcat.jelastic.com/app1/
   ProxyPassReverse / http://env-tomcat.jelastic.com/app1/

   ErrorLog logs/dummy-host.jelastic.com-error_log
   CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

static content settings

ProxyPass  /static !  states that we don’t proxy any request beginning with the keyword /static/.

The rest of the requests are going to be proxied to our application, deployed on Tomcat (the path to the app is defined by ProxyPass and ProxyPassReverse).

5. To check the result, click Open in browser for your Apache environment and you’ll see your application proxied from your Tomcat server.

proxied tomcat application

6. Specify the path to your static content in the URL and it will be also available.

static folder

static image

As a result both the app located on Tomcat and static content stored in Apache are available at the same port.

Load balancing

To handle an additional load or to just get some sort of fail-over, you can add more Tomcat application servers. And, an Apache server can be used in order to distribute the load between these Tomcat servers.

apache load balancing

Let’s configure Apache for load balancing in your Jelastic Java applications using mod_rewrite:

1. For this configuration we’ll use three environments:

  • two back-end Tomcat environments with Java apps deployed

Note that you need to use the same context for deploying both applications. It’s even better to create one environment and then clone it to have identical copies.

  • one front-end Apache environment

envs for load balancing

2. Click Config for your Apache server.

3. Navigate to the conf.d folder and create a new file e.g. server_list.

4. In the created file, add the hosts of your environments with deployed applications in the following format:

servers {env1_name}.{hoster_domain}|{env2_name}.{hoster_domain}

server list

5. Go to the conf folder and make the following configurations in the httpd.conf file:

<VirtualHost *:80>
   ServerAdmin webmaster@domain.com
   DocumentRoot /var/www/webroot/ROOT
   ServerName website.jelastic.com
   ServerAlias *
   RewriteEngine On
   RewriteMap lb rnd:/etc/httpd/conf.d/servers.list
   RewriteRule ^/(.*) http://${lb:servers}/app1/$1 [P,L]
   RewriteLog "/var/log/httpd/rewrite.log"
   ErrorLog logs/dummy-host.jelastic.com-error_log
   CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

load balancing settings

  • RewriteEngine On enables rewriting ability

  • RewriteMap states the path to the hosts listed in the earlier created file

  • RewriteRule specifies the conditions of the load balancing

  • RewriteLog is optionally added to store the logs of rewriting in the specified location

6. Restart your Apache server.

7. To see the result, click Open in browser for your Apache application.

You’ll see one of your applications deployed on Tomcat.

Refresh the page (once or several times) and as a result of load distribution your second app will be opened.

 first app

second app

These are just some basic cases of using Apache as the front-end of your Tomcat server. You can get even more benefits from combining Apache with Tomcat. We hope that these instructions will help you to make your application even more high-performing, flexible and stable. Let us know about your experience in fronting Tomcat with an Apache server in the comments below.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

3 Comments

  • Fabrizio says:

    What if my webapp needs to create static resources that have to be served by Apache? The second environment doesn’t have rights to do that…

    • Tetiana Fydorenchyk says:

      This can be gained by using webdav. Please contact your hosting provider’s support and they will set this for you.

  • bandia says:

    hi:
    i can also use proxy to implement Rewriting links,but what is the difference between the two methods(proxy and rewriting)

Leave a Reply

Subscribe to get the latest updates