Regex match (~, ~*) in Nginx - Commands & Configuration
events {}
http {
server {
listen 8080;
# Case-sensitive regex match (~)
location ~ ^/images/.*\.jpg$ {
root /var/www/images;
# This matches URLs starting with /images/ and ending with .jpg exactly
}
# Case-insensitive regex match (~*)
location ~* ^/videos/.*\.mp4$ {
root /var/www/videos;
# This matches URLs starting with /videos/ and ending with .mp4 ignoring case
}
# Default location
location / {
root /var/www/html;
}
}
}
This nginx.conf file sets up a server listening on port 8080.
The location ~ block uses a case-sensitive regular expression to match URLs starting with /images/ and ending with .jpg. Requests matching this pattern serve files from /var/www/images.
The location ~* block uses a case-insensitive regular expression to match URLs starting with /videos/ and ending with .mp4. Requests matching this pattern serve files from /var/www/videos.
The default location / block serves all other requests from /var/www/html.
nginx -t
systemctl reload nginxcurl -I http://localhost:8080/images/sample.jpg
curl -I http://localhost:8080/videos/Sample.MP4
If you remember nothing else from this pattern, remember: the tilde (~) means case-sensitive regex match and tilde-star (~*) means case-insensitive regex match in nginx location blocks.
Practice
~ operator mean in an nginx location block?Solution
Step 1: Understand nginx regex operators
The~operator in nginx is used for regex matching that is case-sensitive.Step 2: Differentiate from
The~*~*operator is for case-insensitive regex matching, so it is different from~.Final Answer:
It performs a case-sensitive regular expression match. -> Option CQuick Check:
~= case-sensitive regex match [OK]
- Confusing ~ with ~* for case sensitivity
- Thinking ~ matches exact strings only
- Assuming ~ matches all requests
Solution
Step 1: Identify case-insensitive regex syntax
In nginx,~*is used for case-insensitive regex matching.Step 2: Check other options
~is case-sensitive,=is exact match, and no operator means prefix match.Final Answer:
location ~* /images/ { } -> Option AQuick Check:
Case-insensitive regex = ~* [OK]
- Using ~ instead of ~* for case-insensitive matching
- Confusing exact match (=) with regex
- Omitting regex operator for regex matching
location ~* \.jpg$ {
return 200 'Image file matched';
}What will be the response for a request to
/photos/Sunset.JPG?Solution
Step 1: Analyze the regex and operator
The~*operator means case-insensitive regex match. The regex\.jpg$matches strings ending with '.jpg' ignoring case.Step 2: Check the request URL
The request is/photos/Sunset.JPG, which ends with '.JPG' (uppercase). Case-insensitive match succeeds.Final Answer:
200 with 'Image file matched' -> Option DQuick Check:
Case-insensitive regex matches .JPG [OK]
- Assuming regex is case-sensitive with ~*
- Ignoring the $ end anchor in regex
- Confusing response codes returned
location ~* /images/(.*\.png$ {
proxy_pass http://backend;
}Solution
Step 1: Check regex syntax
The regex/images/(.*\.png$has an opening parenthesis but no closing parenthesis.Step 2: Validate other parts
The~*operator is valid for case-insensitive match, proxy_pass URL looks valid, and curly braces are present.Final Answer:
Missing closing parenthesis in regex -> Option AQuick Check:
Regex parentheses must be balanced [OK]
- Forgetting to close parentheses in regex
- Confusing ~ and ~* usage
- Ignoring syntax errors in regex patterns
.css or .CSS using a regex match in nginx. Which location block correctly matches both cases efficiently?Solution
Step 1: Understand case-insensitive matching
Using~*makes the regex case-insensitive, so it matches both '.css' and '.CSS'.Step 2: Evaluate other options
location ~ \.css$ { } uses case-sensitive~, so it misses '.CSS'. Options C and D have incorrect regex syntax with unescaped pipes and redundant patterns.Final Answer:
location ~* \.css$ { } -> Option BQuick Check:
Use ~* for simple case-insensitive regex [OK]
- Using ~ and missing uppercase matches
- Trying to match cases with complex regex instead of ~*
- Incorrect escaping of regex special characters
Ready to prove it?
Pass the test and this pattern moves unknown
