ScaleScaleScaleScale

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/mysite1.com
mkdir /var/www/virtualhosts/mysite2.com
cd /var/www/virtualhosts
chmod 755 mysite1.com mysite2.com

Create a test page

cd /var/www/virtualhosts
nano -w mysite1.com/index.html
nano -w mysite2.com/index.html

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

Nice! This is the index for mysite1.com

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/mysite1.com.conf
nano -w /etc/nginx/conf.d/mysite2.com.conf

On both, place this content inside:

### mysite1.com

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
server_name  mysite1.com www.mysite1.com;

# define the root directory of your site and index files.
        location / {
            root   /var/www/virtualhosts/mysite1.com;
            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/mysite1.com;
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

}

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

Of course, on the examples previously posted, you need to change mysite1.com and mysite2.com 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 www.mysite1.com and www.mysite2.com 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 
12.34.56.78    www.mysite1.com mysite1.com
12.34.56.78    www.mysite2.com mysite2.com

Replace 12.34.56.78 with your server real IP, and mysite1.com / mysite2.com with your real domain names. Clean all your browser cache, cookies, temporary files and open a new browser and move to http://www.mysite1.com and http://www.mysite2.com. 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 mysite3.com.

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
  • virtual host nginx
  • nginx vhost
profile

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?
    Thanks.

    • 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.