Imagine you have one apartment building but many tenants. How does virtual hosting in nginx let one server handle many websites (domains) like tenants in that building?
Think about how a receptionist knows which tenant to call by reading the visitor's name on the request.
Virtual hosting uses the Host header sent by the browser to decide which website to show. This way, one server can serve many domains by checking the requested domain name.
Given this nginx server block configuration snippet, what will nginx do when a request comes for example.com?
server {
listen 80;
server_name example.com;
root /var/www/example;
}Check how server_name matches the Host header.
The server block listens on port 80 and serves files from /var/www/example only when the Host header matches example.com.
You configured two server blocks for site1.com and site2.com. But when you visit site2.com, nginx shows site1.com content. What is the most likely cause?
server {
listen 80;
server_name site1.com;
root /var/www/site1;
}
server {
listen 80;
server_name site2.com;
root /var/www/site2;
}Check if nginx matches the Host header correctly.
If the server_name for site2.com is wrong or missing, nginx will use the default server block, often the first one, causing site1.com content to show.
Put these steps in the correct order to add a new domain mynewsite.com to an nginx server using virtual hosting.
Think about preparing files before telling nginx about them.
First create the directory, then upload files, add the server block, and finally reload nginx to apply changes.
When hosting multiple domains on one nginx server, which practice best improves security and isolation?
Think about limiting damage if one site is compromised.
Using separate users and directories limits access and damage scope, improving security for each domain.