Bird
Raised Fist0
Nginxdevops~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the ~ operator mean in an nginx location block?
easy
A. It matches any request regardless of URL.
B. It performs a case-insensitive regular expression match.
C. It performs a case-sensitive regular expression match.
D. It matches the exact string only.

Solution

  1. Step 1: Understand nginx regex operators

    The ~ operator in nginx is used for regex matching that is case-sensitive.
  2. Step 2: Differentiate from ~*

    The ~* operator is for case-insensitive regex matching, so it is different from ~.
  3. Final Answer:

    It performs a case-sensitive regular expression match. -> Option C
  4. Quick Check:

    ~ = case-sensitive regex match [OK]
Hint: Remember: ~ is case-sensitive, ~* is case-insensitive [OK]
Common Mistakes:
  • Confusing ~ with ~* for case sensitivity
  • Thinking ~ matches exact strings only
  • Assuming ~ matches all requests
2. Which of the following is the correct syntax to match URLs case-insensitively using regex in nginx?
easy
A. location ~* /images/ { }
B. location = /images/ { }
C. location ~ /images/ { }
D. location /images/ { }

Solution

  1. Step 1: Identify case-insensitive regex syntax

    In nginx, ~* is used for case-insensitive regex matching.
  2. Step 2: Check other options

    ~ is case-sensitive, = is exact match, and no operator means prefix match.
  3. Final Answer:

    location ~* /images/ { } -> Option A
  4. Quick Check:

    Case-insensitive regex = ~* [OK]
Hint: Use ~* for case-insensitive regex in nginx location [OK]
Common Mistakes:
  • Using ~ instead of ~* for case-insensitive matching
  • Confusing exact match (=) with regex
  • Omitting regex operator for regex matching
3. Given this nginx config snippet:
location ~* \.jpg$ {
  return 200 'Image file matched';
}

What will be the response for a request to /photos/Sunset.JPG?
medium
A. 404 Not Found
B. 500 Internal Server Error
C. 200 with empty body
D. 200 with 'Image file matched'

Solution

  1. Step 1: Analyze the regex and operator

    The ~* operator means case-insensitive regex match. The regex \.jpg$ matches strings ending with '.jpg' ignoring case.
  2. Step 2: Check the request URL

    The request is /photos/Sunset.JPG, which ends with '.JPG' (uppercase). Case-insensitive match succeeds.
  3. Final Answer:

    200 with 'Image file matched' -> Option D
  4. Quick Check:

    Case-insensitive regex matches .JPG [OK]
Hint: ~* ignores case, so .JPG matches \.jpg$ regex [OK]
Common Mistakes:
  • Assuming regex is case-sensitive with ~*
  • Ignoring the $ end anchor in regex
  • Confusing response codes returned
4. Identify the error in this nginx location block:
location ~* /images/(.*\.png$ {
  proxy_pass http://backend;
}
medium
A. Missing closing parenthesis in regex
B. Using ~* instead of ~ for case-sensitive match
C. proxy_pass URL is invalid
D. location block missing curly braces

Solution

  1. Step 1: Check regex syntax

    The regex /images/(.*\.png$ has an opening parenthesis but no closing parenthesis.
  2. Step 2: Validate other parts

    The ~* operator is valid for case-insensitive match, proxy_pass URL looks valid, and curly braces are present.
  3. Final Answer:

    Missing closing parenthesis in regex -> Option A
  4. Quick Check:

    Regex parentheses must be balanced [OK]
Hint: Count parentheses in regex to avoid syntax errors [OK]
Common Mistakes:
  • Forgetting to close parentheses in regex
  • Confusing ~ and ~* usage
  • Ignoring syntax errors in regex patterns
5. You want to serve all URLs ending with .css or .CSS using a regex match in nginx. Which location block correctly matches both cases efficiently?
hard
A. location ~ \.css$ { }
B. location ~* \.css$ { }
C. location ~ \.css$|\.CSS$ { }
D. location ~* \.css$|\.CSS$ { }

Solution

  1. Step 1: Understand case-insensitive matching

    Using ~* makes the regex case-insensitive, so it matches both '.css' and '.CSS'.
  2. 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.
  3. Final Answer:

    location ~* \.css$ { } -> Option B
  4. Quick Check:

    Use ~* for simple case-insensitive regex [OK]
Hint: Use ~* with simple regex to match case-insensitive extensions [OK]
Common Mistakes:
  • Using ~ and missing uppercase matches
  • Trying to match cases with complex regex instead of ~*
  • Incorrect escaping of regex special characters