Tips / Nginx

How to create Nginx virtual hosts (Server Blocks)

On this tutorial I will show you How To create Nginx Virtual Hosts (Server Blocks) on both RHEL based systems and also Ubuntu/Debian. But first, let’s introduce the concept of Virtual Hosts.

nginx virtual hosts

What is a Virtual Host?

Virtual Hosts are often used to run multiple websites on a single IP address. It’s a concept that comes from the old Apache era, because according to Nginx authors, on Nginx it is called ‘Server Blocks’.

Install Nginx

On Ubuntu/Debian: How to install Nginx on Ubuntu

On CentOS/RHEL: How to install Nginx on RHEL/CentOS

Define your virtual hosts directories

Let’s assume you need to create two websites to share the same IP address. Then, I will use this path on the example to create the root directories and also set proper permissions to each one.

mkdir -p /var/www/virtualhosts/
mkdir /var/www/virtualhosts/
cd /var/www/virtualhosts
chmod 755

Create a test page

cd /var/www/virtualhosts
nano -w
nano -w

You can put something like “This is a test page for” and the same for, customize the message as you like. For example:

Nice! This is the index for

Save the file and Exit (CTRL + X and press Y)

Configure Nginx Virtual Host includes

At nginx.conf (on centos/rhel usually located at /etc/nginx/nginx.conf and at ubuntu/debian at /usr/local/nginx/conf/nginx.conf) file, ensure there is a line like this:

include "/etc/nginx/conf.d/*.conf";

If it is not there, add it and then save the file, finally restart nginx to apply the new configuration:

service nginx restart

Create your virtual hosts definitions

nano -w /etc/nginx/conf.d/
nano -w /etc/nginx/conf.d/

On both, place this content inside:


server {

# disable access log and define error_log to critical.
access_log off;
error_log  logs/mysite1.com_error_log crit;

# listen at 80 port, on any ip address.
listen    80;

# set the server name of your website

# define the root directory of your site and index files.
        location / {
            root   /var/www/virtualhosts/;
            index  index.php index.html index.htm;


# define the root of static files and set cache to the max 
        location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$ {
        root   /var/www/virtualhosts/;
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";


Do the same for and you are done configuring your nginx virtual hosts.

Of course, on the examples previously posted, you need to change and with your real domain names and that same goes for the path /var/www/virtualhosts.

Restart Nginx to apply changes

service nginx restart

Test your Nginx virtual hosts

If you have pointed your DNS correctly for both sites, to the same DNS server and to the same IP address, if you type and you should see the websites online. If you are doing this on a private network or haven’t changed your DNS server, you can hack your /etc/hosts file to let your computer know that this two websites are located on a single IP, that way you will be able to see the websites even if they are actually offline for the rest of the world.

nano -w /etc/hosts

Add this code:

# Nginx Virtual Hosts

Replace with your server real IP, and / with your real domain names. Clean all your browser cache, cookies, temporary files and open a new browser and move to and Now you should be able to see your two Nginx virtual hosts live and working.

Adding more Nginx Virtual Hosts

If you need to add more virtual hosts, just repeat the process and add

PHP support for Nginx Virtual Hosts

On this tutorial I just covered a basic nginx virtual host configuration, ready to parse simple HTML and other static files (jpg, gif, png, etc) as a test. If you need to add PHP support to your Nginx virtual hosts, you need to check out these tutorials:

Popular search terms:

  • nginx virtual host
  • nginx virtual hosts
  • how to create virtual host in nignix
  • nginx vhost

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • ninoslav

    great post. i have similar configuration with many hosts like VMasines.
    My question is: http (80) forwarding is ok, but cant find, and cant setup forward for email ports. Any idea, maybe some example?

    • nginxadmin

      Nerver done that before with Nginx, I’ve only used it for http/https protocol 🙁

      Will do some research on that for the future =). Let me know if you find the answer first.