Nginx Server Blocks: Host Multiple Websites on One VPS

One VPS can host several websites at once, each with its own files, logs, and domain name. The piece that makes it work in Nginx is the server block: a small config file that tells Nginx which site to serve when a visitor asks for a given domain. This guide shows how nginx server blocks multiple websites on a single VPS, from the first block to a second live site, with the commands you can run as you read.

What a Server Block Actually Is

A server block is a server { } section in an Nginx config file. It holds the domain name in a server_name line, the folder that holds the site files in a root line, and the log files that record visits and errors. When a request arrives, Nginx reads the Host header, finds the block whose server_name matches, and serves files from that block’s root folder.

Apache users will recognise the idea: a server block is the Nginx name for what Apache calls a virtual host. Each site gets its own block, and Nginx keeps the sites apart by domain name, not by port or IP address, so all of them can share port 80 and port 443.

What You Need Before You Start

You need a VPS with Nginx installed and running, a domain pointed at the server IP with an A record, and a user account with sudo access. Both domains should already resolve to the server before you test. A dig +short example.com check confirms the DNS side.

You also need to know how your Nginx install is laid out. On Debian and Ubuntu, site configs live in /etc/nginx/sites-available/ and are switched on by a symlink in /etc/nginx/sites-enabled/. On RHEL-based systems the files sit under /etc/nginx/conf.d/. The examples below follow the Debian and Ubuntu layout. If you are still choosing a web server for a busy site, our Nginx vs Apache comparison for high-traffic servers in 2026 explains where each one wins before you commit.

Step 1: Create the Site Folder and a Test Page

Start with the first site, example.com. Make its web root folder and give it to your user account so you can edit files without sudo every time:

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

Then drop in a test page so you can tell the sites apart later:

echo "<h1>Hello from example.com</h1>" > /var/www/example.com/html/index.html

Keep each site in its own folder under /var/www/. Shared folders cause shared mistakes: one bad deploy can overwrite another site, and one site’s uploads can fill the disk for all of them. Separate roots also make backups simple.

Step 2: Write the First Server Block

Create a config file for the site in the available folder:

sudo nano /etc/nginx/sites-available/example.com

Paste in this block, which is a working starting point for a plain static or PHP site behind Nginx:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

Read it line by line and it stops looking cryptic. The listen lines take HTTP traffic. The server_name line lists the domains this block answers for. The root line points at the folder from step 1. The two log lines give this site its own logs. The location section tries the requested file, then a folder index, then returns a 404.

This is a nginx server block config you can reuse as a template: copy the file, change the domain, root, and log names, and you have the next site. Keep the template in your notes, because you will reach for it every time you add a domain. If your sites run WordPress rather than static files, the same block shape applies, with a PHP location added; our guide to Nginx caching for WordPress at scale builds on this exact starting point.

Step 3: Enable the Site and Test the Config

A file in sites-available does nothing until it is linked into sites-enabled. Create the symlink, then ask Nginx to check the config before you reload:

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

The nginx -t step is the one people skip and then regret. It catches typos, missing semicolons, and duplicate server_name entries before they take the server down. If the test passes, reload applies the new config without dropping existing connections. If it fails, read the error line, fix the file, and test again. Never restart Nginx to apply a config you have not tested.

Now visit http://example.com in a browser. You should see your test page. If you see the Nginx welcome page instead, the request hit the default block, which usually means the domain still points elsewhere or the server_name line does not match the domain you typed.

Step 4: Add a Second Site the Same Way

The second site repeats the same three moves: a folder, a block, a symlink. Make a folder for example.net, add a test page with different text, then copy the first config and edit the three lines that change:

sudo mkdir -p /var/www/example.net/html
echo "<h1>Hello from example.net</h1>" > /var/www/example.net/html/index.html
sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/example.net
sudo nano /etc/nginx/sites-available/example.net

In the copy, change server_name to example.net www.example.net, change root to /var/www/example.net/html, and change both log file names to use example.net. Then link it, test, and reload:

sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Visit both domains. Each one should show its own test page, which proves Nginx is routing by domain name. Adding a third or fourth site is the same routine. The practical limit is the server: CPU, RAM, and disk decide how many sites the box can carry. That is the whole idea behind nginx virtual hosts, one config per site, one folder per site, and the server sorting requests by name.

Shared hosting still needs isolation between sites. A runaway script on one site can eat the CPU the others need. If your sites run different apps with different PHP versions, consider containers for the app layer; our guide on how to isolate multiple sites on one VPS with Docker shows that setup, while server blocks stay in charge of routing.

Add HTTPS to Every Site

Plain HTTP is fine for a first test, but every live site needs HTTPS. Certbot can issue a free certificate per domain and edit your server blocks to add the redirect. Run it once per site:

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

Certbot adds a second server block on port 443 and a redirect in the port 80 block. Check both sites after each run. Certificates renew through a timer Certbot installs, but test renewal once with sudo certbot renew --dry-run, so an expired certificate never surprises you.

When several sites share one server, connection limits matter more than they do on a single-site box. A traffic spike on one domain can fill the worker connections that the others need. Our guide to Nginx concurrent connection tuning explains how to size worker processes and connections for the traffic you actually get.

Nginx Server Blocks Multiple Websites: Mistakes to Avoid

Most server block problems come from a short list. A missing semicolon stops Nginx from starting, so always run nginx -t first. A server_name typo sends visitors to the default site, so copy the domain from your DNS record. A wrong root path gives a 404 on every page, so check that the folder exists and holds an index file.

Two more habits save trouble later. Keep the default server block disabled once your real sites are live, so stray domains do not show your content. And check a site’s own error log when it breaks; the log names the missing file or the bad directive. If you later put Cloudflare in front of the server, read our zero-downtime Nginx reverse proxy tutorial with Cloudflare and HTTP/3 before you change the blocks, because the proxy headers and ports need to match.

That is the full routine: one folder and one block per site, a test after every change, and HTTPS on each domain. Once the second site works, the third takes minutes, and you host multiple sites on one VPS.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.