Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Set Up Nginx Server Blocks for Multiple Sites?

Hosting
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
5 min read

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.

Table of Contents

  • How Nginx picks a server block
  • Preparing directories and test pages
  • Writing one server block per domain
  • Enabling the sites and reloading
  • Adding a catch-all default server
  • Common mistakes with multiple sites
  • Frequently Asked Questions
    • How many server blocks can Nginx have?
    • Can two sites share one IP address?
    • Do I need a separate config file per site?
    • Do I need to restart Nginx after adding a site?
  • Summary
  • Related Articles

How Nginx picks a server block

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:

  1. An exact name, such as example.com.
  2. The longest wildcard starting with *, such as *.example.com.
  3. The longest wildcard ending with *, such as example.*.
  4. The first matching regular expression, written with a leading ~.
  5. The default server for that port.

If nothing matches, the default server answers. That is the first block defined for the port unless you mark another with default_server.

Preparing directories and test pages

Create a separate document root for each site and give it a simple page so you can tell them apart:

bash

Using /var/www/<domain>/html keeps each site isolated and makes backups and permissions easy to reason about.

Writing one server block per domain

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.

nginx
nginx

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.

Enabling the sites and reloading

On Debian-style layouts a site is active only when it is symlinked into sites-enabled:

bash

Test without touching DNS by sending the Host header yourself:

bash
text

Adding a catch-all default server

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:

nginx

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.

Common mistakes with multiple sites

ProblemCause and fix
Wrong site appearsMissing or misspelt server_name, so the default server answered
conflicting server name warningSame name defined in two blocks; remove the duplicate
403 or 404 on filesWrong root path or files not readable by the Nginx user
New site ignoredSymlink missing in sites-enabled, or you forgot to reload
Changes to root seem lostBrowser cache; test with curl

Frequently Asked Questions

How many server blocks can Nginx have?

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.

Can two sites share one IP address?

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.

Do I need a separate config file per site?

Not required, but strongly recommended. One file per domain makes enabling, disabling and reviewing a site simple.

Do I need to restart Nginx after adding a site?

No. Run nginx -t and then systemctl reload nginx, which applies the change without dropping active connections.

Summary

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.

Related Articles

  • How Do You Understand the Nginx Configuration File Structure?
  • How Do You Redirect HTTP to HTTPS and www in Nginx?
  • Learn Web Hosting: Complete Study Roadmap from Zero to Pro
Share

Similar posts

  • How Do You Understand the Nginx Configuration File Structure?
  • How Do You Install Nginx on Ubuntu and Start It?
  • What Is Nginx and How Is It Different from Apache?
  • How Do You Fix Apache 403 Forbidden Errors?
  • How Do You Set Up a Reverse Proxy in Apache?
  • How Do You Redirect HTTP to HTTPS in Apache?
  • How Do You Enable and Use Apache Modules with a2enmod?
  • How Do You Configure Apache Virtual Hosts on Ubuntu?