NGINX is a powerful and flexible web server that any business can benefit from. Like Apache’s virtual hosts, NGINX has its own means of serving up multiple websites on a single machine. That means is called server blocks.

Effectively, server blocks are the same as Apache virtual hosts. Although the name might be different, the functionality is the same. As you might expect, the configuration is slightly different as well. I want to show you how to configure these blocks, so you can start serving up more than one site with NGINX.

What you’ll need

  • NGINX properly installed and running on your Ubuntu 18.04 server.
  • Account on your Ubuntu server with sudo privileges.
  • A domain name pointing to the IP address of your NGINX server.

SEE: Wireless networking policy (Tech Pro Research)

I am going to show you how to set up a single server block, but the process is the same for setting up multiple blocks. I’ll use the address as an example. For your setup, make sure to replace with the domain you have pointing to the server.

Creating the necessary directories

The first thing to do is create the necessary directories. Our document root for the base NGINX server is /var/www, so we’re going to create a new document root (for our server block), called Open a terminal window and issue the command:

sudo mkdir /var/www/

With the document root created, create the subdirectory for public HTML with the command:

sudo mkdir /var/www/

Let’s create a file for testing our server block. Issue the command sudo nano /var/www/ Inside that new file, add the following contents:

Hello, TechRepublic! This is

Save and close that file. Next, give the directory the correct ownership with the command:

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

Creating the server block

Now it’s time to create the actual server block. For anyone that’s worked with Apache on Ubuntu, this will be fairly familiar. Create a new file with the command:

sudo nano /etc/nginx/sites-available/

In this new file, add the following content:

server {
listen 80;
listen [::]:80;

root /var/www/;

index index.html;


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

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

Enable the new server block with the command:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Test the configuration file with the command:

sudo nginx -t

You should see the configuration is ok (Figure A).

Figure A

Testing the new block

Before we point our browser to the newly created server block, we must restart NGINX with the command:

sudo systemctl restart nginx

Once the server restarts, point your browser to, and you should see the greeting from the index.html file (Figure B).

Figure B

If your server block doesn’t come up, you might need to remove the default configuration file from sites-enabled with the command:

sudo rm /etc/nginx/sites-enabled/default

Welcome to NGINX server blocks

And that, my friends, is the basics for configuring name-based server blocks in NGINX. It’s not all that much different from creating virtual hosts in Apache. You might have noticed this only addresses name-based server blocks for Ubuntu 18.04. I’ve yet to find if it’s possible to create IP-based server blocks with Ubuntu’s new Netplan network configuration. Hopefully, for those that need to use IP-based server blocks, a solution will be coming soon.