Sysadmin · Explorer · Interactive explainer

Nginx location match explorer

Paste a set of location blocks, try a request path, and see which block Nginx would choose, including exact-match checks, longest-prefix memory, regex order, and the ^~ short-circuit.

Location rules

Target search intent: nginx location match order, nginx location precedence, nginx regex location order, and nginx ^~ explained.

Use plain prefix lines like /images/, or start a line with =, ^~, ~, or ~*.

Why this page is useful

  • It shows the actual decision path instead of only restating the precedence rules.
  • It makes regex order visible, which is where many real config surprises come from.
  • It keeps the strongest literal prefix in memory so you can see when regex overrides it.
  • It models the common top-level location selection flow without making you mentally simulate Nginx.

Scope note: this explorer focuses on top-level location matching for a request path, not nested locations, rewrites, or filesystem lookups.

Read the launch note for this sysadmin explainer.

Winning location

Decision trace

Candidate scan

Matching order refresher

  1. Exact = matches win immediately.
  2. Nginx remembers the longest matching literal prefix.
  3. If that remembered prefix uses ^~, regex checks are skipped.
  4. Otherwise regex locations run in declaration order, and the first regex match wins.
  5. If no regex matches, Nginx falls back to the remembered literal prefix.

Common debugging traps

  • A shorter regex can beat a longer plain prefix unless the prefix uses ^~.
  • Regex order matters. The first matching regex wins, not the most specific regex.
  • Query strings are ignored here because Nginx location matching works on the URI path.
  • If you paste an invalid regex, the tool flags it instead of pretending it matched.