RE: nginx and tomcat integrated but how to serve static files

Hi pals,

I have my applications running on tomcat and to improve performance I
have
put nginx infront.Nginx proxy pass successfully pass all request to
tomcat
server.

There are some html static files and images in my application which I
don’t
to be served by tomcat. again, I am trying to as much as performance
boost
for my app.

I am tried to do some research but unable to get solutions.

please see my current config ans suggest!

server {
index index.html index.htm;
listen 192.168.0.16:80;
server_name localhost;
location / {
root /var/www/nginx-default;
index index.html index.htm index.jsp;
}

location /abc/ {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;

}

thanks in advance!

Irfan Khan

On Fri, Dec 07, 2012 at 04:38:20PM +0530, Irfan Khan wrote:

There are some html static files and images in my application which I don’t
to be served by tomcat. again, I am trying to as much as performance boost
for my app.

I am tried to do some research but unable to get solutions.

nginx chooses how to handle a request based on the location{} blocks
you have defined.

Currently, you have: if it starts with /abc/, proxy to tomcat;
otherwise,
serve from the filesystem.

So: which urls do you really want proxied to tomcat, and which do you
really want served from the filesystem?

If I guess that “url starts with /abc/ and ends in html” means “serve
from filesystem, not tomcat”, then you could add one line:

location /abc/ {

location ~ html$ {}

proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;

}

and a request for /abc/a.html will look for the file
/usr/local/nginx/html/abc/a.html (or strictly: abc/a.html below whatever
you have configured “root” to be).

Best would be to make the non-tomcat things be in a different url prefix
to the tomcat things – such as /abc/static, for example – because then
you could just use prefix locations. That depends on how your
application
is written, which may not be changeable.

f

Francis D. [email protected]

I’m still relatively new to nginx but find it to be great.

My high-level recommendation would be twofold:

  1. Make sure you define a “proxy_cache” (check the nginx website for
    details on these directives). In there you can define further how to
    cache anything that’s even a bit “static” from tomcat. And if you’re
    overriding specific paths or regex’es from tomcat (like if you know
    there are static images or something being served from your webapp that
    you can’t easily move out to the flat filesystem), you’ll want to add
    proxy_ignore_headers to override what tomcat is telling the browser, add
    the proxy_cache_valid, the Cache-Control header (see #2), and maybe
    “expires”.

  2. For images and other truly static content, add_header Cache-Control
    with some reasonable values. This will tell any downstream proxies and
    the actual client PC’s to cache those images locally, so they won’t be
    continuously fetched from your server.

There is no “one size fits all”, but the power contained in nginx is
really great. You just have to take some time to try the settings and
use something like fiddler on the desktop to see the differences when
you make changes. (Be sure to clear your local browser’s cache between
tests!)

Good Luck!

On Mon, Dec 10, 2012 at 09:55:01PM +0530, Irfan Khan wrote:

Hi there,

Thanks for the suggestion, but unfortunately it didn’t worked.

What one url did you try that you wanted to be served from the
filesystem,
but was instead served by tomcat? (And why do you think that it was
served by tomcat?)

What config file do you use that shows the problem?

f

Francis D. [email protected]

Hi,

Thanks for the suggestion, but unfortunately it didn’t worked. I have
tried
to create new location to be served by nginx but all requests goes to
tomcat
by next location directive. Any html within /foo/ directory doesn’t
work at
all.
I am a newbie to Nginx and really loves it. I hope some regex
combination
would help to solve the problem.

Please suggestion!

My config as follows;

Location /foo/*
Root /tomcat/webapps/abc/
Index.html

location /abc/ {

proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header
X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host
$http_host;

}

Irfan Khan
Lead - IT

eNovate Media Solutions Pvt Ltd
#204, 2nd Floor, Cunningham Classic
22, Cunningham Road
Bangalore - 560052
Voice - + 91 80 41657660 | Mob - +91 903 589 38 14
www.enovatemedia.com

On Tue, Dec 11, 2012 at 02:18:27PM +0530, Irfan Khan wrote:

Hi there,

My config is same as earlier. I had tried to access /foo/learning.html from
Nginx. But if I keep tomcat server off then I am unable to load this page.

If you use the config you posted in the first mail, /foo/learning.html
should give you the file /var/www/nginx-default/foo/learning.html, and
should not involve tomcat at all (so shouldn’t change whether tomcat is
on or off).

Does it give you that file?

If not, what does it give you instead?

Probably best is to copy-paste the output of

curl -i http://192.168.0.16/foo/learning.html

if it is not what you want it to be.

The later mails, including this one, don’t appear to include any other
valid nginx config.

If I keep tomcat server up and try for some files (blah.html)which is not
exists then tomcat reports an error on page.

curl -i http://192.168.0.16/blah.html

should show you the file /var/www/nginx-default/blah.html, or an nginx
http 404 message.

curl -i http://192.168.0.16/abc/blah.html

should show you whatever tomcat produces, if you use your original
configuration; or should show you the content of (probably)
/usr/local/nginx/html/abc/blah.html, if you add the change suggested.

I have also tried giving full access to webapps directive just for testing
purpose. Nothing worked!

I want to use the nginx config that you provide, to reproduce the error
that you report.

So far, I can’t.

f

Francis D. [email protected]

Hi,

My config is same as earlier. I had tried to access /foo/learning.html
from
Nginx. But if I keep tomcat server off then I am unable to load this
page.
If I keep tomcat server up and try for some files (blah.html)which is
not
exists then tomcat reports an error on page.

I have also tried giving full access to webapps directive just for
testing
purpose. Nothing worked!

Kindly suggest, my config is as follows;

Location /foo/*
Root /tomcat/webapps/abc/
Index.html

location /abc/ {

proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header
X-Forwarded-For
$proxy_add_x_forwarded_for; proxy_set_header Host $http_host;

}

Thanks and Regards,
Irfan Khan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs