To host several websites on one server, create one Nginx server block per site, give each its own server_name and root, and Nginx will choose the right block from the Host header of each request. This is how nginx server blocks for multiple domains work: one Nginx process, one IP address, many sites, each with separate files and logs.
The same idea is called virtual hosting. You only need DNS records pointing every domain to the server.
When a request arrives, Nginx first matches the IP address and port against every listen directive. Among the server blocks that share that address, it compares the Host header with each server_name. The order of preference is:
example.com.*, such as *.example.com.*, such as example.*.~.If nothing matches, the default server answers. That is the first block defined for the port unless you mark another with default_server.
Create a separate document root for each site and give it a simple page so you can tell them apart:
Using /var/www/<domain>/html keeps each site isolated and makes backups and permissions easy to reason about.
On Debian and Ubuntu, put each site in its own file under /etc/nginx/sites-available/. On RHEL-family systems use /etc/nginx/conf.d/example.com.conf.
Each block lists both the bare domain and the www host in server_name. Per-site log files make debugging much easier than one shared log.
On Debian-style layouts a site is active only when it is symlinked into sites-enabled:
Test without touching DNS by sending the Host header yourself:
Requests that use an unknown host name, or only the raw IP address, land in the default server. It is better to reject them deliberately than to show one of your real sites:
The name _ is just a placeholder that never matches a real host. Status 444 is an Nginx-specific code that closes the connection without a response. For HTTPS you also need a certificate for this block, or use ssl_reject_handshake on; in a default TLS server.
| Problem | Cause and fix |
|---|---|
| Wrong site appears | Missing or misspelt server_name, so the default server answered |
conflicting server name warning | Same name defined in two blocks; remove the duplicate |
| 403 or 404 on files | Wrong root path or files not readable by the Nginx user |
| New site ignored | Symlink missing in sites-enabled, or you forgot to reload |
Changes to root seem lost | Browser cache; test with curl |
There is no small fixed limit. Thousands are possible; memory for the hash tables of names grows, and you may need to raise server_names_hash_bucket_size for very long names.
Yes. Name-based virtual hosting uses the Host header, and for HTTPS the TLS SNI extension, so one IP can serve any number of domains.
Not required, but strongly recommended. One file per domain makes enabling, disabling and reviewing a site simple.
No. Run nginx -t and then systemctl reload nginx, which applies the change without dropping active connections.
Create one server block per domain with its own server_name, root and logs, enable it, test with nginx -t, and reload. Add a default server that rejects unknown hosts, and verify each site with curl -H "Host: ..." before DNS changes go live.