Bird
Raised Fist0
Nginxdevops~10 mins

Why virtual hosting serves multiple domains in Nginx - Visual Breakdown

Choose your learning style10 modes available
Process Flow - Why virtual hosting serves multiple domains
Client sends HTTP request
↓
Server receives request
↓
Check Host header in request
↓
Match Host to server block
↓
Serve domain A content
↓
Serve domain B content
↓
Serve default content if no match
↓
Send response to client
The server uses the Host header in the request to decide which domain's content to serve, enabling multiple domains on one server.
Execution Sample
Nginx
server {
  listen 80;
  server_name example.com;
  root /var/www/example;
}

server {
  listen 80;
  server_name test.com;
  root /var/www/test;
}
Two server blocks listen on port 80 and serve different content based on the requested domain name.
Process Table
StepClient Request HostServer Block MatchedContent ServedAction
1example.comserver_name example.com/var/www/example contentServe example.com site
2test.comserver_name test.com/var/www/test contentServe test.com site
3unknown.comNo matchDefault server contentServe default site
4example.comserver_name example.com/var/www/example contentServe example.com site
💡 Requests are matched by Host header; if no match, default server block serves content.
Status Tracker
VariableStartAfter 1After 2After 3After 4
Host headernoneexample.comtest.comunknown.comexample.com
Matched server blocknoneexample.com blocktest.com blockdefault blockexample.com block
Content servednone/var/www/example/var/www/testdefault content/var/www/example
Key Moments - 2 Insights
Why does the server use the Host header to decide which content to serve?
Because multiple domains share the same IP and port, the Host header tells the server which domain the client wants, as shown in execution_table rows 1 and 2.
What happens if the Host header does not match any server_name?
The server uses the default server block to serve content, as seen in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what content is served when the Host header is 'test.com'?
A/var/www/example content
B/var/www/test content
CDefault server content
DNo content served
💡 Hint
Check execution_table row 2 under 'Content Served'
At which step does the server serve the default content?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 under 'Content Served'
If a new domain 'newsite.com' is added with its own server block, what changes in the variable tracker after a request to 'newsite.com'?
AHost header changes to 'newsite.com' and matched server block changes accordingly
BHost header remains the same but content served changes
CNo change because server only serves example.com and test.com
DContent served becomes default for all requests
💡 Hint
Refer to variable_tracker rows for 'Host header' and 'Matched server block'
Concept Snapshot
Virtual hosting lets one server handle many domains.
It uses the Host header in HTTP requests to pick the right site.
Each domain has a server block with server_name and root.
If no match, default server block serves content.
This saves IP addresses and simplifies hosting multiple sites.
Full Transcript
Virtual hosting allows a single web server to serve multiple domain names by checking the Host header in each incoming HTTP request. The server compares this header to the server_name directives in its configuration. When a match is found, the server serves the content from the corresponding root directory. If no match is found, the default server block handles the request. This method enables efficient use of one IP address to host many websites.

Practice

(1/5)
1. What is the main purpose of virtual hosting in nginx?
easy
A. To create backups of website data automatically
B. To increase the server's CPU speed
C. To block unwanted visitors from accessing the server
D. To serve multiple websites from a single server using different domain names

Solution

  1. Step 1: Understand virtual hosting concept

    Virtual hosting allows one server to handle many websites by using domain names to select content.
  2. Step 2: Identify the main purpose

    This means the server can serve different websites without needing separate servers for each domain.
  3. Final Answer:

    To serve multiple websites from a single server using different domain names -> Option D
  4. Quick Check:

    Virtual hosting = multiple domains on one server [OK]
Hint: Virtual hosting means many sites, one server [OK]
Common Mistakes:
  • Thinking virtual hosting speeds up CPU
  • Confusing virtual hosting with backups
  • Believing it blocks visitors
2. Which nginx directive is used to specify the domain names for virtual hosting?
easy
A. server_name
B. location
C. listen
D. root

Solution

  1. Step 1: Recall nginx configuration directives

    The server_name directive tells nginx which domain names a server block should respond to.
  2. Step 2: Match directive to purpose

    Other directives like root set content folder, listen sets port, location defines URL paths.
  3. Final Answer:

    server_name -> Option A
  4. Quick Check:

    Domain names use server_name [OK]
Hint: Domain names go in server_name directive [OK]
Common Mistakes:
  • Using root for domain names
  • Confusing listen with domain matching
  • Thinking location sets domain
3. Given this nginx server block snippet, what domain will it serve?
server {
  listen 80;
  server_name example.com www.example.com;
  root /var/www/example;
}
medium
A. Only example.com
B. Only www.example.com
C. Both example.com and www.example.com
D. Any domain on port 80

Solution

  1. Step 1: Read server_name directive

    The server_name lists both example.com and www.example.com, so nginx matches requests for either domain.
  2. Step 2: Understand nginx matching

    Requests to either domain on port 80 will serve content from /var/www/example.
  3. Final Answer:

    Both example.com and www.example.com -> Option C
  4. Quick Check:

    server_name lists multiple domains = serve all listed [OK]
Hint: server_name can list many domains separated by spaces [OK]
Common Mistakes:
  • Assuming only first domain is served
  • Thinking root defines domain
  • Believing nginx serves all domains by default
4. You configured two server blocks with the same server_name but different root paths. What will happen when a request comes for that domain?
medium
A. nginx will merge both roots and serve combined content
B. nginx will serve content from the first server block it finds
C. nginx will return an error about duplicate server_name
D. nginx will randomly choose one root each time

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    nginx will serve content from the first server block it finds -> Option B
  4. Quick Check:

    Duplicate server_name = first block wins [OK]
Hint: Duplicate server_name uses first matching block [OK]
Common Mistakes:
  • Expecting nginx to merge content folders
  • Thinking nginx throws error on duplicates
  • Assuming random selection
5. You want to serve two domains, site1.com and site2.com, on the same nginx server. Which configuration correctly uses virtual hosting to serve different content for each domain?
hard
A. server { listen 80; server_name site1.com; root /var/www/site1; } server { listen 80; server_name site2.com; root /var/www/site2; }
B. server { listen 80; server_name site1.com site2.com; root /var/www/site1; }
C. server { listen 80; server_name site1.com; root /var/www/site1; } server { listen 80; server_name site1.com; root /var/www/site2; }
D. server { listen 80; root /var/www/site1; } server { listen 80; root /var/www/site2; }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Two server blocks with distinct server_name and root for each domain -> Option A
  4. Quick Check:

    Separate server blocks = separate domains and content [OK]
Hint: Use separate server blocks for each domain with server_name [OK]
Common Mistakes:
  • Putting multiple domains in one block with one root
  • Duplicating server_name in multiple blocks
  • Omitting server_name directive
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard