You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dhruva Reddy <bd...@gmail.com> on 2012/12/10 22:22:43 UTC

Running Tomcat Behind Apache HTTP Server

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 (http://www.example.com), 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 http://www.example.com/application/).

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

Re: Running Tomcat Behind Apache HTTP Server

Posted by André Warnier <aw...@ice-sa.com>.
Dhruva Reddy wrote:
> Thanks, André, for your response.
> 
> To give you a little more context, I needed to get this working as soon as
> possible, which I basically have.  Eventually, yes, I want the static
> content to be served by HTTPD, and the dynamic content to be served by
> Tomcat.  I did not want to deploy my application as ROOT, for reasons that,
> now that I think about it, don't really make sense.
> 
> Anyway, your clue about the Directory directive was very helpful.  It
> turned out that my rewrite rules were being ignored.  I ended up with two
> rules, each on the condition that the request URI does not already contain
> the context.  I settled on the following (I'm running a Grails application
> which automagically recognizes the "html" extension):
> 
> RewriteEngine On
> RewriteCond $0 !^/application/(.*)
> RewriteRule ^(.*)/$ ajp://localhost:8009/application/$1 [P]
> RewriteCond $0 !^/application/(.*)
> RewriteRule ^(.*)\.html$ ajp://localhost:8009/application$1.html [P]
> 
> I know I still have some work to do, but at least I now have it basically
> working.
> 
Good if it works. You did not originally specify which Apache-Tomcat connector you were 
using, so I took a potshot and guessed mod_jk, wrongly.

About the rules above :
- what happens when the URL already starts with "/application" ?
- the capture part in the RewriteCond above does not seem to be necessary, as you are 
testing for a "not match", so whatever is captured wouldn't be useful anyway.
- what happens when the Tomcat webapp returns a redirect ?
   Do you have some ProxyPassReverse directive for that ?

If I may comment, I believe that by forwarding "/" to tomcat's "/application", and 
afterward having other sub-sections of "/" not being forwarded to Tomcat, you are setting 
yourself up for a lot of complications later on.
A lot of directive's effects in Apache are "inherited" from a top location to 
sub-locations by default, and you are going to find yourself obliged to countermand this 
inheritance in each sub-section. When you start mixing this with the relative paths that 
tomcat will generate for embedded images etc, and with (maybe) authentication and 
access-control, you'll be in for a treat.
If you are just starting this website's setup, it may be better to pause and think of a 
simpler layout.
Just my two cent.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Running Tomcat Behind Apache HTTP Server

Posted by Dhruva Reddy <bd...@gmail.com>.
Thanks, André, for your response.

To give you a little more context, I needed to get this working as soon as
possible, which I basically have.  Eventually, yes, I want the static
content to be served by HTTPD, and the dynamic content to be served by
Tomcat.  I did not want to deploy my application as ROOT, for reasons that,
now that I think about it, don't really make sense.

Anyway, your clue about the Directory directive was very helpful.  It
turned out that my rewrite rules were being ignored.  I ended up with two
rules, each on the condition that the request URI does not already contain
the context.  I settled on the following (I'm running a Grails application
which automagically recognizes the "html" extension):

RewriteEngine On
RewriteCond $0 !^/application/(.*)
RewriteRule ^(.*)/$ ajp://localhost:8009/application/$1 [P]
RewriteCond $0 !^/application/(.*)
RewriteRule ^(.*)\.html$ ajp://localhost:8009/application$1.html [P]

I know I still have some work to do, but at least I now have it basically
working.


On Mon, Dec 10, 2012 at 5:43 PM, André Warnier <aw...@ice-sa.com> wrote:

> Jeff Beard wrote:
>
>> On Mon, Dec 10, 2012 at 2:22 PM, Dhruva Reddy <bd...@gmail.com> 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 (http://www.example.com), 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 http://www.example.com/**application/<http://www.example.com/application/>
>>> ).
>>>
>>> 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:
>>
>> http://tomcat.apache.org/**connectors-doc/generic_howto/**quick.html<http://tomcat.apache.org/connectors-doc/generic_howto/quick.html>
>>
>>
>>
> 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
> </Location>
>
> 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 is 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 you 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 "a bit late" in the Apache cycle.  By the time Apache looks
> inside a <Directory> section, it 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: users-unsubscribe@tomcat.**apache.org<us...@tomcat.apache.org>
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
“Most people would rather die than think. In fact, they do so.” - Bertrand
Russell

Re: Running Tomcat Behind Apache HTTP Server

Posted by André Warnier <aw...@ice-sa.com>.
Jeff Beard wrote:
> On Mon, Dec 10, 2012 at 2:22 PM, Dhruva Reddy <bd...@gmail.com> 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 (http://www.example.com), 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 http://www.example.com/application/).
>>
>> 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:
> 
> http://tomcat.apache.org/connectors-doc/generic_howto/quick.html
> 
> 

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
</Location>

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 is 
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 you 
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 "a 
bit late" in the Apache cycle.  By the time Apache looks inside a <Directory> section, it 
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Running Tomcat Behind Apache HTTP Server

Posted by Jeff Beard <je...@cyberxape.com>.
On Mon, Dec 10, 2012 at 2:22 PM, Dhruva Reddy <bd...@gmail.com> 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 (http://www.example.com), 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 http://www.example.com/application/).
>
> 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:

http://tomcat.apache.org/connectors-doc/generic_howto/quick.html


Cheers,

Jeff