How to create Virtual Hosts in Nginx server

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 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 and 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/

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

server {
    listen 80;
    root /var/www/html/;

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

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.


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/ /etc/nginx/sites-enabled/

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.


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 site, we just have to execute the following command.

sudo rm -rf /etc/nginx/sites-enabled/

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.


Leave a Comment

Your email address will not be published. Required fields are marked *