DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Deployment of Spring MVC App on a Local Tomcat Server
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • Redis-Based Tomcat Session Management
  • Deploy Spring Boot Apps From Jar to War

Trending

  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  1. DZone
  2. Coding
  3. Languages
  4. Running Tomcat Behind Apache With mod_rewrite and mod_proxy

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.

By 
Tetiana Fydorenchyk user avatar
Tetiana Fydorenchyk
·
May. 09, 18 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
14.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  • http://env-tomcat.jelastic.com/app1/differentiate tomcat server within ip
  • http://second-tomcat.jelastic.com/app2/
    apache tomcat server ip

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

Apache Tomcat application

Published at DZone with permission of Tetiana Fydorenchyk, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deployment of Spring MVC App on a Local Tomcat Server
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • Redis-Based Tomcat Session Management
  • Deploy Spring Boot Apps From Jar to War

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!