How to setup the Nginx web server with Nginx server blocks on Ubuntu 18.04

The Nginx web server is one of the most famous and used web servers in the world. It is a free and open source. It is primarily used as a reverse-proxy. It can also be used as a load balancer, HTTP cache, and mail proxy.

In this guide, we will learn how to setup Nginx Server with Nginx server blocks to host multiple web applications.

Prerequisites

· Root privileges

· Nginx web server

Step 1: Firewall Setting

The first step is to make our web server accept incoming requests to serve the pages. The requests will come from client machines. For this purpose, we have to configure the firewall to allow incoming request traffic through the port 80 which is the default one and port 443 if we want to use the https protocol.

First, we will check the status of the firewall. The Ubuntu command that we need to accomplish this will be as follows.

$ sudo ufw status

If it is inactive, we will activate it through the following command.

$ sudo ufw enable

To allow incoming connections through port 80, the following command will serve the purpose.

$ sudo ufw allow 80/tcp

If we need to allow 443 instead of 80, the following command will serve the purpose.

$ sudo ufw allow 443/tcp

Next, we will check the status of the firewall. The following command will serve the purpose.

$ sudo ufw status numbered

Status: active

         To                             Action          From

         --                             ------          ----

[ 1] 443/tcp                        ALLOW IN        Anywhere

[ 2] 80/tcp                         ALLOW IN        Anywhere

nameservers:

        addresses: [8.8.4.4, 8.8.8.8, 2.2.0.0, 1.1.1.1]

Step 2: Configure Nginx Default Configuration File

The Nginx server blocks are configured and used to run more than one website on the same server machine. By default, there is only one server block at the time of installation of Nginx web server. It is found in the path /etc/nginx/sites-available/default. The following is the default file under sites-available.

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
            listen 80 default_server;
            listen [::]:80 default_server;
            root /var/www/thedefaultserver.com;
            # Add index.php to the list if you are using PHP
            index index.html;
            server_name _thedefaultserver.com;
            location / {
                    # First attempt to serve request as file
try_files $uri $uri/ =404;
            }
}

Step 3: Configure Nginx Server Blocks

Next, we will create a custom block so that we can host one more site using the same server machine. We will create a directory which will be root for the server block. Let us name it block1. The following command serves the purpose.

$ sudo mkdir /var/www/block1.com

Next, we will create an index.html page for this block. The following command will serve the purpose.

$ echo "Welcome to Block 1!" | sudo tee /var/www/block1/index.html > /dev/null

Next, we will create the server block in /etc/nginx/sites-available directory and call it block1. It will have server block file having the server name, location, root and index. The file name is block1. The path containing file is /etc/nginx/sites-available/block1. We can use any editor to create this file.

The following is block1 server block file

server {
        listen 80;
        listen [::]:80;
        root /var/www/block1.com;
        index index.html;
        server_name block1.com;
        location / {
            try_files $uri $uri/ =404;
        }
}

Next, we need to create a symbolic link from file to the sites-enabled directory. It is checked by Nginx during startup. The following command servers the purpose.

$ sudo ln -s /etc/nginx/sites-available/block1.com /etc/nginx/sites-enabled/

Step 3: Restart Nginx Server

We need to restart the server to apply the change of a new server block to serve web application on the same server machine. The following command serves the purpose

$ sudo systemctl restart nginx

Now this server is able to serve websites thedefaultserver.com and block1.com.

In this way, we can add multiple server blocks according to our requirement.

Conclusion

In order to run multiple websites on a single Ubuntu 18.04 server using Nginx web server, we need to configure Nginx server blocks with their corresponding details. It includes a different path, HTML pages, root directory and server block file. After successful configuration, more than one websites will be accessed by the client machine from a single web server.

Was this article helpful?