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
Using Regex Match (~, ~*) in Nginx Configuration
📖 Scenario: You are setting up an Nginx web server. You want to control access to certain URLs using regular expressions.Specifically, you want to allow access only to URLs that start with /images/ and end with .jpg or .png.
🎯 Goal: Build an Nginx configuration snippet that uses regex match operators ~ and ~* to match URLs case-sensitively and case-insensitively.You will create a location block that matches URLs ending with .jpg or .png case-sensitively, and another location block that matches URLs ending with .JPG or .PNG case-insensitively.
📋 What You'll Learn
Create a location block using ~ for case-sensitive regex matching
Create a location block using ~* for case-insensitive regex matching
Use regex to match URLs starting with /images/ and ending with .jpg or .png
Add a simple return 200 directive inside each location block to confirm matching
💡 Why This Matters
🌍 Real World
Web servers often need to route or restrict access based on URL patterns. Regex matching in Nginx helps achieve flexible and powerful URL control.
💼 Career
Understanding regex matching in Nginx is essential for DevOps engineers and system administrators managing web servers and application deployments.
Progress0 / 4 steps
1
Create the base Nginx server block
Write an Nginx server block with listen 80; and an empty body. Name the server block server {}.
Nginx
Hint
Start by writing server { and inside it add listen 80;.
2
Add a case-sensitive regex location block
Inside the server block, add a location block using the ~ operator to match URLs starting with /images/ and ending with .jpg or .png (case-sensitive). Inside it, add return 200;.
Nginx
Hint
Use location ~ ^/images/.*\.(jpg|png)$ to match URLs case-sensitively.
3
Add a case-insensitive regex location block
Below the previous location block, add another location block using the ~* operator to match URLs starting with /images/ and ending with .JPG or .PNG (case-insensitive). Inside it, add return 200;.
Nginx
Hint
Use location ~* ^/images/.*\.(jpg|png)$ for case-insensitive matching.
4
Test and output the final Nginx configuration
Print the entire Nginx configuration code you wrote so far.
Nginx
Hint
Print the full configuration exactly as written.
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
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 C
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
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 A
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
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 A
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
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 B
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 ~*