Sub topic 6
Server Blocks and Virtual Hosts
Prerequisites
Assume that Nginx is installed, the service is running, and you can edit files with sudo. You should also understand that DNS maps a domain name to an IP address and that HTTP clients send a Host header with the requested name.
A server block is Nginx’s configuration for one virtual host. A virtual host lets one server answer for several domain names. Nginx chooses a server block by examining the destination IP address, TCP port, and requested host name.
How Nginx chooses a server block
Nginx first finds blocks whose listen directive matches the local address and port. It then compares the request’s Host header with server_name.
| Configuration | Purpose |
|---|---|
| `listen 80;` | Accept HTTP traffic on port 80 |
| `server_name example.com;` | Match one host name |
| `server_name example.com www.example.com;` | Match several host names |
| `default_server` | Handle unmatched requests for that address and port |
| `root /var/www/example.com/html;` | Set the document root for the block |
Exact names have priority over wildcard names. If no name matches, Nginx uses the default server for that port. If no block is explicitly marked with default_server, the first matching block loaded for that address and port becomes the default.
Configure a site
On Debian and Ubuntu systems, distribution packages commonly use /etc/nginx/sites-available for site files and /etc/nginx/sites-enabled for enabled sites. The main Nginx configuration usually includes files from sites-enabled.
Create /etc/nginx/sites-available/example.com:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}The listen [::]:80 line enables IPv6 traffic. The server_name values must match the names users request. DNS must point both names to this server before clients can reach it by name.
Create the document root and enable the configuration:
sudo mkdir -p /var/www/example.com/html
printf '%s\n' '<h1>Example site</h1>' | sudo tee /var/www/example.com/html/index.html
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo nginx -t
sudo systemctl reload nginxtry_files checks whether the requested path exists below the root directory. The final =404 tells Nginx to return a 404 response instead of exposing an unexpected directory or falling through to another handler.
Use a default server
A default block is useful for requests sent to the IP address or to an unknown host name:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 404;
}The underscore is only a conventional name. The important setting is default_server.
Exercise
A request arrives on port 80 with Host: www.example.com. Two blocks listen on port 80: one has servername example.com www.example.com, and the other is defaultserver. Which block handles the request?
Show answer
The block listing example.com and www.example.com handles it because its server_name exactly matches the Host header. The default block handles only unmatched requests.