0
0
Nginxdevops~10 mins

Wildcard and regex server names in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Wildcard and regex server names
Start: Receive HTTP request
Check server_name exact match
Yes
Serve matched server block
No
Check server_name wildcard match
Yes
Serve wildcard matched server block
No
Check server_name regex match
Yes
Serve regex matched server block
No
Serve default server block
Nginx checks server names in order: exact match, then wildcard, then regex, and serves the first matching server block.
Execution Sample
Nginx
server {
    server_name example.com *.example.com ~^www\d+\.example\.com$;
    root /var/www/html;
}
This config matches exact 'example.com', any subdomain with wildcard '*.example.com', and regex matching 'www' followed by digits.
Process Table
StepIncoming HostCheck TypeMatch ResultAction Taken
1example.comExact matchMatchedServe example.com server block
2blog.example.comExact matchNo matchCheck wildcard
3blog.example.comWildcard match (*.example.com)MatchedServe wildcard server block
4www12.example.comExact matchNo matchCheck wildcard
5www12.example.comWildcard match (*.example.com)MatchedServe wildcard server block
6www12.example.comRegex match (~^www\d+\.example\.com$)MatchedServe wildcard server block (regex lower priority)
7unknown.comExact matchNo matchCheck wildcard
8unknown.comWildcard matchNo matchCheck regex
9unknown.comRegex matchNo matchServe default server block
💡 Nginx serves the first matching server block based on exact, wildcard, then regex checks.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
Incoming HostN/Ablog.example.comblog.example.comwww12.example.comunknown.com
Match TypeN/AExact: NoWildcard: YesWildcard: YesNo match
Server Block ServedNoneNoneWildcard *.example.comWildcard *.example.comDefault
Key Moments - 3 Insights
Why does 'www12.example.com' match both wildcard and regex but serve the wildcard block?
Because nginx tests exact, then wildcard, then regex in order, with priorities exact > wildcard > regex. See execution_table rows 5 and 6.
What happens if no server_name matches the incoming host?
Nginx serves the default server block as shown in execution_table row 9.
Does '*.example.com' match 'example.com' itself?
No, wildcard '*.example.com' matches subdomains only, not the root domain. Exact match is needed for 'example.com' (row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does 'blog.example.com' get served and by which match type?
AStep 6, regex match
BStep 2, exact match
CStep 3, wildcard match
DStep 9, default server
💡 Hint
Check rows where Incoming Host is 'blog.example.com' and see the Match Result and Action Taken columns.
According to variable_tracker, what is the Server Block Served after step 6 for 'www12.example.com'?
ARegex ~^www\d+\.example\.com$
BWildcard *.example.com
CExact example.com
DDefault server block
💡 Hint
Look at the 'Server Block Served' row under 'After Step 6' column.
If we remove the regex server block, what would happen to 'www12.example.com' according to the execution flow?
AIt would be served by the wildcard server block
BIt would be served by the exact match server block
CIt would be served by the default server block
DIt would not be served at all
💡 Hint
Check execution_table rows 4 and 5 for 'www12.example.com' and note the regex match presence.
Concept Snapshot
Nginx server_name matching order:
1. Exact names (e.g., example.com)
2. Wildcards (*.example.com matches subdomains)
3. Regex (~^pattern$ matches complex names)
Nginx serves the best matching server block: exact > wildcard > regex.
Default server block serves unmatched hosts.
Full Transcript
This visual execution shows how nginx matches incoming HTTP request hostnames to server blocks using server_name directives. It first checks for exact matches, then wildcard matches, and finally regex matches. The example config includes an exact name 'example.com', a wildcard '*.example.com' for subdomains, and a regex matching 'www' followed by digits. The execution table traces requests like 'example.com', 'blog.example.com', 'www12.example.com', and 'unknown.com' through these checks, showing which server block serves each. Variables track the incoming host, match type, and served block at each step. Key moments clarify why wildcard matches override regex matches and what happens when no match is found. The quiz tests understanding of matching order and outcomes. The snapshot summarizes the matching priority and behavior for quick reference.