Why virtual hosting serves multiple domains in Nginx - Performance Analysis
We want to understand how nginx handles multiple websites using virtual hosting.
Specifically, how the work grows when more domains are added.
Analyze the time complexity of this nginx virtual hosting setup.
server {
listen 80;
server_name example.com;
root /var/www/example;
}
server {
listen 80;
server_name example.org;
root /var/www/example_org;
}
server {
listen 80;
server_name example.net;
root /var/www/example_net;
}
This config serves three different domains by matching the requested domain name.
When a request comes, nginx checks each server block to find a matching domain.
- Primary operation: Comparing the requested domain to each server_name.
- How many times: Once per server block until a match is found or all checked.
As the number of domains (server blocks) increases, nginx checks more names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 comparisons |
| 100 | Up to 100 comparisons |
| 1000 | Up to 1000 comparisons |
Pattern observation: The work grows roughly in direct proportion to the number of domains.
Time Complexity: O(n)
This means the time to find the right domain grows linearly as more domains are added.
[X] Wrong: "nginx instantly knows the right domain no matter how many there are."
[OK] Correct: nginx checks domains one by one until it finds a match, so more domains mean more checks.
Understanding how nginx matches domains helps you explain real server behavior clearly and confidently.
"What if nginx used a hash map to find domains instead of checking each one? How would the time complexity change?"
Practice
Solution
Step 1: Understand virtual hosting concept
Virtual hosting allows one server to handle many websites by using domain names to select content.Step 2: Identify the main purpose
This means the server can serve different websites without needing separate servers for each domain.Final Answer:
To serve multiple websites from a single server using different domain names -> Option DQuick Check:
Virtual hosting = multiple domains on one server [OK]
- Thinking virtual hosting speeds up CPU
- Confusing virtual hosting with backups
- Believing it blocks visitors
Solution
Step 1: Recall nginx configuration directives
Theserver_namedirective tells nginx which domain names a server block should respond to.Step 2: Match directive to purpose
Other directives likerootset content folder,listensets port,locationdefines URL paths.Final Answer:
server_name -> Option AQuick Check:
Domain names use server_name [OK]
- Using root for domain names
- Confusing listen with domain matching
- Thinking location sets domain
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
}Solution
Step 1: Read server_name directive
The server_name lists both example.com and www.example.com, so nginx matches requests for either domain.Step 2: Understand nginx matching
Requests to either domain on port 80 will serve content from /var/www/example.Final Answer:
Both example.com and www.example.com -> Option CQuick Check:
server_name lists multiple domains = serve all listed [OK]
- Assuming only first domain is served
- Thinking root defines domain
- Believing nginx serves all domains by default
server_name but different root paths. What will happen when a request comes for that domain?Solution
Step 1: Understand nginx server block matching
When multiple server blocks have the same server_name, nginx uses the first matching block it finds in the config.Step 2: Result of duplicate server_name
It does not merge roots or error out; it simply picks the first block, so only one root is used.Final Answer:
nginx will serve content from the first server block it finds -> Option BQuick Check:
Duplicate server_name = first block wins [OK]
- Expecting nginx to merge content folders
- Thinking nginx throws error on duplicates
- Assuming random selection
site1.com and site2.com, on the same nginx server. Which configuration correctly uses virtual hosting to serve different content for each domain?Solution
Step 1: Check domain to root mapping
server { listen 80; server_name site1.com; root /var/www/site1; } server { listen 80; server_name site2.com; root /var/www/site2; } uses two server blocks, each with a unique server_name and root folder, correctly separating content by domain.Step 2: Analyze other options
server { listen 80; server_name site1.com site2.com; root /var/www/site1; } serves both domains from one root, so content is not separated. server { listen 80; server_name site1.com; root /var/www/site1; } server { listen 80; server_name site1.com; root /var/www/site2; } duplicates server_name causing conflict. server { listen 80; root /var/www/site1; } server { listen 80; root /var/www/site2; } lacks server_name, so domains won't be matched properly.Final Answer:
Two server blocks with distinct server_name and root for each domain -> Option AQuick Check:
Separate server blocks = separate domains and content [OK]
- Putting multiple domains in one block with one root
- Duplicating server_name in multiple blocks
- Omitting server_name directive
Ready to prove it?
Pass the test and this pattern moves unknown
