How to Set up a Nginx Server Block on Ubuntu Linux
You set up an Nginx server block on Ubuntu Linux to manage multiple websites on a single server.
A server block is Nginx’s configuration unit that defines how the server handles requests for a specific domain or subdomain, similar to Apache’s virtual hosts. It allows you to host distinct websites, each with its own settings.
For example, you can configure Nginx to serve example.com on port 80 and another.org on port 443 with its own SSL certificate, all from one Ubuntu machine. Your server block configuration file specifies crucial details like the document root, security protocols, and SSL certificate paths for each site.
This method efficiently utilizes your server’s resources, enabling you to host multiple sites without requiring separate physical or virtual machines.
Create website directories in /var/www/, then create an Nginx configuration file in /etc/nginx/sites-available/. Link this file to /etc/nginx/sites-enabled/ and restart Nginx.
How to create website directory structures on Ubuntu Linux
Setting up website directories on Ubuntu Linux means creating a specific folder for each site’s files, called a document root, so your server knows where to find them.
Here’s an example of a directory structure for multiple websites with unique content and domains.
/var/www/ ├── example.com │ └── public_html ├── example.net │ └── public_html
Notice how each domain gets its own folder, including a document root like this: /var/www/domain/public_html.
Let’s create a directory for the example.com domain and its document root using the following commands.
sudo mkdir -p /var/www/example.com/public_htmlYou’ll need an index.html file in each document root to display content to visitors. Run the commands below to create an index.html file for the example.com domain.
sudo nano /var/www/example.com/public_html/index.html
Then copy and paste the content below into the file and save it. This is a basic HTML file we’ll use for testing.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>Once you save the file, we can configure Nginx to reference this content. To avoid permission issues, change the ownership of the domain document root directory and all files within it to the www-data user.
sudo chown -R www-data: /var/www/example.com
How to create an Nginx server block file on Ubuntu Linux
Now that you’ve created domain content in the directory above, configure the Nginx server block configuration file for that content.
You’ll find the Nginx server block configuration files on Ubuntu Linux within the /etc/nginx/sites-available directory.
To create a server block file in the sites-available directory for our content, run the commands below. This creates a site-specific Virtual Host configuration file.
sudo nano /etc/nginx/sites-available/example.com.confAn example configuration that should work with most environments is shown below. Copy and paste the content below into the file you just created and save it.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.html;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}Once the file is saved, you can enable it to become a working server block.
You use the symbolic link command to enable the new server block configuration file. This creates a symbolic link from the server block file to the sites-enabled directory.
Run the commands below to enable the configuration file for our domain.
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
After that, run the commands below to restart the Nginx service.
sudo systemctl restart nginx
Once Nginx is restarted, browse to the server hostname or IP address. It should display the content file we created above.

Repeat this process for other domains and their server block configuration files. You can create as many virtual host files as your server can handle.
That should do it!
Conclusion:
- Configuring Nginx server blocks allows you to host multiple websites on a single server, optimizing resource usage.
- Each website requires a unique directory structure, document root, and configuration settings.
- By following the steps outlined, users can create and enable server blocks for different domains effectively.
- Always ensure proper permissions are set for document root directories to avoid access issues.
- Regularly check server logs to monitor traffic and troubleshoot any problems that may arise.
- Feel free to create additional server blocks as needed, expanding your web hosting capabilities.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!