1

I'm having nginx serve static files that map from uri paths to folder paths like:

www.tempuri.org/js <=> /var/www/plublic/js
www.tempuri.org/css <=> /var/www/plublic/css
www.tempuri.org/img <=> /var/www/plublic/img
www.tempuri.org/foobar <=> /var/www/plublic/foobar

In my nginx conf, the rules for this, essentially all have the same root:

location /js/ {
   root /var/www/public/;
}
location /css/ {
   root /var/www/public/;
}
location /foobar/ {
   root /var/www/public/;
}
location /img/ {
   root /var/www/public/;
}

How can I write a single rule for all 4 paths?

location /(css|js|img|foobar)/ {
   root /var/www/public
}

?

3

root should be placed in the server block, not in the location block. This is one of the most common nginx misconfigurations.

Making this change will also fix your issue and eliminate the need for that location block.

  • Great! So followup question: in my single server tempuri.org, I'm serving two different apps, that each have seperate root paths? How would I accomplish this, using that same notion of having a single root path? – Alan May 22 '13 at 2:07
  • 1
    The other app can use a different root, or more likely an alias. Depending on the characteristics of the app. But like you said, that really is a separate question. The root that appears in the server block is the default, and applies to any location that doesn't explicitly specify one. – Michael Hampton May 22 '13 at 2:08
3

You pretty much have it.

location ~ ^/(css|js|img|foobar)/ {
    root /var/www/public/;
}

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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