Nginx Exact and Prefix Location Matching
Choose a handler by URL path
After Nginx selects an HTTP virtual server, a location block tells it how to handle a matching request path. Use = for one exact path and an ordinary prefix for a family of paths. These examples assume a Linux HTTP build with the rewrite module, default URI normalization, no nested blocks or regex rules unless shown, and no server-level rewrite. Each snippet is a partial configuration inside a server block; return produces a short text marker rather than reading a file.
location = /health { return 200 "health\n"; }location /catalog/ { return 200 "catalog\n"; }location /catalog/sale/ { return 200 "sale\n"; }location / { return 200 "general\n"; }A request to /health returns health; /catalog/sale/boots returns sale; /catalog/book returns catalog; /about returns general. The exact match ends the search. With only prefixes, the longest matching string wins, regardless of declaration order. The selected location is not a directory by itself.
A slash changes the boundary
# Broad prefix: also matches /cataloguelocation /catalog { return 200 "broad\n"; }# Narrow prefix: matches /catalog/ and descendantslocation /catalog/ { return 200 "narrow\n"; }A prefix compares characters; it does not understand business categories or path segments. In the first example, /catalogue begins with /catalog. Use a trailing slash and an explicit exact /catalog rule if both the directory and its base URL should be handled deliberately. Do not expect every trailing-slash prefix to redirect automatically; certain upstream handler directives have a separate slash-redirect behavior.
The path is not the whole request target
/health?check=1 health/catalog/sale/item sale/catalogue general/catalog general/catalog//sale/item saleFor the conventional exact and prefix rules, Nginx uses a processed URI path, not the query string. In this lab the ordinary query is separate, and adjacent slashes are compressed by default. Normalization also handles percent-encoded characters and dot segments. Do not use a raw request target as a substitute for the path used by Nginx. Keep normalization defaults unless a carefully reviewed application contract requires a change.
Try it with a private fixture
curl --noproxy '*' --max-time 2 'http://127.0.0.1:18088/health?check=1'curl --noproxy '*' --max-time 2 'http://127.0.0.1:18088/catalog/sale/item'Expected text markers are health and sale. These are predictions, not captured output. Without a local lab, trace the same requests on paper. Next, learn how a regex can override a prefix.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Exact and prefix routing
Predict the result and explain why the selected route wins.
Which rule wins for /health?check=1 in the first example?
Which prefix wins for /catalog/sale/boots?
Why can location /catalog match /catalogue?
Which path matches /catalog/ but not = /catalog?
What does a location block establish by itself?