How to Configure Nginx VirtualHost in Ubuntu
To Configure Nginx VirtualHost in Ubuntu
Virtual Hosting is a method which facilitates hosting multiple domains on a web server. Virtual Hosting is of two types &ndash Name based and IP based. While multiple sites can be run on the same IP address with Name based Virtual Hosting, the IP based virtual hosting can have different IP addresses for each site. This article simplifies the configuration procedure of name based Nginx Virtual Host in Ubuntu.
Configuration of Nginx VirtualHost
The initial step to configure Nginx VirtualHost is to go to its web root directory and create two directories as follows.
root@linuxhelp1:/home/user1# cd /var/www/html/ root@linuxhelp1:/var/www/html# mkdir site1 site2
Once the directories are created, use the following command to create a file with text in the site1 directory. Repeat the same for site2 directory.
root@linuxhelp1:/var/www/html/site1# vim index.html
Now, use the following commands to create server block files.
root@linuxhelp1:~# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site1.conf root@linuxhelp1:~# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site2.conf
Configure the site1.conf file as follows.
root@linuxhelp1:~# vim /etc/nginx/sites-available/site1.conf
server {
listen 80 default_server
listen [::]:80 default_server
# SSL configuration
#
# listen 443 ssl default_server
.
.
.
root /var/www/html/site1
# Add index.php to the list if you are using PHP
index index.html index.htm
server_name linuxhelp1.com www.linuxhelp1.com
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404
}
Now configure the site2.conf as follows.
Make sure you remove default_server behind listen 80 and listen [::]80
root@linuxhelp1:~# vim /etc/nginx/sites-available/site2.conf
server {
listen 80
listen [::]:80
# SSL configuration
# listen 443 ssl default_server
.
.
.
root /var/www/html/site2
# Add index.php to the list if you are using PHP
index index.html index.htm
server_name linuxhelp2.com www.linuxhelp2.com
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404
}
You can remove the default server block with the following command.
root@linuxhelp1:~# rm /etc/nginx/sites-enabled/default
Now enable the new server block by running the following command.
root@linuxhelp1:~# ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/ root@linuxhelp1:~# ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/
You can check the Nginx configuration with the help of below command.
root@linuxhelp1:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
After checking the Nginx configuration, restart the Nginx service by running following command.
root@linuxhelp1:~# systemctl restart nginx
Now you have to edit your hosts file by entering your IP address and domain name as below.
root@linuxhelp1:~# vim /etc/hosts
192.168.5.126 www.linuxhelp1.com
192.168.5.126 www.linuxhelp2.com
You can check the VirtualHost configuration by calling http://www.linuxhelp1.com on your browser' s address bar. The browser displays the content of that particular file as follows.
Now to check the second virtual domain, type http://www.linuxhelp2.com and see its content.
ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default