Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running Tomcat Behind Apache With mod_rewrite and mod_proxy

DZone's Guide to

Running Tomcat Behind Apache With mod_rewrite and mod_proxy

Learn how to combine Tomcat with Apache in order to bring load balancing and additional functionality to your application.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 your 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 that show the IPs of the servers to easily differentiate them.

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

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 up 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 applications deployed
  • one front-end Apache environment

back-end tomcat and front-end apache environment2. Click Config for your Apache application server.

3. Navigate to the conf folder and open the httpd.conf file. Make the following configurations in the <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/ [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/

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


Apache application server config

  • RewriteEngine On enables rewriting
  • 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. Restart the Apache server. Check the result by clicking Open in browser and adding the path you have specified in the RewriteRule as a condition to open the right application. In our case we add:

  • /application1/   apache front-end of tomcat servers
  • /application2/   apache in front of tomcat application servers

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. serving static content to the TomcatLet’s see how this can be configured in Jelastic using mod_proxy:

1. Get started with creating two environments:

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

2. Click Config for your Apache app server. And create a separate folder for your static content (e.g. static) in the webroot > ROOT folder as it is shown below and upload your files to it.  create a separate folder for static content3. Navigate to the conf folder and open the httpd.conf file. Make the following configurations 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>


make changes in configurationsProxyPass  /static !  states that we don’t proxy any request beginning with the keyword /static/ (you need to state their the name of your folder for content)The rest of the requests 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 check the result by clicking Open in browser for your Apache environment. You’ll see your application proxied from your Tomcat server.  mod_rewrite and mod_proxy

5. Specify the path to your static content in the URL and it will be also available.  path to your static content in the URLavailable static content in the URLAs a result, both the application located on Tomcat and static content stored in Apache are available at the same port.

Load Balancing

To handle additional load or just get some sort of failover, you can add more Tomcat application servers. And, an Apache server can be used in order to distribute the load between these Tomcat servers.  load balancing, apache distributes load over tomcat serversThis can be done within a single environment (where Apache will be chosen as a load balancer) or in two separate environments. In the first case, you can add Apache server while creating the environment via topology wizard:  install Apache application serverAs a result, this Apache will automatically start evenly distribute the load among the Tomcat servers.

If you have several environments with different applications deployed, you should create a separate environment with Apache and configure it for load balancing with mod_rewrite.

Below we describe the main steps that should be performed for this configuration using three environments:

  • Two back-end Tomcat environments with Java applications deployed.Note that you need to use the same context for deploying both applications.
  • One front-end Apache environment.  back-end Tomcat with Java application

1. Click Config for your Apache server. Navigate to the conf.d folder and create a new file e.g. servers_list.

2. 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}

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


configurations in the httpd.conf file

  • RewriteEngine On enables rewriting
  • 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

4. Restart your Apache server.

5. 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.  Apache as a front-end of your Tomcat servertomacat behind apache, mod_rewrite , mod_proxyThese are just some basic cases of using Apache as a 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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,tomcat ,apache server ,load balancing

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}