How do Apache httpd and Tomcat work together?

IAmYourFaja picture IAmYourFaja · Mar 20, 2012 · Viewed 7.9k times · Source

I am inheriting a project involving a Java web app whose backend is powered by an Apache httpd/Tomcat combo. The web server is being used to serve back JS, static content, and to perform general load balancing, and Tomcat is serving back JSPs via a single WAR file.

I will be receiving access to the code base later on today or tomorrow, but wanted to try and do some research ahead of time.

My question can be summed up as: how do these two work together?

  • Who first receives HTTP requests?
  • How does httpd know when to forward JSP requests on to Tomcat, or to just respond to a request itself?
  • How does httpd "pass" the request to, and "receive" the response from, Tomcat? Does it just "copy-n-paste" the request/response to a port Tomcat is listening on? Is there some sort of OS-level interprocess communication going on? Etc.

These are just general questions about how the technologies collaborate with each other. Thanks in advance!

Answer

T.J. Crowder picture T.J. Crowder · Mar 20, 2012

Who first receives HTTP requests?

Apache, almost certainly. There could be admin processes that talk directly to Tomcat, though.

How does httpd know when to forward JSP requests on to Tomcat, or to just respond to a request itself?

From its configuration. The specifics will vary. It might, for instance, be using mod_jk or mod_jk2, in which case you'll find JkMount directives in the config files, e.g.:

JkMount /*.jsp ajp13_worker

...which tells it to pass on requests at the root of the site for files matching *.jsp to the ajp13_worker, which is defined in the workers.properties file.

Or it could be set up in a simple HTTP reverse-proxy arrangement. Or something else.

How does httpd "pass" the request to, and "receive" the response from, Tomcat?

It depends on the configuration; it could be HTTP, it could be AJP, or it could be using some other module.

Does it just "copy-n-paste" the request/response to a port Tomcat is listening on?

Sort of. :-) See the reverse-proxy link above.

Is there some sort of OS-level interprocess communication going on?

Yes. AFAIK, it's all socket-based (rather than, say, shared memory stuff), which means (amongst other things) that Tomcat and Apache need not be running on the same machine.