How To Set Up nginx Virtual Hosts (Server Blocks) on CentOS 6.7



When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server.

In this guide, we’ll discuss how to configure server blocks in Nginx/1.10.0 on an Centos 6.7 server.



You will also need to have Nginx installed on your server. If you want an entire LEMP (Linux, Nginx, MySQL, and PHP) stack on your server, you can follow our guide on setting up a LEMP stack in Centos 6.7. If you only need Nginx, you can install it by typing:


Creating Name-based Virtual Hosts in Nginx

For demonstration purposes, we’re going to set up two domains with our Nginx server. The domain names we’ll use in this guide are and

As I’m sure you already know, a virtual host is a website that is served by Nginx in a single cloud VPS or physical server. However, in the Nginx docs you will find the term "server blocks" instead, but they are basically the same thing called by different names.

The first step to set up virtual hosts is to create one or more server blocks (in our case we will create two, one for each dummy domain) in the main configuration file (/etc/nginx/nginx.conf) or inside /etc/nginx/sites-available.

Although the name of the configuration files in this directory (sites-available) can be set to whatever you want, it’s a good idea to use the name of the domains, and in addition we chose to add the .conf extension to indicate that these are configuration files.


1. Configuring Virtual Host Directories

Creating virtual host directories under /etc/nginx.

Add following line of code to /etc/nginx/nginx.conf file at the end, before closing (http block) tag.

For reference see below of code



2. Creating Website Directory

Creating a directory structure for your website under /var/www/vhost/. In my case I used This way you can create for your real site.



3. Creating Virtual Host File

Add the following lines of code to /etc/nginx/sites-available/ file. This is a basic virtual host config file.


4. Linking Virtual Host

Linking virtual host to /etc/nginx/sites-enabled.


5. Restarting Nginx Service

We need here restart for Nginx service to reflect all the configuration which we have done above.


6. Testing Nginx

Create a file called phpinfo.php under /var/www/vhost/ and add the following lines of code to it. For example (/var/www/vhost/

Now access the page with web browser using http://your_virtual_hostname/phpinfo.php and verify all versions.


Adding More Virtual Hosts

To create additional virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate new domain name each time.

That’s It! for now, If you’re having any problems in installation and configuration, please feel free to ask your questions using our comment form below.

How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6.7



  1. linode-docs