Nginx Server Blocks (Virtual Hosts)

Configure Nginx server blocks to host multiple websites on a single server. Covers name-based virtual hosting, default server handling, and SNI setup.

Routing

Detailed Explanation

Server blocks (the Nginx equivalent of Apache's Virtual Hosts) allow you to host multiple independent websites on a single Nginx instance, each with its own domain name, configuration, and document root.

Basic Server Block

Each server block defines a separate site with its own server_name and root directory:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name blog.example.com;
    root /var/www/blog/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Default Server

When a request arrives with a Host header that does not match any configured server_name, Nginx uses the default server. You can explicitly define one with the default_server parameter to control this behavior:

server {
    listen 80 default_server;
    server_name _;
    return 444;
}

The server_name _ is a catch-all convention that matches no real hostname. Returning status code 444 is an Nginx-specific response that closes the connection immediately without sending any data back, which is useful for blocking automated scanners that probe your server with random or missing Host headers.

File Organization

Best practice is to create separate configuration files for each site, organized into two directories:

/etc/nginx/
    nginx.conf
    sites-available/
        example.com.conf
        blog.example.com.conf
    sites-enabled/
        example.com.conf -> ../sites-available/example.com.conf

Use symbolic links from sites-enabled to sites-available to activate or deactivate sites without deleting their configurations. This makes it easy to temporarily disable a site during maintenance by simply removing the symlink.

SSL with Multiple Domains

Thanks to SNI (Server Name Indication), a TLS extension, Nginx can serve different SSL certificates for different domains hosted on the same IP address. Each server block specifies its own certificate and private key:

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/ssl/example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/example.com/privkey.pem;
}

Server Name Matching Priority

Nginx matches the server_name in a specific order: exact name match first, then the longest wildcard starting with an asterisk, then the longest wildcard ending with an asterisk, and finally the first matching regular expression. Understanding this priority prevents unexpected routing behavior when hosting many domains.

Use Case

You are hosting multiple websites or applications on a single server and need each domain to serve different content with independent SSL certificates and configurations.

Try It — Nginx Config Generator

Open full tool