Web / NGINX Interview questions
Why should location block matching order matter between regex and prefix matches?
NGINX doesn't simply match location blocks in the order they appear in the config file - it applies a specific precedence, and misunderstanding that precedence is a common source of "why isn't my location block being used" bugs.
The evaluation order is: exact match (location = /path) first, then the longest matching prefix (location /path/), unless a regex location is set to override it. Regular expression locations (location ~ pattern or location ~* pattern) are checked next, in the order they're written in the file, and the first matching regex wins - stopping further regex checks even if a later regex would also match.
This means a regex location appearing later in the file can still take priority over a prefix match, and among multiple regex locations, file order (not specificity) decides the winner. Getting this wrong commonly causes an intended catch-all regex to shadow a more specific one simply because of the order it was written in, or a prefix match to unexpectedly lose to an unrelated regex block.
More Related questions...