How do you configure virtual hosts (server blocks) in Nginx?
Learn how to configure Nginx virtual hosts using server blocks: listen, server_name, roots, default_server, and the sites-available workflow with examples.
Expected Interview Answer
Virtual hosts in Nginx are defined with server blocks, where each server { } block uses the listen and server_name directives to decide which requests it answers, letting one Nginx instance serve many domains from a single IP.
When a request arrives, Nginx first matches the listen address and port, then compares the Host header against server_name to select the block; if nothing matches it falls back to the default_server. Each block typically sets its own root, index, access_log, and location routes. Convention is one file per site under /etc/nginx/sites-available, symlinked into sites-enabled (or conf.d), so sites can be enabled and disabled independently.
- Hosts many domains on one server and IP
- Isolates configuration, logs, and roots per site
- Enables and disables sites independently
- Supports name-based and port-based routing
- Provides a default_server for unmatched hosts
AI Mentor Explanation
A single stadium hosts several different matches on different pitches on the same day. The gate staff read each ticket's match name and send that spectator to the correct pitch, seating, and scoreboard. A server block is one pitch's setup: Nginx reads the Host header like a ticket's match name and routes the request to the block whose server_name matches, each with its own boundary and scorers.
Step-by-Step Explanation
Step 1
Create a site config file
Add a file such as /etc/nginx/sites-available/example.com.conf containing one server { } block per site.
Step 2
Set listen and server_name
Use listen 80; (or 443 ssl) and server_name example.com www.example.com; so Nginx knows which requests this block answers.
Step 3
Define root and index
Point root to the site's document directory and set index index.html; then add location blocks for routing.
Step 4
Enable the site
Symlink the file into /etc/nginx/sites-enabled (or place it under conf.d) so Nginx loads it on start.
Step 5
Test and reload
Run nginx -t to validate syntax, then systemctl reload nginx (or nginx -s reload) to apply without downtime.
What Interviewer Expects
- Knows a virtual host is a server block
- Explains listen plus server_name matching
- Understands name-based vs port-based hosting
- Mentions default_server for unmatched hosts
- Describes sites-available/sites-enabled workflow
- Knows to run nginx -t before reload
Common Mistakes
- Confusing server_name with the listen address
- Forgetting to symlink into sites-enabled
- Assuming Nginx auto-reloads config changes
- Omitting a default_server so unmatched hosts hit a random block
- Reloading without running nginx -t first
Best Answer (HR Friendly)
“Virtual hosts, called server blocks in Nginx, let one server handle several websites at once. Each block says which domain and port it answers and where its files live, so Nginx sends every visitor to the right site.”
Code Example
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public;
index index.html;
access_log /var/log/nginx/example.access.log;
location / {
try_files $uri $uri/ =404;
}
}Follow-up Questions
- What is the difference between name-based and IP-based virtual hosting?
- How does Nginx choose the default_server when no server_name matches?
- How do you serve HTTPS from a server block?
- What is the purpose of nginx -t?
- How do sites-available and sites-enabled differ?
MCQ Practice
1. Which directive determines which domain a server block answers?
server_name is matched against the request's Host header to select the correct server block.
2. What happens if a request's Host header matches no server_name?
Nginx routes unmatched hosts to the block marked default_server (or the first block on that listen address).
3. Which command validates Nginx configuration before reloading?
nginx -t tests the configuration syntax and reports errors before you reload.
Flash Cards
What is a virtual host in Nginx? — A server { } block that answers requests for a specific listen address and server_name, letting one server host many sites.
Which two directives select a server block? — listen (address and port) narrows candidates, then server_name is matched against the Host header.
What is default_server? — The block Nginx uses when no server_name matches the request's Host header.
How do you apply config changes safely? — Run nginx -t to validate, then reload with systemctl reload nginx or nginx -s reload.