NGINX location Matching Explained: Exact, Prefix, Regex and Priority Order

How NGINX chooses a location block: exact =, preferential ^~, regex ~ and ~*, and longest prefix. With worked examples and a step-by-step algorithm.

Published November 27, 2026

location blocks decide what happens to each URL: serve a file, proxy to an app, deny access, or redirect. The order NGINX checks them in is not the order they appear in the file, and that surprises almost everyone at least once. Learn the algorithm once and you will never have a location block "mysteriously ignored" again.

The concept

There are four kinds of location match. location = /path is an exact match. location ^~ /path is a prefix match that, if it is the longest prefix, stops the regex search. location ~ regex and location ~* regex are case-sensitive and case-insensitive regular expressions. A plain location /path is a normal prefix match.

NGINX first checks all prefix locations (including = and ^~) and remembers the longest match. An exact match ends the search immediately. If the longest prefix is a ^~ location, it is used and regexes are skipped. Otherwise NGINX tests regex locations in the order they appear in the file, and the first matching regex wins. If no regex matches, the remembered longest prefix is used.

The algorithm in five steps

  1. Exact match location = /uri? Use it and stop
  2. Find the longest matching prefix (plain and ^~), remember it
  3. Is that longest prefix marked ^~? Use it and stop
  4. Test regex locations (~ and ~*) top to bottom; first match wins
  5. No regex matched: use the remembered longest prefix
The location selection algorithm.

Modifiers and priority

ModifierTypePriorityExample
=ExactHighest, ends searchlocation = /health
^~Prefix, blocks regexHigh if longest prefixlocation ^~ /static/
~Regex, case-sensitiveFile order, beats plain prefixlocation ~ \.php$
~*Regex, case-insensitiveFile order, beats plain prefixlocation ~* \.(jpg|png)$
(none)PrefixLowest, longest winslocation /api/

Worked example

Given this configuration, here is which block handles each URL and why.

location = /            { return 200 "A: exact root"; }
location /              { return 200 "B: catch-all prefix"; }
location /images/       { return 200 "C: images prefix"; }
location ^~ /static/    { return 200 "D: static, no regex"; }
location ~* \.(png|jpg)$ { return 200 "E: image regex"; }
RequestWinnerWhy
/AExact match ends the search
/aboutBOnly / prefix matches, no regex matches
/images/cat.pngELongest prefix is C, but C is plain so regex E is tried and wins
/images/readme.txtCNo regex matches, longest prefix C is used
/static/logo.pngDLongest prefix D has ^~, regexes are skipped

Practical examples

Typical PHP site

Static assets cached, PHP to FPM, hidden files blocked.

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.(?!well-known) {
    deny all;
}

Fast health check endpoint

An exact match is checked first and does no file lookup.

location = /healthz {
    access_log off;
    return 200 "ok\n";
}

Debug which location matched

Add a temporary response header per location.

location /api/ { add_header X-Loc api always; proxy_pass http://app; }
# curl -sI https://example.com/api/users | grep X-Loc

Common mistakes

  • Expecting file order to matter for prefix locations. Only regex locations are checked in order.
  • A broad regex like ~ \.(js|css)$ stealing requests from a prefix location such as /admin/; use ^~ on the prefix.
  • Forgetting the trailing slash difference: location /api matches /api-docs too, location /api/ does not.
  • Unescaped dots in regexes (\. vs .), matching more than intended.

Key takeaways

  • Exact (=) beats everything; ^~ on the longest prefix blocks regexes.
  • Regexes are tested in file order and beat plain prefixes.
  • Plain prefixes are chosen by length, not position.
  • Use temporary headers or curl -I to confirm which block matched.

FAQ

Can locations be nested?

Yes, prefix locations can contain other locations, but keep nesting shallow; it is easy to create confusing inheritance.

What is a named location?

location @name is never matched by URLs. It is a target for try_files or error_page, such as try_files $uri @backend.

Is location matching done on the decoded URI?

Yes. NGINX normalises the URI (decodes %XX, resolves . and .., merges slashes) before matching, and the query string is never part of the match.

More Nginx articles