Nginx decides which location block handles a request through a matching process that surprises many admins. Two location blocks can both match the same URL, and the one that wins is not always the one listed first or the one that looks most specific. This guide explains nginx location block priority in plain terms: the match types, the order Nginx checks them, and the mistakes that send requests to the wrong block.
Location matching matters because it decides which root directory, proxy target, or rule set serves a request. A wrong match can serve the wrong site files, bypass an access rule, or send an API request to a static file handler. Getting the order right is a precondition for everything else in the config.
The Four Location Match Types
These nginx location matching rules are the foundation of the whole system. The nginx location modifiers are the symbols in front of the path, such as = and ^~. Nginx offers four ways to write a location. An exact match uses = and matches only one URI, such as location = / for the homepage alone. A preferential prefix match uses ^~ and matches a path prefix while skipping regular expression checks. A plain prefix match has no modifier and matches any URI that starts with the given string. A regular expression match uses ~ for case-sensitive matching or ~* for case-insensitive matching.
Plain prefix matches are the workhorse of most configs. A block for /images/ handles every request under that path, and a block for / matches everything, which is why it works as a catch-all. Regular expressions are more flexible, since one pattern can match many paths, but they cost more to evaluate and they are harder to read at a glance. Use them where a pattern is genuinely needed, such as matching file extensions, and prefer prefix matches elsewhere.
Nginx Location Block Priority: The Order Nginx Checks Them
Understanding nginx request matching means learning this fixed order, not guessing from file order. Nginx checks locations in a fixed order. First, it looks for an exact match. If one exists, it wins immediately and no other block is considered. Second, it finds the longest matching prefix, whether plain or preferential. Third, if that longest prefix uses ^~, it wins immediately and regular expressions are skipped. Fourth, if the longest prefix is a plain prefix, Nginx tests the regular expressions in file order and uses the first one that matches. Fifth, if no regular expression matches, the longest plain prefix wins.
Two consequences follow from that order. File order only matters among regular expressions: the first regex in the file that matches wins, so put more specific regex patterns before general ones. File order does not matter among prefix matches: the longest prefix wins regardless of where it appears in the file, so moving a prefix block up or down changes nothing.
A concrete example makes this clear. With blocks for /, /images/, and a regex for image file extensions, a request for /images/logo.png matches both the prefix and the regex. The regex wins if it appears and matches, even though the prefix is longer in text. If the prefix used ^~, the prefix would win instead, because ^~ stops the regex check. That single modifier is the difference between static files being served from disk and being handed to a script handler.
Common Location Priority Mistakes
The most common mistake is assuming the first matching block wins. An admin adds a block for /api/ above a regex that also matches API paths, then wonders why API requests reach the wrong handler. The regex wins over the plain prefix, so the fix is to add ^~ to the prefix or to reorder the regex patterns, not to move the prefix block higher.
The second mistake is a regex that is broader than intended. A pattern meant for one file type can match paths in other sections of the site, silently changing how those requests are handled. Test regex patterns against real URLs from the access log before deploying, and keep them as narrow as the job allows.
The third mistake is forgetting that location blocks do not inherit every directive the way people expect. Some directives inherit from the server level, others do not, and a location that looks complete can behave differently because a setting was set at server level but not repeated inside the location. When a location behaves oddly, check inheritance for the specific directive before rewriting the match logic. Our server blocks guide shows a clean layout that keeps these interactions manageable, and the static site guide shows prefix locations in a simple working config.
How to Test Which Location Wins
You cannot see the chosen location in the access log directly, so test deliberately. Add a distinct response header or a distinct root directory per location during testing, then request a URL and check which one served it. Remove the test markers after you confirm the behavior.
Another practical test is to request the same URL before and after a change and compare the response headers and body. If a change to one location alters the response for a URL you did not intend to touch, a match overlap is the likely cause. The fresh-install 404 guide is a useful companion here, since many fresh-install 404s turn out to be a location or root mismatch rather than a missing file. For requests that should reach a backend, the reverse proxy setup tutorial shows how location blocks and proxy passes work together.
As a rule of thumb, keep the location layout simple: one exact match for the homepage if you need it, plain prefixes for each section, ^~ where a prefix must beat a regex, and a small number of narrow regex blocks ordered from specific to general. A config you can reason about at a glance is a config that stays correct when the next person edits it.