What if one server could magically become many, each showing a different website?
Why virtual hosting serves multiple domains in Nginx - The Real Reasons
Imagine you have a single server and want to show different websites to visitors depending on the web address they type, like example.com or myblog.com.
Without virtual hosting, you'd need a separate server or IP address for each website.
Manually setting up one server per website is expensive and wastes resources.
It also makes managing many sites slow and confusing because you juggle multiple servers.
Virtual hosting lets one server handle many websites by checking the web address visitors use.
This way, the server sends the right website content without needing extra machines.
server {
listen 192.168.1.1:80;
server_name example.com;
root /var/www/example;
}
server {
listen 192.168.1.2:80;
server_name myblog.com;
root /var/www/myblog;
}server {
listen 80;
server_name example.com myblog.com;
root /var/www/sites;
location / {
if ($host = 'example.com') {
root /var/www/example;
}
if ($host = 'myblog.com') {
root /var/www/myblog;
}
}
}Virtual hosting makes it easy and cheap to run many websites on one server, saving money and time.
A small business can host its main site, blog, and shop all on one server using virtual hosting, instead of paying for three separate servers.
Manual hosting needs one server per website, which is costly.
Virtual hosting lets one server serve many domains by checking the requested address.
This saves money, simplifies management, and uses resources efficiently.
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
