How to use nested Nginx location blocks (prefixes vs. regex directives)?

Solution 1:

Old question, but the issue is because the parent location is a regex location while the nested locations are prefix locations.

You may only define nested prefix locations when the parent location is also a prefix location:

location /a {
        location /a {
               # You can also skip this location and just write
               # your code directly under the parent location
        }
        location /a/b {
        ...
        }
}

When a parent location is defined by a regex, any nested locations must also be defined by regexes:

location ~ ^/(a|b) {
        location ~ ^/a {
        ...
        }
        location ~ ^/b {
        ...
        }
}

However, you may also define nested regex locations when the parent location is a prefix location:

location /a/b {
        location ~ /a {
        ...
        }
        location ~ /b {
        ...
        }
}