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 Oct 10, 2026 6 min read
Enable Automatic Suspension in Ubuntu Linux Easily
Enable Automatic Suspension in Ubuntu Linux Easily

An Nginx server block lets you host two or more websites on a single Ubuntu Linux machine. Nginx reads the domain name in each incoming request and routes it to the right site folder. A server block tells Nginx where the website files are and how to handle requests for a specific domain name, such as example.com.

Advertisement

Without server blocks, Nginx cannot tell the sites apart when they share one machine. Every request would end up at the same default site.

⚡ 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.

Advertisement

How to create website directory structures on Ubuntu Linux

Start by making a separate folder for each site inside the /var/www directory. This folder is the document root, the place where all of that site’s web files live. Keeping each site in its own folder is what lets Nginx show visitors the right pages for each domain.

You need a user with sudo rights and a terminal on the server. Before you begin, decide on the domain names you will use. This tutorial uses example.com throughout, so replace it with your own domain.

Here is an example of a directory structure for several websites, each with its own content and domain.

Each domain gets a dedicated folder for its files. For example, the site example.com can keep its files in /var/www/example.com/public_html. Because every site has its own document root, Nginx serves the correct files for each domain and content never gets mixed up between sites.

Advertisement

The name of the last folder is up to you. Some admins use public_html and others use html, as in the next step. What matters is that the folder you create is the same one you point Nginx to later in the server block file. Pick one name and use it for every site.

  1. Open a terminal on your Ubuntu server.
  2. Run the sudo mkdir -p /var/www/example.com/html command to create the directory for the website and its files.
  3. Check that the folder exists by listing the contents of /var/www. You should see a folder named after your domain.

The -p option tells mkdir to create any missing parent folders along the way. It also keeps the command from failing if a folder already exists. That makes the command safe to run more than once.

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

Each document root needs an index.html file. It is the page Nginx shows when someone visits the bare domain. Run the commands below to create an index.html file for the example.com domain.

  1. Run the commands below. The first one opens a text editor with an empty file.
  2. Copy the sample HTML from the block below and paste it into the editor.
  3. Save the file and close the editor. You should be back at the command prompt.

This gives you a basic HTML page for testing. Put some text in it that is unique to this domain, such as the domain name itself. That way you can tell at a glance which site Nginx is serving when you test later.

Advertisement
💻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 the file is saved, you can configure Nginx to use this content. First, avoid permission problems by changing the ownership of the domain's document root directory, and all the files inside it, to the www-data user. This is the account the Nginx worker processes run as on Ubuntu. If the files belong to another user and are not readable, visitors can get a 403 Forbidden error.

Run the commands below. They only change who owns the files. They do not change the content, and you can run them again at any time.

🐧Bash / Shell
sudo chown -R www-data: /var/www/example.com

How to create an Nginx server block file on Ubuntu Linux

The site content is in place, so the next job is the Nginx server block configuration file that points to it. You will create the file, paste in the configuration, and then enable it.

On Ubuntu, the server block configuration files live in the /etc/nginx/sites-available directory. Files placed here are not active yet. A server block only goes live once you link it into the enabled sites directory, which is covered below. That lets you keep a site's configuration on disk and switch it off without deleting it.

Advertisement

Run the commands below to create a server block file in the sites-available directory for your content. This creates a site-specific virtual host configuration file.

  1. Run the commands below. The first one opens a text editor with an empty file.
  2. Paste the server block configuration from the next block into the editor.
  3. Check that the domain name and the root folder in the configuration match your site, and edit them if they do not.
  4. Save the file and close the editor.

This Nginx server block configuration works in most environments. Copy and paste it into the nginx.conf file you created, then save the file.

The root path in the file must be the same folder you made earlier. A mismatch is the most common reason a new site shows a 404 error.

Saving the file is not enough on its own. The server block becomes active only after Nginx reloads its configuration and reads the new file. With Nginx version 1.14.2 and later, the reloaded server block then handles requests for your site. The next steps enable the file and reload Nginx.

Advertisement

To enable the new server block, you create a symbolic link from the server block file to the sites-enabled directory. A symbolic link is a pointer, not a copy. If you edit the file in sites-available later, the enabled site picks up the change after the next reload.

Run the commands below to enable the configuration file for your domain.

🐧Bash / Shell
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Next, run the commands below to restart the Nginx service. A restart makes Nginx load the new site. If Nginx fails to start, the cause is usually a typo in the server block file, such as a missing semicolon or a wrong path. Reopen the file, fix the mistake, and restart again.

🐧Bash / Shell
sudo systemctl restart nginx

Once Nginx has restarted, open a browser and go to the server hostname or IP address. You should see the content of the file you created earlier.

Advertisement

If you see the default Nginx welcome page instead, check these things:

  • The symbolic link exists in the enabled sites directory.
  • The server name in your server block matches the domain you typed.
  • Nginx was restarted after the last change.

If you see a 403 error, recheck the ownership and permissions on the document root.

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

Repeat the process for each additional domain. Give each one its own document root, its own index file, and its own server block configuration file. You can create as many virtual host files as your server can handle.

With server blocks configured, you can host multiple websites on a single server and make better use of its resources.

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.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

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

Leave a Comment

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