How to Set Up Nginx Virtual Hosts on Ubuntu 20.04
Nginx is designed to handle multiple domains on a single server and IP address. Virtual hosts provide this feature. In this tutorial, we will set up Nginx virtual hosts.
In the Serverspace you can create a server with already installed app "Nginx".
Nginx configuration files
First, you need to install the Nginx package.
All configuration files for Nginx virtual hosts are stored in the /etc/nginx/sites-available/ folder. The best way is to create a separate file for each web site on the server. Let’s create the first configuration for domain-name.com.
Now insert this configuration there.
listen 80; # Specify the listening port
listen [::]:80; # The same thing for IPv6
root /var/www/domain-name.com/html; # The path to the website files
index index.html index.htm; # Files to display if only the domain name is specified in the address
server_name domain-name.com; # Domain name of this site
location / {
try_files $uri $uri/ =404;
}
}
Save and close this file.
Create a folder for the website and place its files there.
And set permissions for the folder.
Enabling the Nginx virtual host
You need to create a symbolic link to the configuration in the sites-enabled directory to enable the virtual host.
Now check the configuration for errors.
And restart the service.
Now you have a working virtual host for a single domain. You can access it by domain name if the DNS server is configured correctly. Any number of domains can be added to the server in this way.
Disabling Nginx virtual hosts
To disable a virtual host, remove the symbolic link from the sites-enabled folder. To disable returning a standard web page when accessing the server's IP address, you can simply delete the link to the default configuration.
Restart the service after that.