Mahdi.Kh
September 21, 2026
We get familiar with location modifiers and look at how to use them.
The location directive in NGINX is used to define how HTTP requests are processed based on the URI (not the full URL).
When Nginx receives a request, the full URL is first received from the client. Then only the URI part (that is, the path portion of the address) is extracted from it and compared against the location blocks to find the right match.
Together with the location directive, you can use a modifier that determines how it's matched against the URI; next, we'll go over each of the modifiers.
If we use = to match the URI, the match is done exactly. That is, the URI must be exactly equal to the value we specified:
Using ~, you can match the URI against a case-sensitive regular expression.
If we don't want our regular expression to be case-sensitive, we can do the match using *~.
Using parentheses creates a capture group, and you can access the value matched by that capture group using the $ sign together with a number indicating the capture group's index.
Following the example above, if you go to /age/123 (you need to put a number after age for the path to match), the value matched by the capture group is shown on the page.
If we want to match every path that starts with a specific prefix, we can use a prefix match. Prefix matching comes in two forms.
^~If we use ^~, a high-priority prefix match is performed. This means that if the URI starts with this prefix, this location block is chosen as the match right away.
There's another type of prefix match, done as follows:
No modifier at all is used in this type of match! This kind of match has the lowest priority.
The priority nginx uses to select blocks is as follows:
= (exact match)^~ (priority prefix)~ or *~) and picking the best matchFor more information, check out the documentation for the location directive. You can also access the examples we covered in this part on GitHub.
Previous part: Compressing Responses | Nginx from Scratch