Nginx same config for multiple paths

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?

Upvotes: 11

Views: 9229

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

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;

Upvotes: 5

Related Questions