How to create and manage Virtual Hosts in Nginx server

Posted on July 30th, 2019

Nginx is one of the best web servers that you can use to serve your websites from your server. It is faster than Apache and we can customize the configuration as per our requirements. With Nginx, you can also manage multiple sites on your server using Nginx virtual hosts.

In this guide, we will learn almost everything about Nginx virtual hosts. By the end of this guide, which should take less than 10 minutes of your time, you will know how to read, enable, disable and create new virtual host files in Nginx. First of all, Let’s just learn about virtual hosts files in brief.

What are the virtual hosts in Nginx?

Virtual hosts are the configuration files that inform the webserver to route traffic based on the domain name and other parameters and conditions. For example, if you want to host two websites on your server, you have to create two virtual host files that will hold information about your sites like a document root (a directory that holds the website files), the domain name(s), the path of the log files and much more.

The virtual host files in Nginx are very easy to create and manage. It follows the same directory structure as Apache virtual hosts. It means that Nginx too has two directories called sites-available and sites-enabled that holds the virtual host files.

Just like Apache, In Nginx, we have to create our virtual host files in sites-available directory. And to enable a specific virtual host file, we just have to create a symlink of that virtual host file from sites-available  to sites-enabled directory.

Now, we can get started with the actual tutorial. I assume that you already have Nginx installed on your server. If you do not have Nginx already installed on your server, Let’s first do it.

Install Nginx on Ubuntu

You can skip this step if you have already configured Nginx on your server. If not, execute the following set of commands to install Nginx on Ubuntu. It’s a very easy task.

$ sudo apt-get update
$ sudo apt-get install nginx -y

It might take a minute or so to install Nginx, once done, visit the public IP address of your server in your browser to verify the installation. If you can see a page like the following image, Nginx is successfully installed on your server.

Nginx default page

Now, Let’s move on to understand the Nginx directory structure in detail.

Nginx Virtual Host directories

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.

But why do we need two directories to store our virtual host files? Actually, we will store our virtual host files in the sites-available directory only. Then, we will create a symlink of the virtual host file that we want to activate from sites-available to sites-enabled directory.

Nginx will only include files stored inside the sites-enabled directory in the main Nginx configuration. The main benefit we get from this directory structure is, we can create many virtual host files on our server but we can enable them as per our requirements.

For example, Let’s say that there is one website called example.com on our server. We have two virtual host files for this domain name in our sites-available directory. The first virtual host file points to the actual document root in which we have stored our website files. Another virtual host points to the maintenance document root with minimal settings.

Now, whenever we want to put our website on maintenance mode, we can remove the symlink of the main virtual host file from the sites-enabled directory and create a symlink of maintenance virtual host file inside the sites-enabled directory. It’s just a quick example to explain to you why there are two directories to store our virtual host files.

Create Nginx Virtual Hosts

Now, we will create virtual host files to host multiple sites with multiple domains on a single server. In this guide, we will create the simplest virtual host files in which you can understand all the basic directives that are required in a virtual host configuration file.

However, you can learn more directives from Nginx official documentation for advanced configurations. So, let’s assume that we want to host two websites on our Nginx server. The domain names are example1.com and example2.com. And we also want to make sure that our websites are accessible with www. prefix.

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

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

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

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

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

Now, press CTRL+X followed by followed by the Enter key to save the virtual host file. Similarly, you can create another virtual host file for our example2.com site inside the sites-available directory. Let’s understand what these directives mean.

  1. listen: In this directive, we have mentioned a port on which our Nginx server will listen for connections.
  2. server_name: The Nginx server name directive will hold a list of domain names that you want to use to access your site.
  3. root: This directive will contain the location of the directory in which you have stored your website files.
  4. error_log: Enter the location of a log file in which you want to store all the error logs of this virtual host.
  5. access_log: Enter the location of a log file in which you want to store all the access logs of this virtual host.

Once all your virtual host files are ready, you can move on to the next section in which we will learn how to enable and disable sites or virtual host files in Nginx.

Enable/Disable Site or Virtual Hosts in Nginx

Apache provides a command that creates a symlink of our virtual host stored in sites-available to the sites-enabled directory. However, Nginx is a lightweight server. It does not have any in-built command that we can use to create symlinks automatically. So, we have to create symlinks of our multiple sites manually. Let’s first learn how to enable sites in Nginx.

Enable Site in Nginx

To enable a specific virtual host file, we have to use ln command to create a symlink. A symlink is kind of a shortcut to the original file. It means that you can edit your virtual host file inside sites-available directory and the changes will be applied in sites-enabled directory too.

Let’s create a symlink of one of our example site from sites-available directory to sites-enabled directory. Execute the following command to perform this task.

$ ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/example1.com

Just creating a symlink will not enable the website. We also have to restart the Nginx server to apply the changes. Execute the following command to restart the Nginx server.

$ sudo service nginx restart

Now let’s see how to disable site in Nginx.

Disable Site in Nginx

To disable a site in Nginx, we just have to remove the symlink we had created while enabling the virtual host file. We can use the regular rm command to remove the symlink. For example, to disable our example1.com site, we just have to execute the following command.

$ sudo rm -rf /etc/nginx/sites-enabled/example1.com

To apply the changes that we have made, we have to restart the Nginx server. Again, execute the following command to restart the Nginx server to apply the changes.

$ sudo service nginx restart

So, this is how you can enable and disable sites on Nginx.

 

Conclusion: Nginx is one of the best web servers available for Free. And it provides all the features we need to host multiple domains on our VPS. This guide does not cover some directives that are not mandatory in the virtual host configuration. But you can find advanced settings and directives from the Nginx official documentation. Nginx provides lots and lots of features that you can use to further perfect your configuration.

Let us know if you need help setting up Nginx virtual hosts on your server. You can either comment down in the comment section or contact our support department. We will respond with the help as soon as possible.

3 Responses to “How to create and manage Virtual Hosts in Nginx server”

  1. Muhammad SH says:

    Very Very Very Thanks !!! You did a lot for me

  2. Jurgen says:

    Thank you! This helped me a lot.

    You forgot a ‘;’ in the last line:

    server {
    listen 80;
    server_name example1.com http://www.example1.com;
    root /var/www/html/example1.com;

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

  3. SenVolut says:

    Hello and thank you for the nice tutorial.

    It works verry well, if the root directory (/var/www/html/example) is not a symlink.
    My code is in my home directory (dev env) and when I linked it to the var/www-directory, it dosnt work.
    can someone help?
    Thank you

Leave a Reply