3
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?

2 Answers 2

10

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.

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

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;
}
6
  • @ErikA,thanks for your reply!But seems this only works for specific requests,right?
    – yum
    Apr 7, 2011 at 3:36
  • Yes, it would be invoked for requests to /images/.
    – EEAA
    Apr 7, 2011 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, 2011 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, 2011 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, 2011 at 3:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .