tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier>
Subject Re: Running Tomcat Behind Apache HTTP Server
Date Mon, 10 Dec 2012 22:43:05 GMT
Jeff Beard wrote:
> On Mon, Dec 10, 2012 at 2:22 PM, Dhruva Reddy <> wrote:
>> Hello,
>> I have a question about running Tomcat behind Apache HTTP server.  I hope
>> this is not inappropriate for this list.
>> I am running a website on Tomcat 7.0.33, behind Apache 2.2.15, on CentOS
>> 6.3 (using Tomcat connector 1.2.37).  I would like the user to go to our
>> base URL (, but have the request served by a web
>> application running on Tomcat (I have the connector working, so that you
>> can access the application through
>> I thought this could be accomplished through a rewrite rule on the HTTP
>> server, but I can't seem to get it to work, nor can I find much information
>> on it.  I can't really find any best practices around this, either.  Has
>> anyone done this successfully?
>> I have tried many things, but this is what I currently have in httpd.conf:
>> <Directory "/var/www/html">
>>     Options Indexes FollowSymLinks
>>     AllowOverride None
>>     Order allow,deny
>>     Allow from all
>>     RewriteEngine On
>>         <IfModule mod_rewrite>
>>         RewriteBase /
>>         RewriteRule ^/ application/ [NC]
>>         RewriteLog "/etc/httpd/logs/rewrite.log"
>>         RewriteLogLevel 9
>>     </IfModule>
>> </Directory>
>> I did confirm that the rewrite module is loaded.  Any insights would be
>> greatly appreciated.
>> Thanks,
>> Dhruva
> Dhruva,
> Based on the snippet of configuration you've provided it isn't clear that
> you've configured the Tomcat Connector. Perhaps there's more that you
> haven't shown us? Anyway, most recently I've configured Apache much like
> this document describes:

I have another couple of questions :
1) considering that you want that "^/" (the top of your Apache httpd's URL space) be 
forwarded to Tomcat, do you have anything that would /not/ be forwarded to Tomcat ?
If yes, where would that part live (in which directory/URL space of your httpd server) ?
If no, why do you even need the front-end Apache httpd ?
2) have you considered making your Tomcat application "/application" be the "/ROOT" 
default webapp of Tomcat ? Then it would be mapped to the path "/" in Tomcat also, and no

rewriting would be required.

But, assuming for now that you need a front-end httpd and that you want to forward 
everything to "/application" in Tomcat, do this instead :

RewriteEngine On
RewriteLog "/etc/httpd/logs/rewrite.log"
RewriteLogLevel 9

<Location />
      Order allow,deny
      Allow from all
      RewriteRule (.*) /application/$1 [NC]
      SetHandler jakarta-servlet    <-- this is what forwards to Tomcat

Explaining all the changes in detail would be a lot here, but here's a few notes :
1) SetHandler jakarta-servlet : replaces "JkMount" instructions, with similar effect. It 
tells Apache that for this, you want mod_jk (the Apache-Tomcat connector) to generate the

response, which it does by forwarding the request to tomcat, and collecting tomcat's response.
2) removal of <IfModule> directives : you don't want them here, I guess. If mod_rewrite
not there, none of your Rewrite directives will be understood, and your Apache won't 
start. That is probably what you really want; you don't just want Apache to do something 
else then.
3) you can move the 3 first Rewrite* directives back into the <Location> section if
want.  But where they are, they will be inherited by it anyway.
4) I think (I am not 100% sure) that using a <Directory> section for this is already
bit late" in the Apache cycle.  By the time Apache looks inside a <Directory> section,
has already parsed the URL, and mapped it to a disk directory. In this case, you don't 
want a disk directory (you may not even have one there). You want to catch this earlier, 
as soon as Apache is just looking at the URL. That's what <Location> sections are about.
5) Inside a <Directory> or <Location> section, the first element to the Rewrite
rule is 
matched against what remains of the URL path, after stripping the portion which made this

Directive being interpreted inside that section. So in
    <Location />
      RewriteRule (.*) /application/$1 [NC]
the "(.*)" matches everything after "/", and is captured in $1.
Then you re-inject that same $1 value in the rewritten path /application/$1.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message