Skip to content

Intro

When using the Nginx web server, Server Blocks (similar to Apache's virtual hosts) can be used to encapsulate configuration information and host more than one domain from a single server.

By default, Nginx in Ubuntu 22.04 has one server block configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites.

Instead of changing the format of /var/www/html, let's create a directory structure under /var/www for test.local and leave /var/www/html as the default directory to be displayed if the client's request does not match any existing site.

New server block for Nginx on filesystem

Create the directory test.local for your test-domain as follows, using the -p flag to create the necessary top-level directories:

Bash
sudo mkdir -p /var/www/test.local/html
Next, assign ownership of the directory to www-data, the user and group that Nginx runs as:
Bash
sudo chown -R www-data:www-data /var/www/test.local/

Set file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute files while granting only read and execute permissions to groups and others, enter the following command:

Bash
sudo chmod -R 770 /var/www/test.local
Create an example index.html page using the nano editor:

Bash
nano /var/www/test.local/html/index.html

Inside, add the following sample HTML:

Bash
<html>
    <head>
        <title>Welcome to Test.local!</title>
    </head>
    <body>
        <h1>Success!  The Test.local server block is working!</h1>
    </body>
</html>
There is still one more step before the server block can be used. Run Command:
Bash
ls /var/www/test.local 

The file just created is owned by root or your own user account, so it is necessary to change the ownership of the file to www-data:

Bash
sudo chown www-data:www-data /var/www/test.local/html/index.html

Nginx settings to run new server block

In order for Nginx to serve this content, it is necessary to create a server block with the correct instructions. Instead of editing the default configuration file directly, let's create a new file at /etc/nginx/sites-available/test.local:

Bash
sudo nano /etc/nginx/sites-available/test.local
Paste the following configuration block, similar to the default, but updated for the new directory and domain:

Nginx Configuration File
server {
        listen 80;
        listen [::]:80;

        root /var/www/test.local/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test.local www.test.local;

        location / {
                try_files $uri $uri/ =404;
        }
}

Next, let's enable the file by creating a link from it to the sites-enabled directory, which Nginx reads during startup:

Bash
sudo ln -s /etc/nginx/sites-available/test.local /etc/nginx/sites-enabled/
Note: Nginx uses a common practice called symbolic links or symlinks to keep track of which of your server blocks are in use. Creating a symlink is like creating a shortcut on disk, so you can later remove the shortcut from the sites-enabled directory and keep the sites server block available if you want to enable it.

Two server blocks are now enabled and configured to respond to requests based on their listen and server_name commands:

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

Bash
sudo nginx -t

Restart Nginx to enable your changes:

Bash
sudo systemctl restart nginx

Spoof DNS to test server block

There are multiple different ways to make the server block work. In this demo, we will use the hosts file to spoof the DNS server. This is native way in almost all operating systems the difference is in the location of the hosts file.

  • Windows hosts file location: C:\Windows\System32\drivers\etc\hosts
  • Linux hosts file location: /etc/hosts
  • Mac hosts file location: /private/etc/hosts

We could open a editor and add the following line to the hosts file:

Text Only
Google_cloud_ip test.local
or
127.0.900.1 test.local
But we are little bit lazy and we will use Powertoys tool to do this for us. Open Powertoys and locate "Hosts file editor" add previous line to the hosts file

Host file editor

Save and close the file Test the operation of the server-block by navigating to website on Demo-ubuntu workstation

Bash
http://test.local