Nginx is an open-source, high-performance web server. It is known for its stability, very simple configuration, rich feature set, and low resource consumption.
Nginx lets you use it as a reverse proxy, load balancer, HTTP cache, and mail proxy.
A virtual host is an Apache term, however, is commonly used by Nginx users as well. The proper term for Nginx is “server block”.
In this tutorial we will show you how to set up Nginx Virtual Host on a CentOS 8 server.
First let’s start by ensuring your system is up-to-date.
# dnf update
# dnf install nginx
Enable and start Nginx.
# systemctl start nginx
# systemctl enable nginx
Open your web browser and go to your server. http://server-ip/ you will see something like this.
Creating Virtual Host
By default, Nginx is configured to load all configuration files that ends with .conf from the/etc/nginx/conf.d/ directory.
# nano /etc/nginx/conf.d/example.com.conf
server_name example.com www.example.com;
index index.php index.html index.htm;
Create virtual host home folder.
# mkdir -p /home/example.com
Nginx should be made the owner of that folder.
# chown -R nginx: /home/example.com
To test the Nginx configuration, run the following command.
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx service.
# systemctl restart nginx
Test your Results
To test Virtual host, you’ll have to create a index.html file to the document root directory.
# nano /home/example.com/index.html
<h1>Welcome to Test Page <br> The Virtual Host is working</h1>
Go to your browser and access your server hostname. You should see the next.
Nginx virtual hosts or server blocks are a great way to add additional websites to the same origin server. The number of configuration possibilities for a given site is nearly endless when you start modifying the virtual host configuration file to suit your site’s specific needs.