nginx and tomcat integrated but how to serve static files

Francis Daly francis at
Fri Dec 7 16:01:28 UTC 2012

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.

Francis Daly        francis at

More information about the nginx mailing list