Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple independent websites on a single VPS, each with its own domain, document root, and configuration.

Prerequisites

  • Nginx installed and running
  • One or more domains with DNS pointing to your VPS

Step 1 — Create a Directory for the New Site

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html

Step 2 — Add a Test Page

nano /var/www/example.com/html/index.html
<h1>Welcome to example.com</h1>

Step 3 — Create the Server Block Configuration

sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

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

Step 4 — Enable the Site

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 5 — Test and Reload

sudo nginx -t
sudo systemctl reload nginx

Step 6 — Repeat for Additional Domains

Each new domain gets its own file in sites-available, symlinked into sites-enabled, with a unique server_name and root.

Disabling a Site

sudo rm /etc/nginx/sites-enabled/example.com
sudo systemctl reload nginx

Adding HTTPS to Each Site

Run Certbot separately per domain after the server block is working over HTTP:

sudo certbot --nginx -d example.com -d www.example.com

Common Errors

"Duplicate default server" — only one server block per port can have default_server; remove it from all but one.

Wrong site loads for a domain — check that server_name exactly matches the domain and that DNS actually points here.

404 for all pages except the homepage — check the try_files directive and that your app's routing rules are correctly configured.

Best Practices

  • Use one file per domain in sites-available for clarity
  • Always run nginx -t before reloading
  • Keep a default "catch-all" server block to reject requests with unrecognized Host headers

FAQ

How many websites can one VPS host this way?
Limited mainly by server resources (RAM/CPU) rather than Nginx itself — dozens of low-traffic sites can comfortably run on a modest VPS.

Continue Reading

Browse more articles in Web Servers.

  • nginx virtual host, server blocks, multiple websites, nginx configuration
  • 1 Benutzer fanden dies hilfreich
War diese Antwort hilfreich?

Verwandte Artikel

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...

How to Install OpenLiteSpeed on a Linux VPS

OpenLiteSpeed is a high-performance, open-source web server known for excellent PHP performance...