Knowledge Base
MilesWeb / Cloud Servers

How to Run Tomcat Behind Apache with mod_rewrite and mod_proxy?

Approx. read time : 6 min

This article will cover the topic of fronting a Tomcat application server with Apache and involves the three steps as below:

Tomcat is identified as a fast, secure and completely featured server. It can consistently provide a large volume of data. Therefore, it offers high performance in comparison to the native web servers.

This give rise to a question: Why is it required to put Tomcat behind Apache server? This type of solution is extensively used to enhance the performance of high-load environments. Apache is a fast, secure and flexible HTTP server that comprises of variety of modules and so, it easily offers additional functionality to some extent for several purposes.

Below are some of the reasons for using your Tomcat app server behind Apache:

  • You get high availability of Apache server when load balancing is provided between multiple Tomcat app servers.
  • The process and delivery of static content speeds up by using Apache as front-end
  • It is easy to configure additional security issues in Apache and those can be used to protect your application located on Tomcat server.
  • You can also add extra functions with the use of wide range of Apache modules.

Apache and Tomcat can be interconnected in different ways. One of the popular methods is by setting up the connection with mod_proxy or mod_rewrite which are quite easy in terms of configurations. These basic configurations help Apache start passing on request to the back-end Tomcat server and conveys the responses back to the client.

Below are three widely-used cases of using mod_rewrite and mod_proxy to run a Tomcat server behind Apache in the MilesWeb cloud powered by Jelastic:

Rewriting Links

Imagine that you are running two or more Java applications on different application servers and different ports. For example, we have deployed simple apps showing the IPs of the servers to easily differentiate them.

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

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

When you use Apache with mod_rewrite, you will find both of your apps available on one port but with different paths. For example:

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

This enables each application to restart, manage and debug separately. Simultaneously, your users will consider them as single application.

Let’s check the steps for setting up this in MilesWeb:

1. It is required to have three (or more) environments of the following type:

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

2. Click on Config for your Apache application server.
3. Go to the conf folder and open the file with httpd.conf name. Configure the file as below in <VirtualHost> block and Save the changes:

<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/$1 [P]
ProxyPassReverse /application1/ http://env-tomcat.jelastic.com/app1/
RewriteRule ^/application2/(.*)$ http://second-tomcat.jelastic.com/app2/$1 [P]
ProxyPassReverse /application2/ http://second-tomcat.jelastic.com/app2/ErrorLog logs/dummy-host.jelastic.com-error_log
CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

  • RewriteEngine On enables the ability of rewriting
  • RewriteRule and ProxyPassReverse state the conditions and the result of rewriting for both applications
  • RewriteLog is added as an option for storing the logs of rewriting in the specified location

4. Restart Apache To check the result, click on Open in browser and add the path specified in the RewriteRule as a condition to open the right application. In our case we add:

/application1/

/application2/

The image above displays that the required two applications are opened under the same port, simply with a different path.

This type of approach can also be used for making clean and more descriptive links as users and search engines prefer getting useful information about page content from the URL.

Serving Static Content

For your application to work faster, the activities can be distributed between your Tomcat and Apache servers. With this, your Tomcat will perform the key functions by serving the applications and Apache will be responsible for delivering static content. This will help you applications to server more concurrent users.

Let’s check the configuration of this using mod_proxy:

1. Start with creating two environments:

  • one with the Tomcat server for your application running
  • one with the Apache server for storing static content

2. For Apache server click on Config and create a separate folder for the static content (e.g. static) in the webroot > ROOT folder as shown below and upload your files to it.

3. Go to the conf folder and open httpd.conf file. Configure the file as below in the <VirtualHost>  block and Save the changes:

<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>

ProxyPass  /static !  indicates that any request beginning with the keyword /static/ (you need to state their the name of your folder for content) isn’t proxied. The other remaining entries are going to be proxied to our application, deployed on Tomcat (the path to the application is defined by ProxyPass and ProxyPassReverse).

4. Restart Apache server and for the result, click on Open in browser for your Apache environment. You will see your application proxied from your Tomcat server.

5. Define the path to your static content in the URL and it will be available too.

This results into availability of both – the application located on Tomcat and static content stored in apache on the same port.

Load Balancing

When there is additional load or there is some kind of failover, more Tomcat application servers can be added. You can use the Apache server for distributing the load between these Tomcat servers.

This is possible within a single environment (where Apache will be chosen as a load balancer) or in two different environments. In the first case, it is possible to add Apache server when you create the environment via topology wizard:

This will result into automatic even distribution of the load among the Tomcat servers by Apache.

If there are multiple environments with different applications deployed, you need to create a separate environment with Apache and configure it for load balancing with mod_rewrite.

Below are the main steps to configure Apache with mod_rewrite using three environments:

  • Two back-end Tomcat environments with deployment of Java applications.

Note that the same context is needed to be used for deploying both applications.

  • One front-end Apache environment.

1. For your Apache server click Config. Got to the conf.d folder and create a new file e.g. servers_list.

2. In this file, add the hosts of your environments with applications deployed in the below format:

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

3. Navigate to the conf folder and configure the httpd.conf file as below:

<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]ErrorLog logs/dummy-host.jelastic.com-error_log
CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

  • RewriteEngine On enables ability of rewriting
  • RewriteMap defines the path to the hosts mentioned in the earlier created file
  • RewriteRule defines the conditions of the load balancing
  • RewriteLog is added optionally to store the logs of rewriting in the particular mentioned location

4. Restart your Apache server.

5. For checking the result for Apache application, click on Open in browser. One of the applications will be seen deployed on Tomcat. Refresh the page (once or several times) and as the load is distributed, your second app will be opened.

The above mentioned are just some of the basic cases to use Apache as a front-end of your Tomcat server. There can be several other benefits opted by combining Apache with Tomcat. We hope you will be able to improve your application performance as well as make it flexible and stable with this tutorial.

Let us know about your experience in fronting Tomcat with an Apache server in the comments below.

Need help? We’re always here for you.