How to Create Nginx Virtual Host (Server Block)

Learn how to run multiple websites on one server.

How to Create a Virtual Host in Nginx

Server blocks, often referred to as virtual hosts in Nginx allows you to host multiple websites on one server. Here’s how to use them.

Table of Contents

Nginx is designed to handle multiple domains on a single server and IP address. In other words, If you have a single server that is used as web server and it has allocated one IP address only, then in order to have more than one website or domain running on the server, you use Nginx virtual hosts.

Before you begin this tutorial, you’ll need to install Nginx. To learn how, read How to Install and Configure Nginx Web Server.

What is Virtual Host?

If you’re not sure what a virtual host is, let me explain. It’s an Apache HTTP Server term, however, is commonly used by Nginx users as well. In fact, the proper term for Nginx is server block.

Related: Apache vs Nginx: Which Web Server You Should Choose

Virtual host is a method for hosting multiple domain names on a single server.

How Does Virtual Host Works?

When you start web server software like Nginx on a server, that web server is going to bind itself to one or more network ports. Usually for a web server that’s going to be ports 80 and 443.

Now when a computer goes to visit a website it’s going to send a request over the internet to the server and the server is going to receive that request. First thing the server is going to do is going to look at the port that the request was sent to the destination port.

Next it’s going to use that destination port to determine what application to forward that information along to. What that means is that your web server can only have one application that is receiving the requests.

But what if you want to host two different websites on the same web server? The server needs to be able to tell when a packet comes in requesting for the website. It needs to be able to tell what website is being requested so that it knows what information to send back.

No matter what website is requested, the server can’t use the port number alone the destination port to determine which website a client is trying to access. All the server is going to do is to say “I’m sending this request to Nginx”, and from there Nginx has to decide what it’s going to send back.

Nginx has a concept called server blocks. The server blocks can actually be used for two different things. They can be used to serve different websites based on the IP address that’s being requested or they can be used to serve different websites based on the domain name that’s being used.

How Does Nginx Virtual Host Works

So when you type in the name of the website like www.example.com and when the server receives that, Nginx can actually look inside of the HTTP headers at what server name was requested and from there, based on its configured virtual hosts, to make decision which directory stores the website files, and to return the correct response back to the client based on the server name.

How to Create Nginx Virtual Hosts (Server Blocks)

There are two directories in which we can create and store our virtual host configuration files. Both of them are located inside /etc/nginx directory. The name of those two directories are sites-available and sites-enabled.

  • /etc/nginx/sites-available: The directory where individual virtual hosts (websites) are defined. Keep in mind that Nginx will not use the configuration files found in this directory unless they are linked to the /etc/nginx/sites-enabled directory.
  • /etc/nginx/sites-enabled: List of the virtual hosts (websites) actively being served by Nginx.

You are probably wondering why do we need two directories to store our virtual host files? The main benefit of this is that we can create many virtual hosts files on our Nginx server but we can enable them as per our requirements.

Now, we will create virtual host files to host multiple sites with multiple domains in our Nginx server. The domain names are website1.com and website2.com.

1. Creating the Directory Structure

The first thing we need to do is create a directory for each virtual host to store the different website content in. We will place the directories in /var/www and we will name them after each domain name.

Run the following commands to create the directories:

sudo mkdir -p /var/www/website1.com
sudo mkdir -p /var/www/website2.com
Nginx Virtual Host Directory Structure

The -p flag in the command above tells mkdir to create any necessary parent directories along the way.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

2. Creating Test Pages for Each Website

Next, for demonstration purposes, let’s just create an index.html file inside the domain’s document root directory so that we will have something to display.

sudo vim /var/www/website1.com/index.html

Paste the following lines:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to Website 1!</title>
    </head>
    <body>
        <h1>Welcome to Website 1!</h1>
    </body>
</html>

Now repeat the above step for website2.com.

sudo vim /var/www/website2.com/index.html

Paste the following lines:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to Website 2!</title>
    </head>
    <body>
        <h1>Welcome to Website 2!</h1>
    </body>
</html>

3. Create Nginx Virtual Host (Server Block)

To create a Nginx virtual host file inside the sites-available directory, execute the following command:

sudo vim /etc/nginx/sites-available/website1.com

It’s important to note that the Nginx virtual host configuration file can be named anything you want, but usually, it’s a good practice to use the domain name.

Now, paste the following content in your virtual host file:

server {
    listen 80;
    server_name website1.com www.website1.com;
    root /var/www/website1.com;

    access_log /var/log/nginx/website1-access.log;
    error_log /var/log/nginx/website1-error.log;
}

To make things clearer, let’s break down this syntax. Here is what each parameter means:

  • listen: Specify on which port the Nginx server to listen for connections.
  • server_name: Here we match requests for our domain. The directive holds a list of domain names that we want to use to access our site.
  • root: The location of the directory in which we have stored our website files.
  • error_log: Full path to the log file in which we store all the error logs of this virtual host.
  • access_log: Full path to the log file in which we store all the access logs of this virtual host.

Now we can use our initial virtual host configuration as a basis for our second file. Just copy it over to create a new file:

sudo cp /etc/nginx/sites-available/website1.com /etc/nginx/sites-available/website2.com

Open the new file and adjust it to match our second (website2.com) domain name:

sudo vim /etc/nginx/sites-available/website2.com
server {
    listen 80;
    server_name website2.com www.website2.com;
    root /var/www/website2.com;

    access_log /var/log/nginx/website2-access.log;
    error_log /var/log/nginx/website2-error.log;
}

4. Enable Nginx Virtual Hosts (Server Block)

The last thing we need to do before testing is to enable the virtual hosts. We can do this by creating symbolic link from the file to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/website1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/website2.com /etc/nginx/sites-enabled/

As you can see from the image below these files are now linked into the sites-enabled directory.

Linked Nginx Configuration Files Into sites-enabled Directory

Before we go forward, let’s double check that there aren’t any typos. Test the Nginx configuration for correct syntax:

sudo nginx -t

And finally, if no problems were found, restart the Nginx service to apply changes:

sudo systemctl restart nginx

5. Testing the Virtual Hosts (Optional)

In order to test the Nginx virtual hosts we will first need to point our domain name records at the IP address of the server. In our case the server IP address is 192.168.122.101.

If you are on Linux, open the /etc/hosts file on your desktop PC (the one from which you will perform the test) using the below command:

sudo vim /etc/hosts

And add the virtual domain names one by one as shown below.

/etc/hosts

Save and close the file when you are finished. Proceeding in this way will allow us to test domains that we don’t actually own.

Now open your browser and enter www.website1.com and www.website2.com one by one. You will see the index pages of both domains respectively.

Nginx Virtual Hosts Test

You have successfully configured two independent domains with Nginx.

6. Disable Nginx Virtual Hosts (Server Block)

To disable a virtual host in Nginx, we just have to remove the symlink we had created while enabling the virtual host file. For example, to disable website2.com, we just have to execute the following command:

sudo rm -rf /etc/nginx/sites-enabled/website2.com

To apply the changes we have to restart the Nginx server:

sudo systemctl restart nginx

Conclusion

In this article I have shown you how to create Nginx virtual hosts (server blocks) and host multiple domains on a single Linux server. To create a virtual host for another domain, just repeat the same steps. Of course, Nginx can do a lot more.

Related: How to Configure Nginx to Work with PHP via PHP-FPM

In addition, if you want to know how to secure your website with a free SSL certificate, I recommend going through our guide Let’s Encrypt: Get Free SSL Certificate Using Certbot.

For more information on setting up a Nginx server, you can refer to the official Nginx documentation.

Thanks for reading! I hope you found the article helpful and you were able to setup the virtual hosts. Feel free to leave a comment if you have any questions.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge