How to Set Up Nginx Virtual Hosts on CentOS 7

How to Set Up Nginx Virtual Hosts on CentOS 7

Nginx is one of the popular and mostly used Web servers. Nginx offers a variety of features such as load balancer, reverse proxy, etc. It is very fast as compared to Apache. Nginx is lighter and easy to scale. Nginx uses a virtual host (server blocks) to manage the domains. Virtual hosts, allow one server to host multiple websites.

In today’s tutorial, we will learn how to configure the Virtual host in Nginx to host the content of a website.


  • Server with CentOS 7 operating system
  • Root user or another user with Sudo privileges
  • Nginx web server on CentOS 7 instance

Disable the SELinux (Default Firewall)

  • To disable the SELinux temporarily, run the following command.
setenforce 0

Note:- If we reboot the server, then SELinux will become active again.

  • If you want to disable the SELinux permanently, run the following command.
vim /etc/sysconfig/selinux

Replace the SELinux=enforcing to SELinux=disabled.

Create Nginx Document Root Directory

  • To create the document root, use the mkdir command with the -p attribute.
sudo mkdir -p /var/www/
  • Change the ownership of the document root directory with the $USER environment variable.
sudo chown -R $USER:$USER /var/www/
  • Assign the right directory permissions recursively to the document root.
sudo chmod -R 755 /var/www/

Create a Demo Page for the Domain

  • Now we are going to create the index.html file inside the document root to show the demo content.
sudo vim /var/www/
  • Enter the following data inside the index.html file.
        <title>Welcome to tutorial!!!!!</title>
  <h1>Congratulation! Your Nginx virtual host is working!</h1>

Create Nginx Virtual Host

To serve the content of the index.html file on the domain. We need to create a virtual configuration file with the appropriate instructions.

  • To create the virtual configuration file, run the following command.
sudo vim /etc/nginx/conf.d/
  • Enter the following data inside the virtual configuration file.
server {
        listen 80;
        listen [::]:80;

        root /var/www/;
        index index.html index.htm index.nginx-debian.html;


        location / {
                try_files $uri $uri/ =404;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

  • To check the configuration which you have entered above is correct and error free, then run the following command.
sudo nginx -t
 Check the Nginx configuration
  • Restart and check the Nginx service.
sudo systemctl restart nginx
sudo systemctl status nginx

Test the Nginx Server Virtual Host

To test whether the virtual host is working or not. Enter the Domain name in the browser. You will get the following output if the virtual host is perfectly working.

Testing the Nginx Virtual host


In today’s tutorial, we have learned how to configure the virtual host (Server blocks) to serve the content of the website. We can host multiple websites on a single server by creating a different virtual host for every domain.

If you guys have any queries related to this tutorial, let me know in the comments section.