Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I run Tomcat by itself or Apache + Tomcat?

Tags:

apache

tomcat

I was wondering if it would be okay to run Tomcat as both the web server and container? On the other hand, it seems that the right way to go about scaling your webapp is to use Apache HTTP listening on port 80 and connecting that to Tomcat listening on another port? Are both ways acceptable? What is being used nowdays? Whats the prime difference? How do most major websites go about this?

Thanks.

like image 721
OckhamsRazor Avatar asked Sep 09 '12 21:09

OckhamsRazor


People also ask

Is Apache Tomcat same as Tomcat?

There are many ways to compare Tomcat vs. the Apache HTTP Server, but the fundamental difference is that Tomcat provides dynamic content by employing Java-based logic, while the Apache web server's primary purpose is to simply serve up static content such as HTML, images, audio and text.

Do you need Apache to run Tomcat?

Tomcat is a web server of its own, so a separate web server like Apache is not required.

Can we use Apache and Tomcat together?

To run Tomcat together with Apache: Apache needs to load a "adapter" module, which uses a certain protocol, such as Apache JServ Protocol (AJP), to communicate with the Tomcat, via another TCP port (port 8009 in the default configuration).

What is the advantage of Apache Tomcat?

Web app deployment made easy Apache Tomcat is Open-sourced, and it's free to use. Multiple applications can run at the same time without any issues. Apache Tomcat Has excellent built-in security features and provided a rich API sets.


1 Answers

Placing an Apache (or any other webserver) in front of your application server(s) (Tomcat) is a good thing for a number of reasons.

First consideration is about static resources and caching.

Tomcat will probably serve also a lot of static content, or even on dynamic content it will send some caching directives to browsers. However, each browser that hits your tomcat for the first time will cause tomcat to send the static file. Since processing a request is a bit more expensive in Tomcat than it is in Apache (because of Apache being super-optimized and exploiting very low level stuff not always available in Tomcat, because Tomcat extracting much more informations from the request than Apache needs etc...), it may be better for the static files to be server by Apache.

Since however configuring Apache to serve part of the content and Tomcat for the rest or the URL space is a daunting task, it is usually easier to have Tomcat serve everything with the right cache headers, and Apache in front of it capturing the content, serving it to the requiring browser, and caching it so that other browser hitting the same file will get served directly from Apache without even disturbing Tomcat.

Other than static files, also many dynamic stuff may not need to be updated every millisecond. For example, a json loaded by the homepage that tells the user how much stuff is in your database, is an expensive query performed thousands of times that can safely be performed each hour or so without making your users angry. So, tomcat may serve the json with proper one hour caching directive, Apache will cache the json fragment and serve it to any browser requiring it for one hour. There are obviously a ton of other ways to implement it (a caching filter, a JPA cache that caches the query etc...), but sending proper cache headers and using Apache as a reverse proxy is quite easy, REST compliant and scales well.

Another consideration is load balancing. Apache comes with a nice load balancing module, that can help you scale your application on a number of Tomcat instances, supposed that your application can scale horizontally or run on a cluster.

A third consideration is about ulrs, headers etc.. From time to time you may need to change some urls, or remove or override some headers. For example, before a major update you may want to disable caching on browsers for some hours to avoid browsers keep using stale data (same as lowering the DNS TTL before switching servers), or move the old application on another url space, or rewrite old URLs to new ones when possible. While reconfiguring the servlets inside your web.xml files is possible, and filters can do wonders, if you are using a framework that interprets the URLs you may need to do a lot of work on your sitemap files or similar stuff.

Having Apache or another web server in front of Tomcat may help a lot changing only Apache configuration files with modules like mod_rewrite.

So, I always recommend having Apache httpd in front of Tomcat. The small overhead on connection handling is usually recovered thanks to caching of resources, and the additional configuration works is regained the first time you need to move URLs or handle some headers.

like image 50
Simone Gianni Avatar answered Oct 05 '22 05:10

Simone Gianni