Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

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

SelvakumarSelvakumar

You can host multiple domains on a single server using the Nginx server block.

Since many domains are using the same server, the resource requirement for hosting the server will be reduced.

You have to create a separate server block for Nginx multiple domain configurations.

We will be doing the following tasks on the Nginx server to host multiple domains.

  • Creating new root directories
  • Creating sample pages for testing
  • Creating server block for each domain

Prerequisites

Test Configuration

We are going to use two domains for the testing purpose. Those two domains are example.com and test.com.

But:

You have to use your own domain names for this purpose. Make them point to the server.

Creating New Document Root Directories

Nginx server has a server block by default which will serve the website from /var/www/html.

For single domain, this directory is enough. But for multiple domains, you have to create multiple directories to store website and serving it for the corresponding domain request.

Here:

We will create the new directory under /var/www for all the sites.

We will also place the web content inside the html. It will let us create the sibling directory to the html directory if we require that one.

Use the below code to create the directories under /var/www.

$ sudo mkdir -p /var/www/example.com/html
$ sudo mkdir -p /var/www/test.com/html

Once you create the directory, you have to assign the ownership to the normal user. This will help us in writing in those directories without sudo.

Sometimes you need to change the permission of the directory to make it accessible to the www-data user. It is required for dynamic sites since they need to access the file.

You should fix the permission level based on the configuration and technology you are using.

Here, we will be using $USER to give permission to the user which we logged in currently.

$ sudo chown -R $USER:$USER /var/www/example.com/html
$ sudo chown -R $USER:$USER /var/www/test.com/html

The Webroot permissions will be correct if you did not change the unmask value.

$ sudo chmod -R 755 /var/www

The next steps should be creating the sample web page for each domain.

Sample Page for Domains

Here, we are going to create the sample website for each domain. First, create the index.html file in the corresponding domain.

$ nano /var/www/example.com/html/index.html

Paste the below code in the file.

/var/www/example.com/html/index.html
<html>
<head>
    <title>Welcome to Example.com!</title>
</head>
<body>
    <h1>Success!  The example.com server block is working!</h1>
</body>
</html> 

Then save and close the file. After that, we will be going for the second website creation.

Simply copy the existing website to another file.

$ cp /var/www/example.com/html/index.html /var/www/test.com/html/

We will make some modifications in the second file to use it to create second sample site.

nano /var/www/test.com/html/index.html

Modify the code as mentioned below.

/var/www/test.com/html/index.html
<html>
<head>
    <title>Welcome to Test.com!</title>
</head>
<body>
    <h1>Success!  The test.com server block is working!
</h1>
</body>

After that, save and close the file.

Then, we have to display the correct pages for the correct domain request.

Creating Server Blocks for the Domains

We have created sample websites and we have to serve that for the corresponding domain requests.

We have to create Nginx server blocks for each of the sites. This server blocks will tell Nginx how to serve the site.

Nginx contains one server block called default. We are going to use this to create server blocks for our sample sites.

Creating First Server Block

Copy the default configuration block to create the new server block.

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

After that, open the new file where you have copied the default block using nano editor.

sudo nano /etc/nginx/sites-available/example.com

The file will look like the following

      /etc/nginx/sites-available/example.com
server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    server_name _;

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

The default_server block option is the main thing. It will have the website to serve in case of the requested host is not found on the server.

There is only one server block can have this default_server option enabled.

You can set the website to serve the requests for which, the Nginx server could not find the host.

If you leave it to default in the default server block, then /var/www/html will be served.

Here, in this article, we will be using the default one. So, we have to remove the default option in both of the Nginx server blocks which we have created to serve the domain.

/etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    . . .
}

To check whether there is only one default option among all the Nginx server blocks, use the below command.

grep -R default_server /etc/nginx/sites-enabled/

If matches found, then Nginx will complain the server configuration.

Next, we have to make changes in the document root which is still pointing to the default document root. Make it point to our newly created document root.

/etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;

}

The next changes have to be made at server_name.
This is to match the domain to serve the request.
We can also add aliases to match.

Here, we will add www.example.com alias for testing.

Once you made the modification, the configuration will look like this.

/etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

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

That's all. We have done with the first server block. Let us configure the second server block.

Creating Second Server Block

Here, we will copy the second server block file to create a new file.

Use the below command for that.

 $ sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com

After that, open the copied file using nano editor.

 $ sudo nano /etc/nginx/sites-available/test.com

If you have the default_server option enabled already, make sure to remove that option in this blog.

Also, make the root directive to point to your exact root document for the domain.

Additionally, make changes in the server_name to match it with the second domain name.

/etc/nginx/sites-available/test.com
server {
    listen 80;
    listen [::]:80;

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

    server_name test.com www.test.com;

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

Once you are done with the configuration, save and close it.

Enable the Server Blocks

We have created the Nginx server blocks successfully. Our next task is to create the symbolic link from the server blocks to the sites-enabled directory using nginx sites-enabled symlink command.

This directory will be read by the Nginx server when it starts.

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

We have now enabled three server blocks using nginx enable site command. Each them will respond based on the server_name and listen directive.

Here, let us see which are all the blocks responding to which requests.

  • example.com: This one will respond to example.com and www.example.com

  • test.com: This will respond to test.com as well as www.test.com

  • default: This one will respond to the requests from port 80 which does not match with any other server blocks.

To avoid the hash bucket memory problem, we have to make a change in the Nginx configuration file.

Open the Nginx configuration file using nano editor.

$ sudo nano /etc/nginx/nginx.conf

In that file, find the server_names_hash_bucket_size directive. You have to remove the # symbol from the line to uncomment it.

/etc/nginx/nginx.conf
http {
. . .

server_names_hash_bucket_size 64;

. . .
}

Then save and close the file. After that, check the syntax using the below command.

$ sudo nginx -t

If there is no problem in the syntax, then restart the Nginx.

 $ sudo systemctl restart nginx

You have configured the Nginx to serve the request for both domain names.

Test the Nginx Server block from Local System

If you have used dummy names for the domain, then you can test the Nginx server block from your local system.

For that, you should edit your local system host file and make it point the IP address of your Nginx server for the given dummy domain name.

If you are the Linux user, you can open this by using below command.

local$ sudo nano /etc/hosts

You have to find the server's Public IP address and use that in this temporary setup in the local system.

If the server's Public IP address is 102.153.17.35, you have to make modification in the host file as mentioned below.

     /etc/hosts
127.0.0.1   localhost
. . .

102.153.17.35 example.com www.example.com
102.153.17.35 test.com www.test.com

These instructions will send the requests for the mentioned dummy domain request to your server IP address.

Once you are done with this, save and close the file.

Testing the Nginx

To test the server configuration, you can visit both the domains from your browser.

http://example.com

If you enter the above URL, you will get the following message displayed on your browser.

Success! The example.com server block is working!

http://test.com

If you enter this URL, you will get the below message displayed on the browser.

Success! The test.com server block is working!

If you get the above messages, then you have configured the server blocks successfully.

Once you are done with testing, you have to revert the changes that you have made on your local system host file.

Conclusion

Now, you have successfully setup the Nginx server block. You can do the same for more than two domains.

Make sure you do things accordingly. If you have any doubt, then let us know that in the comment.

We will help you. Also, make sure to signup for the upcoming tutorials.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments