Fortunately, the order in which the individual directives are evaluated is very simple and logical:
- = (exact match), e.g. location = /path – if the URI matches exactly, the search ends.
- The longest matching prefix, e.g. location /path or location ^~ /path – NGINX remembers it. If it has the ^~ modifier, the search ends and regular expressions are not checked. A shorter prefix with ^~ stops nothing.
- ~ and ~* (case-sensitive and case-insensitive regular expressions), e.g. location ~* \.(jpg|png|bmp)$ – they are checked in the order in which they appear in the configuration, and the first match wins.
- If no regular expression matches, the longest prefix from step 2 is used.
You can try the order in an online tester for location directives.