How to create Virtual Host in Nginx 1.9+

Posted on August 7, 2016

Run multiple websites or domains off a single system or system powered by Nginx 1.9.

Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes. Nginx is often selected by administrators for its resource efficiency and responsiveness under load.

In Nginx 1.9+ there is no site-available or sites-enabled directories. The default configurations for the hosts are in the /etc/nginx/conf.d/default.conf directory. All we would need to do is use this default.conf file to create our own Virtual Hosts.

The default root directory for Nginx is /usr/share/nginx/html. We will be creating our projects in this directory to serve our websites.

It’s not necessary to put the projects in the Nginx default directory. Using Virtual Hosts we can place our prpjects anywhere we want. Just put the appropriate path for the root of your project. For eg: If we want to serve our site to serve from “/srv/public_html/site” then this should be the root of your project in the Virtual Host configuration file

Step 1: Create your projects

cd /usr/share/nginx/html

Site One:

mkdir site1
cd sites1
touch index.html
echo "Welcome to Site One" >> index.html

Site Two:

mkdir site2
cd sites2
touch index.html
echo "Welcome to Site Two" >> index.html

Step 2: Add your Virtual host configuration files.

Next we add the Virtual host files which will have the Virtual host infomation for the projects.

sudo cp /usr/nginx/conf.d/default.conf /usr/nginx/conf.d/site1.local.conf

sudo cp /usr/nginx/conf.d/default.conf /usr/nginx/conf.d/site2.local.conf

Step 3: Setup your Virtual Host

Open your Virtual host file for Site 1 and configure it.

sudo vim site1.local.conf

We need to make a couple of changes in this file.

  • Change the server name to access your local project.
  • Change the Document root to serve the project to Nginx.
server {
    listen       80;
    server_name  site1.local;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/site1;
        index  index.html index.htm;

Similarly, setup the Virtual host for Site 2.

server {
    listen       80;
    server_name  site2.local;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/site2;
        index  index.html index.htm;

The index section tell what is the type of the files that is allowed as the project’s index files. For PHP it will be index index.php

Step 4: Restart the Nginx server

You will need to restart the Nginx server to have the changes in effect.

sudo services nginx restart

Step 5: Add in the site entries in hosts file.

For localhosts you will have to add the server_name in the hosts file to access.

sudo vim /etc/hosts

You can add the local hosts details to this file. As long as that line is there, directing your browser toward, say, will give you all the virtual host details for the corresponding IP address.

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##       localhost

# For local virtual hosts       site1.local       site2.local

#Virtual Hosts via a VM or vagrant box.   site1.local   site2.local

The last block can be used when you are woking on a VM or a Vagrant box. So, you can add the ip of the VM along with the host name to point your browser to the desired location and access with the Virtual Host.

Step 6: Access you projects with the Virtual Hosts in the browser.

Now, when you type in http://site1.local it should show up like this.

Site one

For Site Two, enter http://site2.local.

Site two

Creating More Virtual Hosts

To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.