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-availablefor clarity - Always run
nginx -tbefore reloading - Keep a default "catch-all" server block to reject requests with unrecognized
Hostheaders
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
- How to Install Nginx on Ubuntu & Debian
- How to Install Let's Encrypt SSL with Certbot (Nginx & Apache)
- Nginx as a Reverse Proxy for Node.js/Docker Apps
Browse more articles in Web Servers.
