How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04

How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server.
In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu 16.04 server.

Step One: Set Up New Document Root Directories

By default, Nginx on Ubuntu 16.04 has one server block enabled by default. It is configured to serve documents out of a directory at /var/www/html.

While this works well for a single site, we need additional directories if we're going to serve multiple sites. We can consider the /var/www/html directory the default directory that will be served if the client request doesn't match any of our other sites.

We will create a directory structure within /var/www for each of our sites. The actual web content will be placed in an html directory within these site-specific directories. This gives us some additional flexibility to create other directories associated with our sites as siblings to the html directory if necessary.

We need to create these directories for each of our sites. The -p flag tells mkdir to create any necessary parent directories along the way:

sudo mkdir -p /var/www/saleheen.xyz/html

Now that we have our directories, we will reassign ownership of the web directories to our normal user account. This will let us write to them without sudo.

sudo chown -R $USER:$USER /var/www/saleheen.xyz/html

The permissions of our web roots should be correct already if you have not modified your umask value, but we can make sure by typing:

sudo chmod -R 755 /var/www

Our directory structure is now configured and we can move on.

Step Two: Create Sample Pages for Each Site

Now that we have our directory structure set up, let's create a default page for each of our sites so that we will have something to display.

Create an index.html file in your domain & put your html codes:

sudo nano /var/www/saleheen.xyz/html/index.html

Save and close the file when you are finished.

Step Three: Create Server Block Files for Your 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.

Create a Server Block file in /etc/nginx/sites-available/
directory called (Your Domain Name)

sudo nano /etc/nginx/sites-available/saleheen.xyz

Now Pase the Following Configuration

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

        root /var/www/saleheen.xyz/html;
        index index.html index.htm index.nginx-debian.html;

        server_name saleheen.xyz www.saleheen.xyz;

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

When you are finished, save and close the file.

Step Four: Enable your 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/saleheen.xyz /etc/nginx/sites-enabled/

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:

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 your Nginx files:

sudo nginx -t

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

sudo systemctl restart nginx

Nginx should now be serving your domain name by typing == http://www.saleheen.xyz ==.