/ nginx

Nginx location block and rewriting URLs

URI - the part of URL that comes after the domain (or IP/Port) name.

/guests/35/services/34/locations/33 is the URI

location block allows 4 optional modifiers and a string to match against the URI:

= URI exactly matches location given

~ case sensitive RegEx match

~* case insensitive RegEx match

^~ optional RegEx match (RegEx is evaluated only if a regular match did not occur)

If no modifier is present, the match is interpreted as a prefix match.

location = /guests/35 will only match


location ~ /guests/35 will match


but will NOT match

location ~ /guests/35 will match

and so on

the tricky part is the order in which multiple location blocks are evaluated. In general the longest most specific match wins but there are exceptions:

1)** "no modifier"** or = gives an exact match done
2) else find the longest matching prefix
3) the longest matching prefix has ^~ modifier done
4) evaluate regular expressions, first match done
5) no match, use previously stored longest matching prefix

So the order in which location block are positioned matters!