episyche logo


Nginx/Virtual Host/

How to set up a virtual Host on an Nginx?

Published on

How to set up a virtual Host on an Nginx?
when you’re a beginner and not confident making changes to the default configuration of the web server.  Then setting up a virtual host can avoid making changes to the default configuration. Also using this method you can host multiple numbers of websites in a single Linux server.


 Hosting multiple websites on a single web server is called a virtual host. But in Nginx, the concept is called Server blocks that use the server_name and listen to directives to bind to TCP sockets. Also in certain scenarios, if you don’t want to edit the default Nginx files you can set up a virtual host to avoid editing the default configuration file.

A visual explanation of the virtual host is given below in the image:


Flow Diagram:




  • Ubuntu server

  • Domain name

  • Map ubuntu server public IP to the domain name’s DNS.


Check out this tutorial, to link (map) your newly purchased domain name with the ubuntu server.

Step 2: Install Nginx on Ubuntu.

Check out this tutorial, to install Nginx on Ubuntu.

Step 3: Create an Nginx config file under /etc/nginx/sites-available.

  • Create a new file with your domain name under /etc/nginx/sites-available

1cd /etc/nginx/sites-available 2vim <Domain_Name> 3 4For example: 5vim iternerays.com
  • Add the following content to the file, this

Note:* Change <Domain_Name> with your domain name (for example: iternerays.com)

1server { 2 listen 80; 3 listen [::]:80; 4 5 server_name <Domain_Name>; 6 7 root /var/www/<Domain_Name>; 8 index index.html; 9 10 location / { 11 try_files $uri $uri/ =404; 12 } 13}

The sample domain configuration file link given below.



  • Create a symbolic link from sites-available to sites-enabled

1sudo ln -s /etc/nginx/sites-available/<Domain_Name> /etc/nginx/sites-enabled/ 2 3For example: 4sudo ln -s /etc/nginx/sites-available/iternerays.com /etc/nginx/sites-enabled/


After executing the command a symbolic link will be created. An example output screenshot is given below:



Step 5: Create a root directory under /var/www for the virtual host.

  • Create a directory under /var/www with the domain name.

1cd /var/www 2mkdir <Domain_Name> 3 4For example: 5mkdir iternarys.com
  • Navigate to the domain directory created above step.

1cd /var/www/<Domain_Name> 2 3For example: 4cd /var/www/iternarys.com
  • Create index.html under your domain directory (i.e /var/www/<Domain_Name>)

1touch index.html 2echo"<h1>Hello World</h1>" >> index.html
  • Restart Nginx

1sudo systemctl restart nginx



check your browser, by visiting your domain name (for example: iternerays.com ). The reference output screenshot is given below.