0
0
Nginxdevops~10 mins

Why virtual hosting serves multiple domains in Nginx - Visual Breakdown

Choose your learning style9 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.