0
0
Nginxdevops~20 mins

Wildcard and regex server names in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Wildcard and Regex Server Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Nginx server block matching with wildcard
Given the following nginx server block, what hostname will it match?
Nginx
server {
    server_name *.example.com;
    listen 80;
}
Aexample.com
Bapi.example.com
Csub.api.example.com
Dexample.net
Attempts:
2 left
💡 Hint

Remember that *.example.com matches one subdomain level only.

💻 Command Output
intermediate
2:00remaining
Regex server_name matching in nginx
What hostname will this nginx server block match?
Nginx
server {
    server_name ~^www\.(.+)\.example\.com$;
    listen 80;
}
Awww.api.example.com
Bwww.example.com
Capi.example.com
Dwww.api.example.net
Attempts:
2 left
💡 Hint

The regex matches hostnames starting with www. and ending with .example.com.

Configuration
advanced
2:00remaining
Correct nginx server_name for multiple subdomains
Which server_name directive correctly matches any subdomain of example.com, including multiple levels like sub.api.example.com?
Aserver_name ~^www\.(.+)\.example\.com$;
Bserver_name *.example.com;
Cserver_name example.com *.example.com;
Dserver_name ~^(.+\.)?example\.com$;
Attempts:
2 left
💡 Hint

Think about how to match zero or more subdomain levels with regex.

Troubleshoot
advanced
2:00remaining
Why does this nginx server block not match the hostname?
You configured this server block: server { server_name *.example.com; listen 80; } But requests to example.com are not handled by this block. Why?
AThe server_name directive requires quotes around the wildcard
BThe listen directive is missing the IP address
CThe wildcard does not match the root domain without subdomain
DThe server block must include a regex to match example.com
Attempts:
2 left
💡 Hint

Think about what *.example.com matches.

Best Practice
expert
2:00remaining
Choosing server_name order for overlapping patterns
You have these server blocks: 1) server_name example.com; 2) server_name *.example.com; 3) server_name ~^www\.(.+)\.example\.com$; Which order should nginx evaluate these to ensure the most specific matches are used first?
AExact match first, then wildcard, then regex
BRegex server_name first, then wildcard, then exact match
CExact match first, then regex, then wildcard
DWildcard first, then exact match, then regex
Attempts:
2 left
💡 Hint

Exact matches have highest priority, regex lowest.