0
0
Nginxdevops~20 mins

Regex match (~, ~*) in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Nginx regex match case sensitivity
Given the following nginx location block, what type of requests will it match?
location ~* ".*\.jpg$" { return 200; }
ARequests for URLs ending with .jpg or .JPG (case insensitive)
BRequests for URLs ending with .jpg only (case sensitive)
CRequests for URLs containing .jpg anywhere in the path (case sensitive)
DRequests for URLs ending with .JPG only (case sensitive)
Attempts:
2 left
💡 Hint
The ~* operator means case insensitive regex match.
💻 Command Output
intermediate
2:00remaining
Nginx regex match case sensitivity difference
What is the difference in matching behavior between these two nginx location blocks?
location ~ ".*\.css$" { return 200; }
location ~* ".*\.css$" { return 200; }
AThe first matches only .css exactly in lowercase; the second matches .css in any case
BBoth match .css in any case
CThe first matches .css in any case; the second matches only lowercase .css
DBoth match only lowercase .css
Attempts:
2 left
💡 Hint
Check the difference between ~ and ~* operators.
Troubleshoot
advanced
2:00remaining
Why does this nginx regex location not match uppercase extensions?
You have this nginx config:
location ~ ".*\.png$" { return 200; }

You notice requests for image.PNG return 404. Why?
ABecause nginx does not support regex in location blocks
BBecause the regex is invalid and nginx ignores it
CBecause ~ is case sensitive and the regex only matches lowercase .png
DBecause the $ anchor is misplaced causing no matches
Attempts:
2 left
💡 Hint
Consider the difference between ~ and ~* in nginx.
Best Practice
advanced
2:00remaining
Choosing regex match operator for nginx location
Which nginx location regex operator is best to match URLs ending with .html or .HTML regardless of case?
AUse prefix match with ".html"
BUse ~ operator with regex ".*\.html$"
CUse = operator with ".*\.html$"
DUse ~* operator with regex ".*\.html$"
Attempts:
2 left
💡 Hint
Think about case sensitivity and regex matching.
🧠 Conceptual
expert
2:00remaining
Understanding nginx regex match precedence
Given these nginx location blocks:
location /images/ {
  return 404;
}
location ~* ".*\.jpg$" {
  return 200;
}

What happens when a request for /images/photo.JPG is made?
AThe prefix location matches first and returns 404 because prefix locations have higher priority
BThe regex location matches and returns 200 because regex locations have higher priority
CNginx returns 404 because regex locations are ignored when prefix matches exist
DNginx returns 200 because prefix locations are ignored when regex exists
Attempts:
2 left
💡 Hint
Nginx chooses regex locations over prefix if regex matches.