nginx virtual hosts

How to Setup NGINX Virtual Hosts on CentOS

Virtual hosts allow you to host multiple websites and domains from single web server. Here’s how to setup NGINX virtual hosts on CentOS.

 

How to Setup NGINX Virtual Hosts on CentOS

Here are the steps to setup NGINX virtual hosts on CentOS. Please make sure you have installed NGINX in CentOS before you proceed further.

In this article, we will create two virtual hosts for domains domain1.com and domain2.com

 

1. Create Directories

Open terminal and create 2 directories, one for each virtual host, at /var/www. We will place our website files in these 2 directories.

# sudo mkdir -p /var/www/domain1.com/html
# sudo mkdir -p /var/www/domain2.com/html

 

Bonus Read : How to Enable CORS in NGINX

 

2. Create Virtual Host Configuration files

Next, create 2 virtual host configuration files, one for each domain, at /etc/nginx/sites-available

First, create virtual host configuration file for domain1.com domain

# sudo vim /etc/nginx/sites-available/domain1.com.conf

The above command will open a new file in a text editor. Add the following basic configuration in this file.

server {
   listen 80;
   listen [::]:80;
   server_name domain1.com;
   root /var/www/domain1.com/html;
   index index.html;
   location / {
    try_files $uri $uri/ =404;
   }
}

In the above commands we define a server block for domain1.com that listens to port 80 and serves content located at /var/www/domain1.com/html

 

Similarly, create virtual host configuration file for domain2.com domain

# sudo vim /etc/nginx/sites-available/domain2.com.conf

The above command will open a new file in a text editor. Add the following basic configuration in this file.

server {
   listen 80;
   listen [::]:80;
   server_name domain2.com;
   root /var/www/domain2.com/html;
   index index.html;
   location / {
    try_files $uri $uri/ =404;
   }
}

In the above commands we define a server block for domain2.com that listens to port 80 and serves content located at /var/www/domain2.com/html

 

Bonus Read : How to Enable TLS1.3 in NGINX

 

3. Create HTML pages

Next, create sample index.html pages for both domain. Create index.html file for domain1.com in a text editor

# sudo vim /var/www/domain1.com/html/index.html

Add the following HTML:

<html>
   <head>
    <title>Welcome to Domain1.com!</title>
   </head>
   <body>
   <h1>Success! The Domain1.com NGINX Virtual Host is working!</h1>
   </body>
</html>

Similarly, create index.html file for domain2.com

# sudo vim /var/www/domain2.com/html/index.html

Add the following HTML:

<html>
   <head>
    <title>Welcome to Domain2.com!</title>
   </head>
   <body>
    <h1>Success! The Domain2.com NGINX Virtual Host is working!</h1>
   </body>
</html>

 

Bonus Read : How to Fix 503 Service Temporarily Unavailable in NGINX

 

4. Enable Virtual Hosts

Enable virtual hosts by adding symbolic links between the files present at /etc/nginx/sites-available and /etc/nginx/sites-enabled.

# sudo ln -s /etc/nginx/sites-available/domain1.com.conf /etc/nginx/sites-enabled/domain1.com.conf
# sudo ln -s /etc/nginx/sites-available/domain2.com.conf /etc/nginx/sites-enabled/domain2.com.conf

 

5. Restart/Reload NGINX Server

Restart/Reload NGINX server to apply changes.

# sudo nginx -s reload

OR

# sudo service nginx restart

 

6. Test Virtual Hosts

Open browser and enter http://domain1.com and http://domain2.com one by one. You will see the index pages of both domains respectively.

 

Hopefully, now you can easily setup NGINX virtual hosts on Ubuntu.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!