Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx same config for multiple paths

Tags:

nginx

I'd like to configure several paths within a site (such as /foo/ and /bar/) in the same way. To avoid copy-pasting, I figured I should use a single location block, but the only way I found for doing that is to use a regex, such as:

location ~ ^/(foo|bar)/ {
    ...
}

Is this the best way or is there a better alternative?

like image 751
aditsu quit because SE is EVIL Avatar asked Dec 02 '13 04:12

aditsu quit because SE is EVIL


1 Answers

This would work, but I believe it works slower, because it involves a regex engine, another alternative you could create the config you want in a separate file and include them in each location so that it would be written once and could be edited all together, like this

location /foo {
    include foo.conf;
}
location /bar {
    include foo.conf;
}

Inside foo.conf you could write any config that's in a location scope
heres a random sample snippet:

root /foo/bar;
try_files $uri /index.html;
like image 153
Mohammad AbuShady Avatar answered Sep 29 '22 16:09

Mohammad AbuShady