One Nginx server can host many websites. Each site gets its own server block with its own domain name, document root, and log files, while Nginx sorts incoming requests by the Host header and sends each one to the right block. This guide shows how to serve multiple domains from one Nginx server, from the first server block to certificates and the mistakes that send visitors to the wrong site.
To host multiple sites nginx admins usually start with server blocks and add isolation later. Hosting several sites on one server saves money and simplifies maintenance for small projects, client sites, and personal domains. The trade-off is shared fate: a config error or a resource hog on one site can affect the others, so keep the blocks clean and test each site after every change.
Multiple Domains From One Nginx Server: One Block per Domain
An nginx server blocks multiple domains setup starts with one block per domain. The core idea is simple. Create one server block per domain, each with a server_name for that domain and a root pointing at that site’s files. Nginx reads the Host header of each request, finds the block whose server name matches, and serves files from that block’s root. Two sites can share a server and even an IP address, because the domain name, not the IP, does the routing.
This nginx virtual hosts setup uses one file per site, which is the same idea older servers called virtual hosts. Keep each site in its own config file under /etc/nginx/sites-available/ and enable it with a symlink in /etc/nginx/sites-enabled/. One file per site makes it easy to disable a single site without touching the others, and it keeps a growing config readable. Our server blocks guide for Ubuntu walks through the file layout and the symlink step in detail.
What Happens When No Server Name Matches
Every Nginx server needs a default block for requests that match no server name. This happens with direct IP visits, with old domains that still point at the server, and with scanners that send a made-up Host header. Without a default block, Nginx serves the first server block in the file order, which is rarely the site you would choose.
Set one block as the explicit default with listen 80 default_server; and give it a harmless behavior, such as returning 444 to close the connection or serving a simple placeholder page. Being explicit about the default avoids the surprise of a visitor landing on a random client site because of file ordering. The same idea applies on the HTTPS side, where a default block needs a certificate even if it only returns an error, because the TLS handshake happens before Nginx can read the Host header.
Certificates for Multiple Domains
Each domain needs its own certificate, or a certificate that covers several domains. The simplest path is one certificate per domain, obtained with Certbot for each server block. Certbot can handle each domain separately, and renewal works per certificate, so adding a new site later does not disturb existing ones.
A wildcard certificate is the alternative when one owner runs many subdomains, such as app.example.com and api.example.com. Wildcards need DNS validation instead of HTTP validation, which is more setup work up front but one certificate then covers every subdomain. Our wildcard certificate guide covers the DNS challenge step, and the automatic renewal guide explains how to keep all of them renewing without manual attention.
When sites share a server, keep their log files separate too. One log file per site, named after the domain, makes it far easier to find a problem for one site without wading through traffic for all of them. The access and error log locations are set per server block, so this is a one-line choice at setup time that pays off on every later incident.
Isolating Sites From Each Other
Separate document roots are the minimum isolation: each site’s files live in its own directory, owned by its own user where possible, so one site cannot read another site’s config files or uploads. For PHP sites, run a separate PHP-FPM pool per site with its own user, so a compromised script on one site does not get the other site’s files for free. This step matters most on shared servers that host sites for different owners.
Resource isolation is the next layer. A single runaway site can fill the disk with logs or consume all PHP workers, starving the other sites. Set PHP-FPM pool limits per site, watch disk usage per site, and consider a separate Docker container per site when the isolation needs are stronger. Our guide to running multiple WordPress sites on one VPS with Docker shows the container version of this setup, and the Nginx Proxy Manager guide covers a panel-based alternative for managing many sites.
Common Multi-Domain Mistakes
The most common mistake is a missing or duplicate server name. Two blocks claiming the same domain mean Nginx warns at startup and serves one of them, while the other site silently never appears. Run nginx -t after every change and read its warnings, not just its pass or fail result.
The second mistake is sharing a document root between sites by accident, usually from copying a block and forgetting to change the root path. Both sites then serve the same files, and the error only shows up when one site’s content changes. Check each block’s root with a quick curl test against a file that exists on only one site.
The third mistake is forgetting the HTTPS redirect for a new site. A new server block often gets an HTTP block and an HTTPS block, but the HTTP block serves content instead of redirecting, leaving the site reachable over both schemes. Add the redirect as part of creating the block, not as a later cleanup task. With one file per site, a separate log per site, and a tested default block, a single Nginx server can host a dozen small sites without the config turning into a puzzle.