NGINX Virtual Host: Run Websites on a Single Server

nginx virtual host cloudNginx is a reverse open source proxy server for IMAP, POP3, SMTP, HTTPS and HTTP protocols. It is also a web server, an HTTP cache and a load balancer. Nginx can be deployed for serving HTTP dynamic content on the networks using Phusion Passenger module, WSGI application servers, SCGI script handlers or FastCGI.

The project of nginx began with a strong focus on low memory usage, high performance and high concurrency. It runs on HP-UX, AIX, Solaris, Mac OS X, BSD Variants, Linux and other *nix flavors. There is also a Microsoft Windows proof of concept port.

Virtual hosts such as nginx are used for running two or more domains or websites using just one server which you can learn more about in this hosting fundamentals course. On nginx, ‘server blocks’ are what virtual hosts are called.

In this tutorial, we will be referring to them as virtual hosts for easy comparison with Apache, which you can master with this Apache course or read more about in this Apache tomcat article.

As a server platform, nginx is efficient, light weight and speedy. This makes it a web server favorite among webmasters. This is especially true when hosing websites that Magento and WordPress which are based on CMS.

Years ago, there were Apache virtual host systems and Apache servers controlled by .htaccess files. When you set up a virtual host using nginx, you can get this done faster when you use nginx server blocks AKA virtual hosts.

Setting Up

These steps require users on virtual private servers (VPS) to have root privileges. On the virtual private servers, you will need to install nginx by downloading it:


Creating a New Directory

When creating virtual hosts, the first step is creating a place where you can keep all the information of the new website. In this case, you will be creating a directory. This then becomes your document’s root in the virtual configuration file of Apache. You can automatically generate parents for the new directory by the addition of a –p to the code line:


To test that the virtual host works, you will need to designate an IP address or an actual DNS approved domain. In this case, we will use to hold the place of the right domain name.

Granting Permission

Rather than just keeping it on the root’s systems, you will need to grant directory ownership to the right user. You can replace the ‘www-date’ with the correct username:


It is also important to ensure that all persons will be able to start reading the new files. At this point, you have granted permission:


Page Creation

Within the directory you made earlier, you will need to make a new index.html file:


So you can see something when the site begins redirecting you to the virtual host, you will need to add text to the file:


Save this and exit

Creating Newer Virtual Host Files

You can now begin creating new files that have all the information of your virtual host. For this file, nginx provides you with the file layout:


Setting Up Virtual Hosts

All the necessary information you need to set up the virtual hosts within can be seen when you open up the new virtual host file:


There are a few changes you need to make with these lines:


Change the server’s name to your approved DNS domain name. You can use your IP address if you don’t have one.

Remember the directory you created in the first step? Change the root extensions to match this. If you have an absent or incorrect document root, you won’t have the ability to get the virtual host set up.

So that all traffic coming into this port gets redirected towards the site, uncomment ‘listen 80.’ Exit after saving.

The host gets activated when you create symbolic links in the sites-enabled directories and the sites-available directories. In Apache, ‘a2ensite’ is the command to get this done. There is no equivalent shortcut in nginx, but nonetheless it is an easy command:

nginx save and exit pic

To make sure that your new site shows the right information and to avoid the ‘conflict in server name error,’ you can begin deleting the nginx default server block:

nginx server error pic

Restarting Nginx

To make all changes you just made visible, restart nginx:

nginx restart pic

Set Up Local Hosts

You can skip this step if you have pointed the domain name to the IP address of your server. At this point, you should have working virtual hosts. On the other hand, if you want to try a new virtual host without needing to connect to the actual name of the domain, you can set up a local host on just your computer.

If this is something you want to do, you will need to know the administrator password for your computer otherwise you will need to test the virtual host using an actual domain name.

If you are using Windows, you can find host file altering directions on the Microsoft website. If you are on a Linux or Mac, access the computer’s root user and open up the host file:

nginx mac or linux pic

To this file, you can add the details of the local hosts. As long as there is a line, when you direct your browser to, for instance, this gives you all the details corresponding to IP address of the virtual host:

nginx local host pic

To avoid any confusion in the future, it is a good idea to delete these addresses you made up out of the folder for local host.

See Results

Once your virtual host has been set up, you can see how this actually looks and might even want to compare results with the servers developed in this basic website course. Type your IP address or domain name into the browser, and you should see something like this:

nginx see results pic

Adding Virtual Hosts

When you want to create even more virtual hosts, you can repeat what you did carefully and set up a new root document with the right domain name. You can then activate your newly created, additional virtual host file just the way they show you in this web hosting course.