Set up virtual hostings in Nginx to serve multiple domains from the same server. The following guide illustrates how to configure an additional domain name, virtual.host, in Nginx.
Create and configure a new virtual host in Nginx:
Edit or create a new configuration file for your virtual host:
[root@www ~]# vi /etc/nginx/conf.d/virtual.host.conf
# Add the following configuration:
server {
listen 80;
server_name www.virtual.host;
location / {
root /usr/share/nginx/virtual.host;
index index.html index.htm;
}
}
Create the root directory for the new virtual host:
[root@www ~]# mkdir /usr/share/nginx/virtual.host
Apply the new configuration by reloading Nginx:
[root@www ~]# systemctl reload nginx
Verify the virtual host configuration by creating a test HTML page.
Create a simple HTML file in the document root of the new virtual host:
[root@www ~]# vi /usr/share/nginx/virtual.host/index.html
# Add the following HTML content:
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Nginx Virtual Host Test Page
</div>
</body>
</html>
After these steps, accessing http://www.virtual.host should display the Nginx Virtual Host Test Page, confirming that the virtual hosting is correctly set up and operational.