Configuring a virtual host with Nginx

In a previous article we talked about how to perform the installation and initial configuration of the web server Nginx in CentOS 7. This web server has gained immense popularity due to its high performance and good architecture of the program for which such performance became possible.

One of the main features of the web server – serving multiple sites on one IP address and one program. This function is implemented by using virtual hosts. In this article we’ll look at how you are configuring virtual host in Nginx. Before reading the article further, I recommend to review the Nginx configuration, to understand the General syntax of the configuration file.

Setup virtual host Nginx

Generally, Nginx is only one configuration file is /etc/nginx/nginx.conf. All the other files in the folder /etc/nginx/* connect to this file using the include Directive. Therefore, theoretically, all virtual hosts or only some of them can be placed in this file. However, it is not recommended.

For this there is already a folder /etc/nginx/sites-available/ and /etc/nginx/sites-enabled. The first just contains configuration files, each of which is a separate virtual host. The second folder contains links to the files in /etc/nginx/sites-available and connected to the main configuration file. Even if your system until such structure is not used, I recommend to create that configuration has always been the procedure.

1. Syntax virtual host

Each virtual host is like this block of code:

server {
listen ip_address:port;
server_name dominion;
root /path/to/files/site/;
index index.php index.html;
location / {}

In addition, there can be used and other instructions, but these are basic and necessary.

  • listen – specifies the IP address and the port on which the program will wait for connections from this site. To choose any IP address, you can specify the asterisk and the port must be specified. Also in this line you can add a parameter default_server, then this virtual host will be used by default.
  • server_name – the domain names which will respond to the host. When sending a request to the server, the browser specifies which domain it addresses. Nginx parses this parameter and selects the correct virtual host. To handle all domains, use the underscore _;
  • root – the path to the files of the website that will open when the request to this virtual host. From Nginx needs to be read access to all folders this way;
  • index files that will open if the file address is not specified in the URL;
  • location is a set of rules for processing the paths in the url. Each location may contain an URL path and inside it you can configure the opening of another file, the authentication request to another server, and other such things. Nginx parses all location in the configuration file and selects the most appropriate. This rule has one exception. If multiple location contain regular expressions, then processing is selected, the first matching.

2. Virtual host default

Now let us examine the creation of virtual hosts with nginx for example. Let’s create a virtual host, which will process all pending requests:

sudo vi /etc/nginx/sites-available/000-default.conf

server {
listen *:80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.html index.htm;
location / {}

All directives used in the server block can be used in location blocks. But we don’t have to specify root and index in each location. If omitted, will be inherited by those that were specified in the parent block. Blocks server behave similarly, so if we don’t specify an alternate path to access.log, it will use the path specified in /etc/nginx/nginx.conf and so on.

Now we need to activate the created virtual host with nginx. To do this, create a symbolic link:

sudo ln-s /etc/nginx/sites-available/000-default.conf /etc/nginx/sites-enabled/000-default.conf

Then make sure that the files in this directory connected in the main configuration file:

sudo vi /etc/nginx/nginx.conf

Then run this command to make sure that you are not allowed error:

nginx -t

Then re-read the nginx configuration:

nginx -s reload

Now, if you open the IP address of the server, you will see we have created virtual host.

2. Virtual host domain

Similarly, you can create a virtual host for the domain. For example

sudo vi /etc/nginx/sites-available/example.conf

server {
listen *:80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {}

If you are working on a local machine and access the DNS for the selected domain you have there, you have to add its IP in the /etc/hosts file:

sudo vi /etc/hosts

Repeat the procedure activation domain, and then in the browser when you request the domain opens start page with Nginx. If for any reason the virtual host does not work with Nginx you can watch full the compiled nginx.conf:

nginx -T

You can also check if there are any configuration desired host for example, you mention

nginx -T | grep

3. Disabling a virtual host

Due to the structure of the directories we used will be fairly easy to disable any unwanted host. All of our Nginx virtual hosts are in the folder /etc/nginx/sites-available, and in the active folder only links to these files. Therefore, for removal sufficient to remove a link to it from the folder /etc/nginx/sites-enabled/:

rm /etc/nginx/sites-enabled/001-example.conf

And then, if necessary, we can activate it back by simply creating a link.


In this article, configuring a virtual host with Nginx. As you can see, it’s pretty simple and very convenient, especially if you need to have multiple sites on one machine. Of course, Nginx is no such convenient utilities for activation sites as in Apache, but the work is quite possible.


(Visited 50 times, 1 visits today)