0
0
Nginxdevops~30 mins

Regex match (~, ~*) in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Print the full configuration exactly as written.