How NGINX Hosts Multiple Websites on One Server (Server Blocks Explained)

Host many websites on one NGINX server with server blocks. Learn how the Host header and server_name pick a site, default_server, and sites-available.

Published November 27, 2026

One small VPS with one IP address can comfortably host dozens of websites: your blog, a client's landing page, an API and a staging site. NGINX makes this possible with server blocks, its version of virtual hosts. This article explains exactly how NGINX decides which site answers each request, and how to set up new sites cleanly.

The concept

Every HTTP/1.1 request includes a Host header naming the site the browser wants, such as Host: blog.example.com. With HTTPS, the same name is also sent earlier, during the TLS handshake, in the SNI (Server Name Indication) extension. Several domains can point their DNS A records at the same IP, and NGINX uses the Host header (and SNI for certificates) to route each request to the right configuration.

Each site is a server { } block with a listen directive (IP and port) and a server_name directive (one or more hostnames). When a request arrives, NGINX first narrows down to server blocks listening on that IP and port, then compares the Host header against their server_name values. If nothing matches, the request goes to the default server for that port.

How one server hosts many sites

ClientHost: shop.example.comNGINX :443match server_nameblog.example.com/var/www/blogshop.example.comproxy → :3000api.example.comproxy → :8000default_serverreturn 444
One IP, one port, many sites: the Host header selects the server block.

How NGINX picks a server block

  1. Find server blocks whose listen matches the IP and port
  2. Exact server_name match (shop.example.com)
  3. Longest wildcard starting with * (*.example.com)
  4. Longest wildcard ending with * (mail.*)
  5. First matching regular expression (~^www\d+\.)
  6. Otherwise: the default_server for that listen socket
NGINX server_name matching order.

The sites-available / sites-enabled convention

Debian and Ubuntu packages keep one file per site in /etc/nginx/sites-available/ and enable a site by symlinking it into sites-enabled/, which nginx.conf includes. RHEL-family packages use /etc/nginx/conf.d/*.conf instead. Both work the same way: NGINX simply includes every file and assembles one big configuration.

/etc/nginx/
├── nginx.conf                  # includes the directories below
├── sites-available/
│   ├── blog.example.com
│   └── shop.example.com
├── sites-enabled/
│   ├── blog.example.com -> ../sites-available/blog.example.com
│   └── shop.example.com -> ../sites-available/shop.example.com
└── conf.d/                     # RHEL-style per-site or global snippets

Proving it with curl

curl -s -H 'Host: blog.example.com' http://203.0.113.10/ | head -1
<title>My Blog</title>
curl -s -H 'Host: shop.example.com' http://203.0.113.10/ | head -1
<title>Shop</title>
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: random.test' http://203.0.113.10/
444
Same IP, different Host header, different site. Unknown hosts hit the default server.

Practical examples

Two sites in two files

Each file is a complete server block.

# /etc/nginx/sites-available/blog.example.com
server {
    listen 80;
    server_name blog.example.com www.blog.example.com;
    root /var/www/blog/public;
    index index.html;
    access_log /var/log/nginx/blog.access.log;
}

# /etc/nginx/sites-available/shop.example.com
server {
    listen 80;
    server_name shop.example.com;
    location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; }
}

Enable a site

Symlink, test, reload.

sudo ln -s /etc/nginx/sites-available/blog.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

A catch-all default server

Reject requests for hostnames you do not serve, including scanners hitting the bare IP.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;     # close connection without a response
}

Common mistakes

  • No explicit default_server, so the first site alphabetically answers for every unknown hostname, including the raw IP.
  • Forgetting www.example.com in server_name, so the www version shows a different site.
  • Two server blocks with the same server_name and port; NGINX warns "conflicting server name" and ignores one.
  • Using the same access log for every site, which makes debugging harder.
  • Testing with the IP address in the browser and expecting a named site to appear.

Key takeaways

  • The Host header (and SNI for HTTPS) tells NGINX which site a request is for.
  • Each site is a server block with listen and server_name.
  • Exact names beat wildcards, which beat regexes; unmatched requests go to default_server.
  • Keep one file per site and always define an explicit catch-all.

FAQ

Do I need a separate IP for each HTTPS site?

No. SNI lets one IP serve many certificates. All modern browsers support it.

How many sites can one NGINX server host?

Hundreds or thousands; NGINX itself is rarely the limit. CPU, memory and the backends behind each site are.

What does server_name _ mean?

Nothing special: _ is just an invalid hostname that never matches, used by convention in catch-all blocks. The default_server flag is what makes it the default.

More Nginx articles