We will explain Nginx location regex examples in brief detail in this article.
Prerequisites
It is assumed in this article that Nginx is already installed on your system.
Nginx location directive syntax
The following syntax is available for the location directory in the Nginx configuration file:
...
}
The Nginx location block can be added in a server block or on another location with some predefined conditions. In the above syntax, the modifier is an optional parameter. The presence of modifiers in the location block allows Nginx to deal with a URL in a different order. Here, few modifiers are mentioned below with some examples:
1. Matching all request with Nginx location block
The ‘/’ is used for prefix location to match all requests. However, if no best matches are searched then, it will be used as a last result.
Example
In the following location block, it will match all requests:
}
2. Match directory using location block in Nginx
The following example will match any request in the location block that is starting with /images/. Consequently, searching for a more specific matched block against the requested URI. However, if Nginx does not search for a more specific match then, the location block will be selected directly.
Example
In the below given an example, Nginx will match any request in the location block that is starting with /images/.
}
3. Exactly match using ‘=’ (equal to) location modifier
The modifier ‘=’ equal sign is used when you exactly match the location block with a requested URI.
Example
Nginx first tries to match the most specific prefix locations. However, in the following example, the ‘=’ modifier in the location block will exactly match the requested prefix path and then stop searching for better matches.
...
}
In the above-mentioned location block example, the ‘=’ location modifier will exactly match against the URL mydomain.com/imgs. The URLs ‘mydomain/imgs/index.html’ or ‘mydomain/imgs/’ will not match the condition.
4. Case sensitive Regex match using Tilda (~) modifier
The tilda (~) modifier performs the case-sensitive regular expression match search against the requested URI and continuously searches for a better match.
Example
The ~ modifier in the following example performs the case-sensitive regex (regular expression) match in the location block:
...
}
5. Case insensitive match using tilda (~*) modifier
The tilde sign with an asterisk (~*) modifier matches any (case-insensitive) request in the next location block that ends with a specific file format such as file ending with png, gif,ico, jpeg, jpg, css, or js. However, any requests that send to the /imgs/ folder will be entertained by the previous location block.
Example
In the following location block, it will match all the case-insensitive file types.
...
}
6. Caret-Tilde Sign (^~) modifier for RegEx Match
The modifier caret-tilda (^~) is used to perform the case-sensitive regular expression match against the requested URL. Therefore, if the matching URI will be matched in the /imgs or /imgs/pico.png, it stops searching to find a better match.
...
}
Conclusion
We have discussed a few examples and important details about the Nginx location directive with regex. We have also mentioned the use of modifiers with examples in this article. I hope this article will help you to understand the main concept of the location blocks in NGINX. Thanks!