Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
location ~ {
            root /var/www/static1/;
            root /var/www/static2/;

I tried the above,so that when /name.gif is requested ,if /var/www/static1/name.gif exists, then just use it as is;but if there isn't such a file,try to see if there's /var/www/static2/name.gif, is it possible to configure nginx this way?

share|improve this question

Assuming you're using nginx 0.7.24(?) or later, you can accomplish this with try_files:

location / {
    root /var/www;
    try_files /static1$uri /static2$uri =404;

Normally, having root inside location / is incorrect, but in this specific case, I don't think you want to set /var/www as your general root in case you end up adding other locations.

share|improve this answer
>"Normally, having root inside location / is incorrect" -- What's the correct way to configure this? – maxm Mar 16 '13 at 18:03
it is best practice to have root be in the server {} block, but I don't think this is a problem. – Collin Anderson Jul 25 '13 at 19:56

I'm fairly certain your exact configuration is not possible. To serve from multiple roots, you'd need to alias one root into your URL structure, something like this:

location  /images/ {
  alias  /path/to/images;
share|improve this answer
@ErikA,thanks for your reply!But seems this only works for specific requests,right? – yum Apr 7 '11 at 3:36
Yes, it would be invoked for requests to /images/. – EEAA Apr 7 '11 at 3:36
@ErikA,but I want it to check both directories if there's no such file under one of them... – yum Apr 7 '11 at 3:41
Or apart from nginx,is it possible to configure linux so that /var/www/static will merge both /var/www/static1 and /var/www/static2? – yum Apr 7 '11 at 3:45
No it is not. Is there a reason you just can't manually merge the two directories? The only reason I'd think you wouldn't be able to do that is if there were possible filename collisions, but in that case, how would nginx know which file to serve anyway? Seems like you're trying to solve this problem the wrong way perhaps. – EEAA Apr 7 '11 at 3:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.