I'm having trouble understanding nested locations in nginx. I have two locations with some configuration directives in common, so rather than repeat the directives, I'd prefer to repeat the URI using a regular-expression:
location ~ /a|/b {
location /a {
}
location /b {
}
}
However, this gives the error
nginx: [emerg] location "/a" is outside location "/a|/b" in /etc/nginx/nginx.conf:36
Without the or |
it seems okay, though.
location ~ /a {
location /a {
}
}
What am I doing wrong? Is there a better way to do this without repeating anything? Can I group locations in another way?
Old question, but the issue is because the parent location is a regex location while the nested locations are prefix locations.
When a parent location is defined by a regex, any nested locations must also be defined by regexes:
You may only define nested prefix locations when the parent location is also a prefix location:
However, you may define nested regex locations when the parent location is a prefix location: