How to Set up a Nginx Server Block on Ubuntu Linux

This post shows students and new users steps to configure a Nginx server block file on Ubuntu Linux. A server block is a feature with Nginx that allows users to run more than one websites on a single server.

A server block file contains configuration directives for website, including a site’s document root, security policies, SSL certificate configuration, and much more. Each website that is configured within a Nginx server block operates independently of each other with separate and unique settings.

Nginx server block feature allows webmaster to maximize a server resources by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures on Ubuntu Linux

When you’re running multiple websites on a single host, each website will have its own document root. A document root is the directory where the website files for a domain are stored and served in response to requests.

Below is an example of a directory structure for multiple website with unique content and domains.

│   └── public_html
│   └── public_html

As you can see above, each domain will have its own folder with a document root in included.

Example: /var/www/domain/public_html

Run the commands below to create directory for domain with its document root.

sudo mkdir -p /var/www/

Each document root will need an index.html file that will be shown to clients. Run the commands below to create an index.html file for the domain.

sudo nano /var/www/

Then copy and paste the content below into the file and save. Below is just a basic HTML file for testing purposes.

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

Once you save the file, we’re ready to configure Nginx to reference this content. To avoid any permission issues, change the ownership of the domain document root directory and all files within the directory to the www-data user.

sudo chown -R www-data: /var/www/

How to create a Nginx server blcok file on Ubuntu Linux

Now that you’ve created a domain content at the directory above, go and configure Nginx server block configuration file for the domain content above.

On Ubuntu Linux, Nginx server block configuration files are located in the /etc/nginx/sites-available directory.

To create a server block file in the sites-available directory for our content above, run the commands below to create a site specifics Virtual Host configuration file

sudo nano /etc/nginx/sites-available/

An example configuration that should work with most environments is shown below. Copy and paste the content below into the file above and save.

server {
    listen 80;


    root /var/www/;

    index index.html;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

Once the file is saved, you can go and enable it to become a working server block.

To enable the new server block configuration file, you use the symbolic link command which 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/ /etc/nginx/sites-enabled/

After that, run the commands below to restart Nginx service.

sudo systemctl restart nginx

Once Nginx is restarted, you now browse to the server hostname or IP address and it should display the content file we created above.

Repeat this for other domains and server block configuration files you want to create. You can create as many virtual host file as you can as long as the server is able to run them all.

That should do it!


This post showed you how to create Nginx server block on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.