On a single Ubuntu server, nginx server blocks let you host more than one website from one Nginx install. Each block is a config file that tells Nginx which domain a request belongs to, which folder holds that site files, and which rules apply to that site only. If you run two domains on one VPS, you need two blocks, not two servers.
This guide walks through the full setup on a clean Ubuntu 24.04 server: install Nginx, create a folder for your site, write a block file, enable it, and test it. You need a VPS with root or sudo access and a domain whose DNS A record already points to your server IP. Set the record before you start.
Before You Start: What You Need
Start with an updated server. Log in over SSH and run the update command, then check that no other web server is using port 80 or 443. If Apache is running, stop it first, since two servers cannot share port 80.
You also need a domain name to test with. This guide uses example.com as a placeholder. Replace every occurrence of example.com with your own domain, including inside file paths and config files. If you change only the config and forget the folder name, the paths will not line up and Nginx will return a 404.
For a WordPress site, the block in this guide is only the Nginx part. You still need PHP and a database behind it. If you plan to run WordPress, read the guide on how to install WordPress on a VPS with a LEMP stack after you finish the block setup, since it covers the PHP and MariaDB pieces this tutorial skips.
How Nginx Server Blocks Route Requests
When a browser asks for a page, it sends a Host header with the domain name. Nginx reads that header and compares it against the server_name line in each enabled block. The block with the matching name handles the request. If no block matches, Nginx serves the default block, which is why a fresh install shows the Nginx welcome page for any domain pointed at the server.
Ubuntu splits Nginx config into two folders: sites-available holds every block file you create, and sites-enabled holds symbolic links to the blocks you want active. You edit files in sites-available and link them into sites-enabled.
The nginx server block config itself is plain text. It sets the listen port, the server name, the document root, and the index files. Later blocks in this guide add logging and error handling, but a working block needs only those four pieces.
Step 1: Install Nginx on Ubuntu 24.04
Install Nginx from the Ubuntu repository. This gives you a version tested for Ubuntu 24.04, and updates arrive through the normal system update path.
sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
Check the status to confirm it started without errors:
sudo systemctl status nginx
Visit your server IP in a browser. You should see the Nginx welcome page. If it does not load, open ports 80 and 443 in your cloud firewall and in UFW:
sudo ufw allow 'Nginx Full'
sudo ufw enable
On ubuntu 24.04 nginx setup work, the Nginx Full profile opens both port 80 and port 443. Port 80 serves normal HTTP traffic, and port 443 serves HTTPS once you add a certificate later.
Step 2: Create the Website Folder and a Test Page
Each site gets its own folder under /var/www. Create one for your domain, then set ownership so your user account can write files without using sudo for every edit:
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
Now create a small test page so you can tell your new block apart from the default welcome page:
nano /var/www/example.com/html/index.html
<html>
<head><title>Welcome to example.com</title></head>
<body><h1>example.com is working</h1></body>
</html>
Save the file and close the editor. Replace this test page once your real site files are ready.
Step 3: Write Your First Server Block File
Create a new block file in sites-available. Name it after the domain so you can find it later:
sudo nano /etc/nginx/sites-available/example.com
Paste this config, then replace example.com with your domain in both places:
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/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
The listen lines bind the block to port 80, server_name lists the domains it answers for, root points at the folder from Step 2, and try_files serves the requested file, then a folder index, then a 404.
If you wonder whether Nginx or Apache fits your stack better, the comparison of LAMP vs LEMP for WordPress hosting explains the trade-offs in plain terms. For a plain HTML site or a WordPress site on Nginx, the block above is the right starting point either way.
Step 4: Enable the Block and Test the Config
Link the file into sites-enabled, then test the config before you reload. Testing first catches syntax errors while the old config is still running:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
A clean test prints a success message. If it reports an error, it names the file and line number: fix that line and test again. Do not reload until the test passes, since a broken config can stop every site on the server.
Once the test passes, reload Nginx so it reads the new block:
sudo systemctl reload nginx
Visit your domain in a browser. You should see the test page you created, with the text “example.com is working”. If you still see the Nginx welcome page, check two things: the symlink exists in sites-enabled, and the domain DNS record points to this server. A welcome page after a correct setup almost always means the request reached the default block, not your new block.
Step 5: Add a Second Site on the Same Server
Adding a second site repeats the same three moves: folder, block file, symlink. Nothing in the first block changes. That is the point of the sites-available and sites-enabled split: each site stays independent, so a mistake in one block does not spill into another.
sudo mkdir -p /var/www/second.com/html
sudo chown -R $USER:$USER /var/www/second.com/html
sudo nano /etc/nginx/sites-available/second.com
server {
listen 80;
listen [::]:80;
server_name second.com www.second.com;
root /var/www/second.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/second.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
This pattern is how multiple websites one nginx server setups stay manageable. In older Apache guides the same idea appears under a different name: ubuntu 24.04 nginx virtual hosts are the Nginx equivalent of Apache virtual hosts, and the nginx sites-available sites-enabled folder pair is what keeps each site separate. Ten sites mean ten folders and ten block files, each with its own log, so one site problem stays visible in its own error log.
Once both sites load over HTTP, the next step for a production site is HTTPS. The guide on how to install a free Let’s Encrypt SSL certificate on Nginx with Certbot covers that step, and Certbot can update your block files to add the HTTPS listener for you. After HTTPS works, a caching layer can cut load times further, and the guide to configure Nginx FastCGI cache for WordPress without a plugin shows how to add it without touching your WordPress install.
Troubleshooting Common Server Block Errors
Three errors cover most first-time setups. A 404 after enabling the block usually means the root path is wrong or the index file is missing. Compare the root line in your block against the real folder path with ls, and confirm an index file exists in that folder.
A welcome page instead of your site usually means the request hit the default block. Check that the symlink exists in sites-enabled, that server_name matches the domain you typed in the browser, and that DNS points to this server. Running curl with the Host header shows which block answers without waiting on DNS:
curl -I -H "Host: example.com" http://127.0.0.1/
An Nginx failed-to-start error after an edit almost always comes from a syntax mistake. Run sudo nginx -t and read the line number it reports. Common causes are a missing semicolon at the end of a line, a missing closing brace, and a duplicate default_server flag on two blocks. Fix the line, test again, and reload only after the test passes.
For a final check, run nginx -t, check systemctl status nginx, and visit each domain in a browser. When all three agree, your blocks are ready for real site files.