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
- Exact match location = /uri? Use it and stop
- Find the longest matching prefix (plain and ^~), remember it
- Is that longest prefix marked ^~? Use it and stop
- Test regex locations (~ and ~*) top to bottom; first match wins
- No regex matched: use the remembered longest prefix
Modifiers and priority
| Modifier | Type | Priority | Example |
|---|---|---|---|
| = | Exact | Highest, ends search | location = /health |
| ^~ | Prefix, blocks regex | High if longest prefix | location ^~ /static/ |
| ~ | Regex, case-sensitive | File order, beats plain prefix | location ~ \.php$ |
| ~* | Regex, case-insensitive | File order, beats plain prefix | location ~* \.(jpg|png)$ |
| (none) | Prefix | Lowest, longest wins | location /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"; }| Request | Winner | Why |
|---|---|---|
| / | A | Exact match ends the search |
| /about | B | Only / prefix matches, no regex matches |
| /images/cat.png | E | Longest prefix is C, but C is plain so regex E is tried and wins |
| /images/readme.txt | C | No regex matches, longest prefix C is used |
| /static/logo.png | D | Longest 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-LocCommon 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.