Skip to content
Follow
Ubuntu Linux

How to Set up a Nginx Server Block on Ubuntu Linux

Richard
Written by
Richard
Dec 7, 2021 Updated Aug 13, 2026 4 min read
Enable Automatic Suspension in Ubuntu Linux Easily
Enable Automatic Suspension in Ubuntu Linux Easily

An Nginx server block lets you host more than one website on a single Ubuntu Linux machine. Think of each block as a separate manager that handles visitor traffic for just one specific site.

You need a server block so your web server knows where to find your website files and how to handle requests for domain names like example.com. Without these blocks, your server cannot tell different websites apart on the same computer.

⚡ Quick Answer

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

Creating a separate folder for each website’s files (known as the document root) is the first step to setting up an nginx server block on Ubuntu. This organisation helps your server quickly find and show the correct content for each website you host.

Here’s an example of a directory structure for multiple websites with unique content and domains.

Each website domain requires a dedicated folder for its files, called a document root. For example, the website "example.com" stores its files in the `/var/www/example.com/public_html` folder. This document root setup ensures Nginx sends the correct website files for multiple sites on one server, preventing content mix-ups for up to 10 different domains.

Run the sudo mkdir -p /var/www/example.com/html command to create the directory for the website and its files. This specific command creates the necessary folders within the `/var/www/` path to store your website content.

🐧Bash / Shell
sudo mkdir -p /var/www/example.com/public_html

You'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.

The basic HTML file for testing is created by copying and pasting the provided content into the configuration file and then saving the file.

💻Code
<!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, you'll want to change the ownership of the domain document root directory and all files within it to the www-data user.

🐧Bash / Shell
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.

This Nginx server block configuration works with most environments. Copy and paste the provided Nginx server block configuration into the `nginx.conf` file you created and save the `nginx.conf` file.

Once saved, the Nginx server block configuration file becomes active when Nginx reloads its configuration. Reloading ensures Nginx reads the new file, allowing the server block to handle website requests starting from version 1.14.2. This step makes the server block functional.

💡Tip
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.

🐧Bash / Shell
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
⚠️Warning
After that, run the commands below to restart the Nginx service.
🐧Bash / Shell
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.

Nginx server block configuration file test example Ubuntu
apache virtual host file test

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.

What is a server block in NGINX?

In NGINX, a server block is a configuration block that specifies how the server should handle traffic for a specific domain or group of domains. This allows you to define different settings and behavior for different websites or applications that are hosted on the same server.

Can NGINX have multiple server blocks?

Can NGINX handle multiple server blocks? Yes, you create each block within the Nginx configuration file to achieve this. In Nginx, you can have different configurations for each domain or subdomain hosted on your server. The `nginx.conf` file will have the server blocks.

Was this guide helpful?

Was this helpful?
Richard

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.

📚 Related Tutorials

Upgrade Ubuntu Linux Without Losing Data
Ubuntu Linux Upgrade Ubuntu Linux Without Losing Data
Enable or Disable Bluetooth on Ubuntu Linux
Ubuntu Linux Enable or Disable Bluetooth on Ubuntu Linux
How to Setup Let's Encrypt with Nginx on Ubuntu Linux
Ubuntu Linux How to Setup Let's Encrypt with Nginx on Ubuntu Linux
How to Enable Nginx Userdir on Ubuntu 24.04 Easily
Ubuntu Linux How to Enable Nginx Userdir on Ubuntu 24.04 Easily

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *