nginx

Location block and rewriting URLs

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

Location block and rewriting URLs

URI - the part of URL that comes after the domain (or IP/Port) name. http://curbsidr.com/guests/35/services/34/locations/33

/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

/guests/35

location ~ /guests/35 will match

/guests/35

/guests/35/services/34/locations/33

but will NOT match

/Guests/35/services/34/locations/33

location ~ /guests/35 will match

/guests/35

/guests/35/services/34/locations/33

/Guests/35/services/34/locations/33

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!

Subscribe to The infinite monkey theorem

Get the latest posts delivered right to your inbox