📖 Scenario: You are setting up a web server using Nginx. Your goal is to configure server blocks that respond to different website names. Some websites use exact names, some use wildcard patterns, and some use regular expressions to match multiple subdomains.
🎯 Goal: Build an Nginx configuration with three server blocks: one for an exact domain, one using a wildcard server name, and one using a regex server name.
📋 What You'll Learn
Create a server block with an exact server name example.com
Add a server block with a wildcard server name matching all subdomains of example.org
Add a server block with a regex server name matching any subdomain starting with test and ending with .net
Print the full Nginx configuration to verify the setup
💡 Why This Matters
🌍 Real World
Web servers often need to handle many domain names and subdomains. Using wildcard and regex server names in Nginx helps manage these efficiently.
💼 Career
Understanding Nginx server name configuration is essential for DevOps roles managing web infrastructure and deployments.
Progress0 / 4 steps
1
Create exact server name block
Create a server block with server_name example.com; and listen on port 80. Use root /var/www/example.com; inside the block.
Nginx
Hint
Use server { ... } block with listen 80; and server_name example.com;.
2
Add wildcard server name block
Add a new server block below the first one with server_name *.example.org; and listen on port 80. Use root /var/www/wildcard; inside this block.
Nginx
Hint
Use server_name *.example.org; to match all subdomains of example.org.
3
Add regex server name block
Add a third server block with server_name ~^test.*\.net$; to match any subdomain starting with test and ending with .net. Listen on port 80 and set root /var/www/regex;.
Nginx
Hint
Use server_name ~^test.*\.net$; for regex matching. Remember to escape the dot.
4
Print the full Nginx configuration
Print the full Nginx configuration by outputting the combined server blocks exactly as written.
Nginx
Hint
Use a print statement to output the full configuration exactly as written.
Practice
(1/5)
1. What does a wildcard server name like *.example.com match in nginx?
easy
A. Any domain ending with example.com, including example.com itself
B. Only the exact domain example.com
C. Any subdomain of example.com, like blog.example.com or shop.example.com
D. Only domains starting with a star character
Solution
Step 1: Understand wildcard usage in server names
A wildcard * before a domain matches any subdomain part, but not the root domain itself.
Step 2: Apply to *.example.com
This matches domains like blog.example.com or shop.example.com, but not example.com alone.
Final Answer:
Any subdomain of example.com, like blog.example.com or shop.example.com -> Option C
Quick Check:
Wildcard * matches subdomains only [OK]
Hint: Wildcard * matches subdomains, not the root domain [OK]
Common Mistakes:
Thinking wildcard matches the root domain
Confusing wildcard with regex syntax
Assuming wildcard matches any domain ending
2. Which of the following is the correct syntax to define a regex server name in nginx?
easy
A. server_name /www\d+\.example\.com/;
B. server_name *example.com;
C. server_name ^www\d+\.example\.com$;
D. server_name ~^www\d+\.example\.com$;
Solution
Step 1: Recall regex server name syntax in nginx
Regex server names start with a tilde ~ followed by the regex pattern.
Step 2: Check each option
server_name ~^www\d+\.example\.com$; uses ~^www\d+\.example\.com$ which is correct regex syntax. Others either miss the tilde or use invalid delimiters.
Final Answer:
server_name ~^www\d+\.example\.com$; -> Option D
Quick Check:
Regex server names start with ~ [OK]
Hint: Regex server names always start with ~ in nginx [OK]
Common Mistakes:
Omitting the ~ before regex pattern
Using * instead of ~ for regex
Using slashes / / as regex delimiters
3. Given this nginx config snippet:
server {
server_name ~^www\d+\.example\.com$;
return 200 'Matched regex server name';
}
What will be the response for a request to www12.example.com?
medium
A. Matched regex server name
B. 500 Internal Server Error
C. 404 Not Found
D. Default nginx welcome page
Solution
Step 1: Understand the regex pattern
The regex ^www\d+\.example\.com$ matches domains starting with 'www' followed by one or more digits, then '.example.com'.
Step 2: Check if 'www12.example.com' matches
'www12.example.com' matches the pattern because '12' are digits after 'www'.
Final Answer:
Matched regex server name -> Option A
Quick Check:
Regex matches www12.example.com [OK]
Hint: Check if domain fits regex pattern exactly [OK]
Common Mistakes:
Assuming regex matches partial strings
Ignoring start ^ and end $ anchors
Confusing regex with wildcard matching
4. You wrote this nginx config:
server {
server_name ~www\d+\.example\.com$;
return 200 'Hello';
}
Why is this regex server_name directive incorrect?
medium
A. Missing ^ anchor at start of regex
B. Regex server name must start with ~ and pattern must start with ^
C. Regex pattern missing delimiters like / /
D. Regex server name must start with ~ and pattern must be valid
Solution
Step 1: Check regex server_name syntax
Regex server names must start with ~ and the pattern should start with ^ to match from the beginning.
Step 2: Identify missing ^ anchor
The pattern www\d+\.example\.com$ lacks the ^ anchor, causing partial matches rather than full hostname matches.
Final Answer:
Regex server name must start with ~ and pattern must start with ^ -> Option B
Quick Check:
Regex patterns need ^ anchor at start [OK]
Hint: Regex patterns should start with ^ for nginx server_name [OK]
Common Mistakes:
Omitting ^ anchor in regex pattern
Thinking delimiters like / / are needed
Ignoring nginx error messages about regex
5. You want nginx to serve requests for any subdomain of example.com except www.example.com. Which server_name config achieves this?
hard
A. server_name ~^(?!www\.)[^.]+\.example\.com$;
B. server_name *.example.com www.example.com;
C. server_name ~^www\d+\.example\.com$;
D. server_name example.com *.example.com;
Solution
Step 1: Understand the requirement
We want to match any subdomain except 'www.example.com'. This requires a regex with negative lookahead.
Step 2: Analyze options
server_name ~^(?!www\.)[^.]+\.example\.com$; uses ~^(?!www\.)[^.]+\.example\.com$ which matches any subdomain not starting with 'www.'. Others either include 'www' or don't exclude it.
Final Answer:
server_name ~^(?!www\.)[^.]+\.example\.com$; -> Option A
Quick Check:
Negative lookahead excludes www subdomain [OK]
Hint: Use negative lookahead in regex to exclude specific subdomains [OK]
Common Mistakes:
Using wildcard that includes www.example.com
Not using regex for exclusion
Confusing wildcard and regex syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.