Skip to content

Nginx server blocks — virtual hosts the way Debian organises them

How to lay out multiple Nginx sites with sites-available and sites-enabled, scope server_name correctly, and keep the default_server trap from biting the next operator.

A “server block” is Nginx’s word for a virtual host — a config stanza that says which incoming requests this block answers. Most hosts run more than one site, and a clean layout decides whether the next operator can reason about the config in two minutes or has to spelunk through eight include files. This article shows the Debian-style layout we use on Ubuntu hosts and the matching rules that decide which block wins.

How to verify

See what is currently enabled and which name each block claims:

ls -la /etc/nginx/sites-enabled/
sudo nginx -T 2>/dev/null | grep -E 'server_name|listen '
sudo nginx -t
curl -H 'Host: example.com' -I http://127.0.0.1/

The nginx -T dump is the rendered config — the union of nginx.conf and every included file. If server_name is missing from a block, Nginx treats it as "" and only the IP-based fallback can hit it.

What’s happening

When a request lands, Nginx picks a server block in two phases. First, it filters by the IP and port the listener bound to (the listen directive). Among the blocks that match the listener, it then picks by server_name: an exact match wins; if none, the longest leading wildcard (*.example.com); if none, the longest trailing wildcard (example.*); if none, the first regex match; and finally the block flagged default_server for that listener. If no block is flagged, the first block parsed wins by accident — which is the source of half the “wrong site loaded” tickets.

The Debian package layout splits the config: actual blocks live in /etc/nginx/sites-available/ as files, and a symlink in /etc/nginx/sites-enabled/ controls which are loaded. This lets you stage a new site (drop the file in available) and only flip the symlink when you reload.

The procedure

  1. Write the block under sites-available:
    sudo install -d -m 0755 /var/www/example.com/html
    sudo tee /etc/nginx/sites-available/example.com.conf >/dev/null <<'EOF'
    server {
        listen 80;
        listen [::]:80;
    
        server_name example.com www.example.com;
        root /var/www/example.com/html;
        index index.html;
    
        access_log /var/log/nginx/example.com.access.log;
        error_log  /var/log/nginx/example.com.error.log;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    EOF
  2. Enable it with a symlink (do not cp — that defeats the layout):
    sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
  3. Decide which block answers unmatched Host headers. Exactly one block per listener should carry default_server:
    listen 80 default_server;
    listen [::]:80 default_server;
    For hardened deployments, make that default a deliberate 444-closer rather than your real site, so unmatched probes get nothing.
  4. Test and reload:
    sudo nginx -t && sudo systemctl reload nginx
  5. Verify name matching with curl:
    curl -H 'Host: example.com' -I http://127.0.0.1/
    curl -H 'Host: wrong.example' -I http://127.0.0.1/
    The first should be a 200 (or your real status), the second should hit your default block.
  6. When you add a server_name, also update DNS — dig +short example.com should resolve to the server IP, or the curl -H test will succeed locally while the real world still sees the old box.

Common pitfalls

  • Multiple blocks both flagged default_server on the same listener — Nginx warns but only the first wins, and that “first” depends on filename sort order.
  • Forgetting listen [::]:80 on IPv6-reachable hosts; the site works on v4 and silently 404s on v6.
  • server_name _; is a common idiom for “catch-all” but it is just a non-matching name relying on default_server to be set. Be explicit about which block is the default.
  • Putting site configs inside nginx.conf directly. It works but breaks the Debian layout — sudo apt upgrade may touch nginx.conf and someone has to merge.
  • Wildcard certs and server_name *.example.com both work, but the cert covers only one level of subdomain — foo.bar.example.com will mismatch.

In the engagements we run, server-block sprawl is the most common reason a host’s config “drifted”. A single source-of-truth template, generated from variables, keeps every site looking the same — see Nginx config test and reload for the safe-reload pattern we wrap around any change. Stack Harbor operates this as part of managed operations for clients who run more than a handful of sites on one box.