Nginx server blocks (the equivalent of Apache virtual hosts) let you host multiple websites on a single server. This guide creates a server block for a custom domain on Ubuntu 24.04 LTS.

Tested and valid on:

  • Ubuntu 24.04 LTS

Prerequisites

  • Ubuntu 24.04 LTS server
  • Nginx installed
  • A domain name pointed to your server’s IP

Step 1 – Create the Document Root

Create a directory for your site (replace example.com with your domain):

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

Step 2 – Create a Sample HTML Page

Create a test index page:

nano /var/www/example.com/html/index.html

Add:


  Welcome to example.com!
  

Success! example.com is working.

Step 3 – Create the Server Block File

Create a new Nginx configuration file:

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

Add the server block:

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/html;
    index index.html index.htm index.php;
    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ =404;
    }
}

Step 4 – Enable the Server Block

Create a symlink to sites-enabled:

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

Step 5 – Avoid Hash Bucket Size Issues

Edit /etc/nginx/nginx.conf and uncomment:

server_names_hash_bucket_size 64;

Step 6 – Test and Reload

Test the configuration and reload:

sudo nginx -t
sudo systemctl reload nginx

Step 7 – Verify in Browser

Navigate to http://example.com — you should see your index page.

Conclusion

Your Nginx server block is configured on Ubuntu 24.04 LTS. Repeat these steps for each additional domain. The next step is to secure each site with a free Let’s Encrypt SSL certificate.