nginx location Matcher

Paste your location blocks and a request URI to see which one nginx actually picks, with the full decision trail.

Result

location ~ ^/api/v[0-9]+/users {

Why:Regexes are tried in config order; this is the first one that matched

L17

Decision trail

  1. 1Exactlocation = / {no match
  2. 2Prefixlocation / {longest
  3. 3Prefixlocation ^~ /static/ {no match
  4. 4Prefixlocation /api/ {longest
  5. 5Regexlocation ~* \.(jpg|png|gif)$ {no match
  6. 6Regexlocation ~ ^/api/v[0-9]+/users {matched

The order is fixed: all exact matches first, then the longest prefix; a ^~ prefix ends it there; otherwise regexes are tried in config order and the first hit wins; only if none match does it fall back to the longest prefix.

The classic trap: with both `location /api/` and `location ~ ^/api/v[0-9]+`, the regex wins even though the prefix looks more specific — regex beats plain prefix. Add ^~ if you want the prefix to win.

Only single-level locations are simulated. Nested locations have their own rules (a regex inside an outer prefix beats the outer block itself) and deserve a separate look when you meet one.

Comments

Sign in to join the discussion

No comments yet — be the first to say something