YouTube Icon

Code Playground.

How To Set Up Nginx Server Blocks on Ubuntu 18.04


How To Set Up Nginx Server Blocks on Ubuntu 18.04

Nginx Server Blocks permits you to run more than one site on a solitary machine. With Server Blocks, you can indicate the webpage record root (the catalog which contains the site documents), make a different security strategy for each website, utilize diverse SSL endorsements for each website, and substantially more. 

In this article, we'll give a bit by bit guidelines about how to set up Nginx worker blocks (like Apache Virtual Hosts) on Ubuntu 18.04. 


Ensure that you have met the accompanying essentials prior to proceeding with the instructional exercise: 

You have a space name highlighting your public worker IP. We will utilize 

You have Nginx introduced by adhering to these guidelines . 

You are signed in as a client with sudo advantages . 

In some documentation, you'll see Server Blocks being alluded to as a Virtual host. A virtual host is an Apache expression. 

Create the Directory Structure

The report root is where the site records for a space name are put away and served because of solicitations. You can set the report root to any area you need. 

We will utilize the accompanying index structure: 

?   ??? public_html
?   ??? public_html
?   ??? public_html

Fundamentally, we will make a different index for every area we need to have on our worker inside the/var/www catalog. Inside every one of these indexes, we will make a public_html registry that will store the area site records. 

We should make the root index for our space 

sudo mkdir -p /var/www/

For testing purposes, we will make an index.html record inside the area's archive root catalog. 

Open your supervisor and make the demo record: 


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

In this model, we are running the orders as a sudo client and the recently made records and indexes are possessed by the root client. 

To keep away from any authorization issues, change the responsibility for area archive root catalog to the Nginx client (www-information): 

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

Create a Server Block

As a matter of course on Ubuntu frameworks, Nginx worker blocks setup documents are put away in/and so on/nginx/destinations accessible registry, which are empowered through representative connects to the/and so on/nginx/locales empowered/catalog. 

Open your proofreader of decision and make the accompanying worker block document: 


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;

You can name the arrangement record as you like however normally it is ideal to utilize the space name. 

To empower the new worker block document, make a representative connection from the record to the destinations empowered registry, which is perused by Nginx during startup: 

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

Test the Nginx arrangement for right language structure: 

sudo nginx -t

In the event that there are no mistakes, the yield will resemble this: 

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx administration for the progressions to produce results: 

sudo systemctl restart nginx

At long last, to confirm that the worker block is filling in true to form, open in your program of decision, and you will see something like this: 


You have figured out how to make a Nginx worker block setup to have different spaces on a solitary Ubuntu worker. You can rehash the means we delineated above and make extra worker blocks for every one of your areas. 

In the event that you need to protect your site with a free LetsEncrypt SSL authentication, you can check the accompanying aide: 

Secure Nginx with Let's Encrypt on Ubuntu 18.04 

In the event that you are confronting any issue, don't hesitate to leave a remark.