Configuring Nginx server blocks for multiple sites

Host multiple domains on one server by configuring Nginx server blocks. Step-by-step guide for configuration, troubleshooting common errors, and performance optimization.

5 min Updated 19 Aug 2026

Why Do You Need Nginx Server Blocks?

If you have a server and want to run multiple websites with different domains on it, Nginx Server Blocks is the standard and efficient solution. This feature, which is the equivalent of Virtual Host in Apache, allows you to define separate configurations for each domain, including Document Root, SSL settings, redirect rules, and access restrictions.

Without using server blocks, Nginx by default routes all requests to a single directory. This means if you point two different domains to your server's IP, both will show the same content. By properly configuring Nginx server blocks, you can optimize server resources and manage each site independently.

Prerequisites for Setting Up Nginx Server Blocks

Before you begin, make sure you have the following:

  • Root access or a user with sudo privileges on a Linux server (Ubuntu/Debian or CentOS/RHEL)
  • Nginx installed (version 1.18 or higher recommended)
  • Two domains whose A records point to your server's IP (e.g., example.com and example.org)
  • Website content in separate directories

To install Nginx on Ubuntu, use the following command:

sudo apt update
sudo apt install nginx -y

On CentOS/RHEL:

sudo yum install nginx -y

Directory Structure for Multiple Sites

The best practice is to create a separate directory for each domain. This makes file and access management simpler. The recommended structure looks like this:

/var/www/
├── example.com/
│   ├── html/
│   │   └── index.html
│   └── logs/
└── example.org/
    ├── html/
    │   └── index.html
    └── logs/

To create this structure, run the following commands:

sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/example.com/logs
sudo mkdir -p /var/www/example.org/html
sudo mkdir -p /var/www/example.org/logs

Then create a simple index.html file in each directory so you can test the result:

echo "<h1>Welcome to Example.com</h1>" | sudo tee /var/www/example.com/html/index.html
echo "<h1>Welcome to Example.org</h1>" | sudo tee /var/www/example.org/html/index.html

Creating Nginx Server Block Files

In Nginx, configuration files are typically placed in the /etc/nginx/sites-available/ directory and are activated via a symbolic link to /etc/nginx/sites-enabled/. This method allows you to disable sites without deleting the main file.

Configuring the First Domain

Create a new file for example.com:

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

Place the following content in it:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    access_log /var/www/example.com/logs/access.log;
    error_log /var/www/example.com/logs/error.log;

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

Explanation of important sections:

  • listen 80 – Listening on port 80 (HTTP)
  • server_name – The domain names this block responds to
  • root – The document root path for this site
  • try_files – Checks for the existence of the requested file and returns 404 if not found

Configuring the Second Domain

Now create the second file for example.org:

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

Similar content with necessary changes:

server {
    listen 80;
    listen [::]:80;
    server_name example.org www.example.org;

    root /var/www/example.org/html;
    index index.html index.htm;

    access_log /var/www/example.org/logs/access.log;
    error_log /var/www/example.org/logs/error.log;

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

Enabling and Testing the Configuration

To enable the server blocks, create symbolic links:

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

Before applying changes, always test the configuration:

sudo nginx -t

If everything is correct, the output will look like this:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Then reload Nginx:

sudo systemctl reload nginx

Now, by visiting http://example.com and http://example.org, you should see different content for each site.

Common Error: Default Server Block Conflict

One of the most common issues is conflict with the default Nginx file. The file /etc/nginx/sites-enabled/default typically acts as a catch-all and may route your requests to the wrong directory.

Solution: If you don't want to use the default site, disable it:

sudo rm /etc/nginx/sites-enabled/default

Then test and reload again.

Adding SSL with Let's Encrypt

For site security, make sure to enable SSL. The certbot tool automates this process:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

For the second domain:

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

certbot automatically adds SSL settings to the server block file and enables HTTP to HTTPS redirect. After running it, the example.com configuration file will look like this:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com www.example.com;

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

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

Performance Optimization for Multiple Sites

When you have multiple sites on one server, resource management is critical. Here are some practical tips:

Enabling gzip

Compressing responses reduces bandwidth usage. In the file /etc/nginx/nginx.conf, make sure these settings exist:

gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss;

Setting Cache for Static Files

You can enable browser caching for each server block:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

Rate Limiting to Prevent Abuse

If one of the sites has heavy traffic or brute-force attacks, add rate limiting:

limit_req_zone $binary_remote_addr zone=sitelimit:10m rate=5r/s;

server {
    # ... rest of the configuration
    location /login {
        limit_req zone=sitelimit burst=10 nodelay;
    }
}

Troubleshooting Common Issues

If the sites aren't working correctly, check these steps:

  1. Check DNS: Make sure the domains point to your server's IP: dig example.com +short
  2. Check Firewall: Ports 80 and 443 should be open: sudo ufw status
  3. Check Logs: Review the error files: sudo tail -f /var/log/nginx/error.log
  4. Test with curl: Check the headers: curl -I http://example.com

Another common mistake is forgetting www in server_name. If a user enters with www and you haven't defined it, they'll be redirected to the default site. Always add both versions or set up a 301 redirect from www to non-www.

Conclusion

Setting up Nginx server blocks for multiple sites is an essential skill for any server administrator. With this method, you can manage multiple domains on one server with independent configurations, separate logs, and different security settings. This not only reduces costs but also provides high flexibility in infrastructure management.

If you're looking for a scalable infrastructure for hosting multiple sites, ServerNet cloud servers with easy management and upgradeable resources can be a suitable choice. With proper Nginx server block configuration, you'll make the most of your server's capacity.

Was this page helpful?