server_name directive in Nginx - Time & Space Complexity
We want to understand the time complexity of matching a server name in nginx.
How does nginx check the server_name directive when many names are configured?
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
server_name example.com www.example.com api.example.com;
location / {
proxy_pass http://backend;
}
}
This snippet defines a server block with three server names that nginx will match against incoming requests.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx uses a hash table to look up the requested hostname against the configured server_names.
- How many times: Constant time operation regardless of list size.
As the number of server names grows, nginx performs a constant-time hash lookup.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~1 hash lookup |
| 100 | ~1 hash lookup |
| 1000 | ~1 hash lookup |
Pattern observation: The number of operations is constant regardless of the number of server names.
Time Complexity: O(1)
This means the time to find a matching server name is constant, independent of how many names are listed, due to hashing.
[X] Wrong: "nginx checks each server_name one by one, so more names mean more time."
[OK] Correct: nginx uses a hash table, enabling constant-time lookups.
Understanding how nginx matches server names helps you explain how web servers handle requests efficiently.
"What if nginx used a linear list to store server names without hashing? How would the time complexity change?"
Practice
server_name directive in an nginx configuration?Solution
Step 1: Understand the role of
Theserver_nameserver_namedirective tells nginx which domain names it should respond to when a request arrives.Step 2: Compare with other directives
Other directives likelistenset ports,rootsets file paths, and SSL directives configure certificates, not domain names.Final Answer:
To specify which domain names the server should respond to -> Option BQuick Check:
server_name = domain names [OK]
- Confusing server_name with listen port
- Thinking server_name sets file paths
- Mixing server_name with SSL settings
server_name directive?Solution
Step 1: Recall nginx syntax for server_name
The correct syntax lists domain names separated by spaces, ending with a semicolon.Step 2: Analyze each option
server_name example.com www.example.com; correctly lists domains separated by spaces and ends with a semicolon. Options B and C use invalid characters (=, :, commas). server_name example.com; www.example.com; incorrectly splits domains into two statements.Final Answer:
server_name example.com www.example.com; -> Option AQuick Check:
Multiple domains separated by spaces [OK]
- Using commas between domain names
- Adding equal signs or colons
- Splitting domains into multiple directives
server {
listen 80;
server_name *.example.com example.org;
}Which domain will nginx respond to?
Solution
Step 1: Understand the wildcard in server_name
The*.example.commatches any subdomain likewww.example.comorblog.example.com, but notexample.comitself.Step 2: Check the other domain
The directive also includesexample.org, so nginx responds to that exact domain as well.Final Answer:
Both example.org and any subdomain of example.com -> Option CQuick Check:
wildcard + exact domain = both matched [OK]
- Thinking wildcard matches main domain without subdomain
- Assuming wildcard matches all .com domains
- Ignoring exact domain entries
server {
listen 80;
server_name www.example.com, example.org;
}Why does nginx fail to start?
Solution
Step 1: Identify syntax error in server_name
The server_name directive expects domain names separated by spaces, not commas.Step 2: Check other parts of config
The listen directive correctly specifies port 80. server_name is valid in server block, and semicolon is present.Final Answer:
Because commas are not allowed between domain names -> Option DQuick Check:
No commas allowed in server_name list [OK]
- Using commas between domains
- Placing server_name inside location block
- Omitting semicolon at end
example.com, www.example.com, and any subdomain like blog.example.com. Which server_name directive is correct?Solution
Step 1: Understand domain matching with wildcards
The wildcard*.example.commatches subdomains likeblog.example.combut does NOT match the root domainexample.com.Step 2: Include all required domains explicitly
To coverexample.comandwww.example.com, they must be listed explicitly along with the wildcard.Final Answer:
server_name example.com www.example.com *.example.com; -> Option AQuick Check:
Wildcard excludes root domain, list all needed domains [OK]
- Assuming wildcard covers root domain
- Using invalid wildcard syntax like example.com*
- Omitting www or root domain
Ready to prove it?
Pass the test and this pattern moves unknown
