How to Create Nginx Virtual Host (Server Block)

How to Create Nginx Virtual Host (Server Block)

Server blocks, often referred to as virtual hosts in Nginx, allow 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 a web server and it has allocated one IP address only, then 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 to install it, read our excellent guide on Installing Nginx Web Server on Linux.

What is a Virtual Host?

If you’re not sure what a virtual host is, let me explain. It’s an Apache HTTP Server term; however, it is also commonly used by Nginx users. The proper term for Nginx is server block.

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

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

How Does Virtual Host Works?

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

When a computer goes to visit a website, it will send a request over the internet to the server, and the server will receive that request. So first thing the server is going to do is look at the port that the request was sent to the destination port.

Next, it will use that destination port to determine what application to forward that information. That means that your web server can only have one application receiving the requests.

But what if you want to host two different websites on the same web server? The server needs to tell when a packet comes in requesting the website. Then, it needs to know what website is being requested to know 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 say, “I’m sending this request to Nginx.” From there, Nginx has to decide what to send back.

Nginx has a concept called server blocks. The server blocks can be used for two different things.

They can be used to serve other websites based on the IP address requested, or they can be used to serve different websites based on the domain name 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 look inside of the HTTP headers at what server name was requested and from there, based on its configured virtual hosts, to make a decision which directory stores the website files, and to return the correct response 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 the /etc/nginx directory. The names of those two directories are sites-available and sites-enabled.

  • /etc/nginx/sites-available: The directory where individual virtual hosts (websites) are defined. Remember 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.

Do you probably wonder why 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.

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. 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.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

2. Creating Test Pages for Each Website

Next, for demonstration purposes, let’s 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 Blocks)

To create an 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 more transparent, let’s break down this syntax. First, 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 Blocks)

The last thing we need to do before testing enables the virtual hosts. We can do this by creating a 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)

To test the Nginx virtual hosts, we first need to point our domain name records at the server’s IP address. 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. This way will allow us to test domains that we don’t 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 Blocks)

To disable a virtual host in Nginx, we have to remove the symlink we had created while enabling the virtual host file. For example, to disable website2.com, we 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

This article shows 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, repeat the same steps. But, 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 an 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 set up the virtual hosts. Feel free to leave a comment if you have any questions.

Leave a Reply

Your email address will not be published.