Top.Mail.Ru
30% off VPS 7 d 08:40:59 Grab it

Nginx Server Blocks: Several Websites on One VPS

1
Nginx Server Blocks: Several Websites on One VPS

A server block in Nginx is a server { } configuration block that lets you host several websites on one server: each block describes its own domain, file folder and SSL settings. One Nginx can serve any number of server blocks at the same time. It looks at the Host header of the incoming request and sends it to the right block. That is how dozens of sites run on one VPS with one IP address, told apart only by domain name.

How Nginx picks the right site

When a browser requests a site, it sends a Host header with the domain. Nginx compares that value with the server_name directive in each server block and passes the request to the block that matches.

The order of matching is fixed:

Nginx first looks for an exact domain match. For example, example.com matches only server_name example.com;.

If there is no exact match, wildcard names with a leading asterisk come next, such as *.example.com. That pattern matches blog.example.com or shop.example.com.

Wildcard names with a trailing asterisk, such as www.example.*, are checked after that.

Regular expressions in server_name, if you used any, are checked last. The first match in this order wins, and the search stops there.

Configuration file layout

The standard Nginx layout on Ubuntu splits the configuration into two directories.

The /etc/nginx/sites-available/ directory stores the configuration files of all sites, active and inactive. It is just storage and has no effect on its own.

The /etc/nginx/sites-enabled/ directory holds symbolic links to those files from sites-available that should be active. A link in this directory is what makes a site visible to Nginx.

This split is handy in practice: to turn a site off temporarily without deleting its configuration, just remove the link from sites-enabled and leave the file in sites-available.

Multiple sites on Nginx: creating server blocks

Create a directory for the first site’s files:

sudo mkdir -p /var/www/site1.com/html

Create a directory for the second site’s files:

sudo mkdir -p /var/www/site2.com/html

Create a test page for each, to check that every site opens the right content:

echo "<h1>Site 1: site1.com</h1>" | sudo tee /var/www/site1.com/html/index.html
echo "<h1>Site 2: site2.com</h1>" | sudo tee /var/www/site2.com/html/index.html

Create the configuration file for the first site:

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

Contents of the first site’s file:

server {
    listen 80;
    listen [::]:80;

    server_name site1.com www.site1.com;
    root /var/www/site1.com/html;
    index index.html;

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

Create the configuration file for the second site:

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

Contents of the second site’s file. The structure is identical, only the domain and the path differ:

server {
    listen 80;
    listen [::]:80;

    server_name site2.com www.site2.com;
    root /var/www/site2.com/html;
    index index.html;

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

Enable both sites by creating symbolic links:

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

Check the configuration for syntax errors before applying it:

sudo nginx -t

If the check passes, apply the changes:

sudo systemctl reload nginx

default_server: what happens to unknown domains

If a request arrives with a domain or IP address that matches no server_name, Nginx has to respond somehow. By default it picks the first server block in file reading order, which is neither obvious nor always predictable.

To control this behavior explicitly, mark one of the blocks with the default_server flag:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    return 444;
}

The value server_name _; means “any name that matched nothing else”. The return 444; directive is a special Nginx code that closes the connection without sending a response. Such a block works as a catch-all for bots and scanners that hit the server directly by IP rather than by domain name. They get a dropped connection instead of a real site.

Put this block in a separate file and include it first in the sites-enabled list, so there is no confusion about which of your real sites accidentally becomes the default.

Setting up SSL for several sites

Each domain on the server gets its own SSL certificate. This causes no conflicts, because modern browsers support SNI (Server Name Indication): the client tells the server the domain it wants before the secure connection is finished, and Nginx uses that to choose the right certificate among several on one IP and port 443.

Issue a certificate for the first site with Certbot:

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

Issue a certificate for the second site:

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

Certbot finds the right server block by the domain you give and adds the ssl_certificate directives to it, along with an automatic HTTP to HTTPS redirect. You do not need to edit the configuration files by hand afterwards.

Need a VPS for several sites? Promo code WIKI15 takes 15% off a VPS order at UFO.Hosting.

Separate logs for each site

If several sites write to a shared log file, diagnosing one site’s problem gets awkward: you have to dig the right lines out of everyone else’s traffic. To give each site its own log files, add these directives inside the matching server block:

server {
    listen 80;
    server_name site1.com www.site1.com;
    root /var/www/site1.com/html;

    access_log /var/log/nginx/site1.com-access.log;
    error_log /var/log/nginx/site1.com-error.log;

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

After this change, all requests and errors of that site go to separate files, and tracking down one site’s problem does not mean wading through the logs of all the others.

Checking which site answers without DNS set up

You often need to test domain routing before the DNS records for a new domain are actually set up and propagated. You can do it by overriding the Host header in the request by hand:

curl -H "Host: site1.com" http://YOUR_IP/

This request goes straight to the server’s IP address but carries a Host header as if it came from the real site1.com domain. Nginx handles it like a normal request from that domain and returns the matching site’s content. That lets you verify the configuration before the domain is pointed at the server through DNS.

Multiple sites on different IP addresses

If the server has several IP addresses, you can separate sites not only by domain but also by IP, by naming it explicitly in the listen directive:

server {
    listen 192.168.1.10:80;
    server_name site1.com;
    root /var/www/site1.com/html;
}

server {
    listen 192.168.1.11:80;
    server_name site2.com;
    root /var/www/site2.com/html;
}

listen 80; without an address makes Nginx listen on that port on all of the server’s IP addresses at once. Giving a specific IP in listen narrows the block to that single address. This can be useful, for example, if different sites must be isolated at the network level.

Finding conflicts between configurations

If several configuration files accidentally contain the same server_name, Nginx applies only the first block it finds in order, and the others with the same name are silently ignored, with no explicit error, which makes diagnosis harder.

To see the full merged Nginx configuration with all loaded files, run:

sudo nginx -T

The command prints the main configuration file together with everything pulled in through include, so you can spot duplicate server_name values across different files in sites-enabled.

Common mistakes

The root directive is missing from the block

If the root directive is missing both in the server block and in its location blocks, Nginx uses a default path, usually /usr/share/nginx/html or a similar system directory, depending on the distribution. The site opens, but shows completely different content from what you expected.

The same server_name in different files

If two different configuration files contain the same server_name, only the first one in Nginx’s file reading order is used. The second site stays unreachable, and there may be no explicit error in the logs. Use nginx -T to find such a conflict.

A forgotten link in sites-enabled

A configuration file can be completely correct, but if no symbolic link to it exists in sites-enabled, Nginx simply does not see it and does not load it. You can check which sites are actually active with ls -la /etc/nginx/sites-enabled/.

Frequently asked questions

How many sites can one Nginx host?

Nginx has no technical limit on the number of server blocks. The practical limit is set by the server’s resources, the RAM and CPU needed to handle the combined traffic of all the sites, not by Nginx itself.

Do I need a separate IP address for each site?

No. All sites can run on one IP address at the same time, because Nginx tells them apart by the Host header of the HTTP request, not by IP address. Separate IP addresses are only needed in special cases, such as separating sites at the network level or for some mail server setups.

What happens if I open the site directly by the server’s IP address?

Either the server block with the default_server flag answers, if it is set explicitly, or the first server block in file reading order, if default_server is not set anywhere. To control this on purpose, always set up a separate block with default_server and return 444;.

Can I use one SSL certificate for several domains?

Yes, if the certificate is issued as a multi-domain (SAN) certificate or as a wildcard certificate for subdomains of one domain. Let’s Encrypt can issue a certificate for several domains at once through the -d flag given several times in one certbot command.

How do I quickly check which sites are active on the server?

The list of active sites is the contents of the sites-enabled directory: run ls -la /etc/nginx/sites-enabled/. The full expanded configuration with every detail of each site is shown by nginx -T.

Several sites on one VPS is an efficient way to use server resources. At UFO.Hosting even a basic 2 GB RAM configuration comfortably serves several small sites through one Nginx.

Related

All articles
Стеклянное хранилище в форме облака с папками и файлами, тарелка лучом открывает замок

Nextcloud on a VPS: your own cloud with Docker Compose

Nextcloud is a cloud you own: files synced to your phone and computer, a calendar, contacts, and share-by-link. On a VPS you run it with Docker Compose as five containers: Nextcloud itself, a PostgreSQL database, a Redis cache, a container…

Server rack with two monitors and two users: two administrators working on one Windows server

RDP for two users on Windows Server 2016-2025: setup guide

By default, Windows Server 2016, 2019, 2022 and 2025 let two administrators connect over RDP at the same time, and you don’t need to buy Remote Desktop licenses for it. Below: how this works, how resources are shared, and a…