Using regular expressions (regex) in the location directive of Nginx provides a powerful mechanism for defining flexible and dynamic URI patterns.
Regular expressions allow you to define complex patterns for matching URIs. This flexibility is useful when you need to capture various URI structures or formats.
It enables dynamic routing based on patterns within URIs. This is particularly valuable when handling dynamic content or when you want to route requests to different backend servers based on specific URI characteristics.
Instead of creating multiple location blocks for specific URI patterns, you can use a single location block with a regex pattern to handle a broader range of cases. This can lead to more concise and manageable configuration files.
The modifiers
In Nginx, modifiers are used with the location directive to specify how the location pattern should be matched. The three common modifiers are '~', '~*', and '='.
'~' modifier: Matches using case-sensitive regular expressions.
'~*' modifier: Matches using case-insensitive regular expressions.
'=' modifier: Matches an exact and case-sensitive string match.
Example
Exact Match:
- Syntax: =pattern
- Description: Performs an exact case-sensitive match. The URI must match the specified pattern exactly.
location = /favicon.ico {
# Matches only requests for /favicon.ico
}
Basic Match:
- Syntax: /pattern/
- Description: Matches the specified pattern in the URI. It's a case-sensitive match, and the pattern must appear in the URI exactly as specified.
location /images/ {
# Matches URIs that start with /images/
# e.g., /images/cat.jpg, /images/photos/
}
Case-Insensitive Match:
- Syntax ~*pattern
- Description: Performs a case-insensitive match. The ~* modifier is used to indicate case insensitivity.
location ~* /images/ {
# Matches URIs that start with /images/ regardless of case
# e.g., /images/cat.jpg, /IMAGES/photos/
}
Regular Expression Match:
- Syntax: ~pattern
- Description: Performs a case-sensitive regular expression match. The specified pattern is a regular expression that can include various constructs.
location ~ \.(jpg|jpeg|png|gif)$ {
# Matches URIs ending with .jpg, .jpeg, .png, or .gif
# e.g., /images/cat.jpg, /photos/image.png
}
Case-Insensitive Regular Expression Match:
- Syntax: ~*pattern
-Description: Performs a case-insensitive regular expression match. The ~* modifier is used to indicate case insensitivity.
location ~* \.(jpg|jpeg|png|gif)$ {
# Matches URIs ending with .jpg, .jpeg, .png, or .gif
# regardless of case
# e.g., /images/cat.jpg, /PHOTOS/image.PNG
}
Match Any Character:
- Syntax: . (dot)
- Description: Matches any single character.
location ~ /files/abc. {
# Matches URIs that contain /files/ followed by any character
# e.g., /files/abc.jpg, /files/abc1/
}
Match - Start and End:
- Syntax: ^pattern$
- Description: Anchors the pattern to the start and end of the URI. Matches only if the entire URI exactly matches the specified pattern.
location ~ ^/users/\d+$ {
# Matches URIs like /users/123 or /users/9876, ensuring numeric IDs
# Example: /users/456
# Does not match: /users/abc or /users/123/edit
}
These examples showcase the versatility of regular expressions in the location directive of Nginx, providing powerful tools for defining flexible and dynamic URI patterns based on your routing requirements.