Bird
Raised Fist0
Nginxdevops~10 mins

Regex match (~, ~*) in Nginx - Step-by-Step Execution

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
Process Flow - Regex match (~, ~*)
Request URL Received
Check location blocks
Is location regex?
Match with ~ or ~*
If match found
Use this location
Process request
If no match
Try next location or default
Nginx checks request URLs against location blocks. If a location uses ~ or ~*, it tests the URL with regex (case-sensitive or insensitive). If matched, it uses that location.
Execution Sample
Nginx
location ~ ".*\.php$" {
    fastcgi_pass 127.0.0.1:9000;
}

location ~* ".*\.jpg$" {
    expires 30d;
}
Two location blocks: one matches URLs ending with .php (case-sensitive), the other matches URLs ending with .jpg (case-insensitive).
Process Table
StepRequest URLLocation TestedRegex TypeMatch ResultAction Taken
1/index.phplocation ~ ".*\.php$"Case-sensitive (~)MatchesUse this location, pass to fastcgi
2/image.JPGlocation ~ ".*\.php$"Case-sensitive (~)No matchCheck next location
3/image.JPGlocation ~* ".*\.jpg$"Case-insensitive (~*)MatchesUse this location, set expires
4/about.htmllocation ~ ".*\.php$"Case-sensitive (~)No matchCheck next location
5/about.htmllocation ~* ".*\.jpg$"Case-insensitive (~*)No matchNo regex location matched, try other locations or default
💡 Request URL matched or no regex location matched, nginx proceeds accordingly.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Request URL/index.php/image.JPG/image.JPG/about.html/about.html
Current Location Testedlocation ~ ".*\.php$"location ~ ".*\.php$"location ~* ".*\.jpg$"location ~ ".*\.php$"location ~* ".*\.jpg$"
Match ResultMatchesNo matchMatchesNo matchNo match
Action TakenUse this locationCheck next locationUse this locationCheck next locationNo regex location matched
Key Moments - 2 Insights
Why does location ~* ".*\.jpg$" match /image.JPG but location ~ ".*\.php$" does not match /image.JPG?
Because ~* means case-insensitive regex match, so .JPG matches .jpg ignoring case. The ~ operator is case-sensitive, so it does not match uppercase .JPG.
What happens if no regex location matches the request URL?
Nginx continues checking other non-regex locations or the default location block to handle the request, as shown in step 5 of the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result for /index.php against location ~ ".*\.php$"?
AMatches
BNo match
CError
DPartial match
💡 Hint
Check Step 1 in the execution_table under 'Match Result' column.
At which step does nginx use a case-insensitive regex match?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Regex Type' column in the execution_table for step 3.
If the request URL was /photo.JPG, which location block would match it?
Alocation ~ ".*\.php$"
Blocation ~* ".*\.jpg$"
CNo location matches
DBoth locations match
💡 Hint
Refer to how ~* matches case-insensitively in the variable_tracker and execution_table.
Concept Snapshot
Nginx regex match uses ~ for case-sensitive and ~* for case-insensitive matching.
Syntax: location ~ /pattern/ { ... } or location ~* /pattern/ { ... }
Nginx tests request URLs against these regex locations in order.
If matched, nginx uses that location block to process the request.
If no regex matches, nginx tries other locations or defaults.
Full Transcript
When nginx receives a request URL, it checks location blocks to find a match. Locations using ~ perform case-sensitive regex matching, while ~* performs case-insensitive matching. For example, location ~ ".*\.php$" matches URLs ending with .php exactly, while location ~* ".*\.jpg$" matches .jpg endings ignoring case. Nginx tests each regex location in order. If a match is found, it uses that location block to handle the request. If no regex location matches, nginx tries other locations or the default. This process ensures flexible URL matching based on regex patterns.

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