How to Configure Nginx Server Blocks on Ubuntu 18.04/20.04

Setup Nginx Server Blocks Ubuntu

In Nginx, server blocks are used to define specific domain information. It helps to host multiple domains or websites. Each block contains specific domain information such as document root, SSL, port, and more. Nginx server blocks are similar to Apache virtual hosts.

In this tutorial, we learn how to configure Nginx server blocks on Ubuntu 18.04/20.04.

Prerequisites

Requires a Ubuntu 18.04/20.04 instance. The instance should have Nginx installed. Require a user with sudo access.

Step 1. Create the Directory Structure

The document root is the directory where the website files for a domain name are stored and served in response to requests. We can set the document root to any location we want but in this article, we will use the following directory structure:

/var/www/
├── domain1.com
│   └── images
│   └── index.html
│   └── style.css
├── domain2.net
│   └── images
│   └── index.html
│   └── style.css

Basically, we will create a separate directory for each domain we want to host on our server inside the /var/www directory.

Let’s create the root directory for our domains:

$ sudo mkdir -p /var/www/domain1.com

For testing purposes, we will create an index.html, style.css files, and images directory inside the domain’s document root directory.

$ cd /var/www/domain1.com/
/var/www/domain1.com$ sudo mkdir images index.html style.css

Open our editor and create the demo file:

/var/www/domain1.com/index.html
<!DOCTYPE html> <html lang="en" dir="ltr">

<head> <meta charset="utf-8">

<title>Welcome to domain1.com</title> <link rel="stylesheet" href="style.css"/>

</head>

<body> <h1 style="color:green;text-align:center;">Success! domain1.com home page!</h1>

</body>

</html>

Here we are running the commands as sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we can change the ownership of the domain document root directory to the Nginx user (www-data):

$ sudo chown -R www-data: /var/www/domain1.com

we will do the same procedures to the domain2.net

Step 2. Create Nginx Server Block Files for Each Domain

Now that we have the content we wish to serve, we need to actually create the server blocks that will tell Nginx how to do this.

By default, Nginx contains one server block called dfault which we can use as a template for our own configurations. We will begin by designing our first domain's server block, which we will then copy over for our second domain and make the necessary modifications.

1) Create the First Nginx Server Block File

As mentioned above, we will create our first server block config file by copying over the default file:

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/domain1.com

Now, open the new file you created in your text editor with sudo privileges:

$ sudo nano /etc/nginx/sites-available/domain1.com

After editing the file, it will look similar to this:

/etc/nginx/sites-available/domain1.com
server { 

listen 80; 

listen [::]:80; 

root /var/www/domain1.com; 

index index.html index.htm index.nginx-debian.html; 

server_name domain1.com www.domain1.com; 

location / 

    { try_files $uri $uri/ =404; 

  } 
}

That is all we need for a basic configuration. Save and close the file to exit Ctrl + x.

2) Create the Second Nginx Server Block File

Now that we have our first Nginx server block configuration, we can use that as a basis for our second file. Copy it over to create a new file:

$ sudo cp /etc/nginx/sites-available/domain1.com /etc/nginx/sites-available/domain2.net

Open the new file with sudo privileges in your editor:

$ sudo nano /etc/nginx/sites-available/domain2.net

Adjust the root directive to point to our second domain's document root and adjust the server_name to match your second site's domain name (make sure to include any aliases).

When you are finished, our file will likely look something like this:

/etc/nginx/sites-available/domain2.net
server { 

listen 80; 

listen [::]:80; 

root /var/www/domain2.net; 

index index.html index.htm index.nginx-debian.html; 

server_name domain2.net www.domain2.net; 

location / 

{ try_files $uri $uri/ =404; 

  } 
}

Save and close the file to exit Ctrl + x.

Step 3. Enable our Nginx Server Blocks and Restart Nginx

Now that we have our server block files, we need to enable them. We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.

We can create these links by typing:

$ sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/domain2.net /etc/nginx/sites-enabled/

These files are now in the enabled directory, We now have three server blocks enabled:

1. domain1.com: Will respond to requests for domain1.com and www.domain1.com
2. domain2.net: Will respond to requests for domain2.net and www.domain2.net
3. default: Will respond to any requests on port 80 that do not match the other two blocks.

In order to avoid a possible hash bucket memory problem that can arise from adding additional server names, we will go ahead and adjust a single value within our /etc/nginx/nginx.conf file. Open the file now:

$ sudo nano /etc/nginx/nginx.conf

Within the file, find the server_names_hash_bucket_size directive. Remove the # symbol to uncomment the line:

/etc/nginx/nginx.conf
http { . . . server_names_hash_bucket_size 64; . . . }

Save and close the file when you are finished.

Next, test to make sure that there are no syntax errors in any of our Nginx files:

$ sudo nginx -t

If no problems were found, restart Nginx to enable our changes:

$ sudo systemctl restart nginx

Nginx should now be serving both of our domain names.

Step 4. Verify by Accessing the website

Now that we are all set up, we should test that our server blocks are functioning correctly. We can do that by visiting the domains in our web browser:

http://domain1.com
domain1

You should see a page that looks like this:

If you visit your second domain name, you should see a slightly different site:

http://domain2.net
domain2

Conclusion

In this tutorial, we learned how to configure the Nginx server block on Ubuntu 18.04/20.04. Using Nginx server blocks we have set up two domains to serve web requests.

Leave a Comment