How To Set Up Vhosts In The Nginx Webserver?

NGINX, pronounced “engine-ex,” is an open-source web server software used for various purposes, including web serving, reverse proxying, caching, load balancing, and media streaming. It was initially designed to handle large numbers of concurrent connections, making it suitable for high-performance web applications.

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Prerequisites

  • A Debian Bookworm (or other Debian based) server
  • Root access
  • Internet connection
  • Website code (or test html)
  • Coffee

Installing the Nginx webserver

Nginx is in the Debian package repository. Install it with apt.

apt install nginx

Creating the directory structure for the sites

We are going to host two sites in our imaginary setup:

  1. prodsite.com
  2. testsite.org

Create the following web root directories.

mkdir -p /var/www/prodsite.com/public_html
mkdir -p /var/www/testsite.org/public_html

Give read access to the web directory.

chmod -R 755 /var/www

Testing with small HTML files

I just create two small “websites” to test the settings.

/var/www/prodsite.com/public_html/index.html

<html>
  <head>
    <title>Welcome to prodsite.com!</title>
  </head>
  <body>
    <h1>This is the prodsite.com.</h1>
    <p>If you see this then the vhost configuration was successful!</p>
  </body>
</html>

/var/www/testsite.org/public_html/index.html

<html>
  <head>
    <title>Welcome to testsite.org!</title>
  </head>
  <body>
    <h1>This is the testsite.org.</h1>
    <p>If you see this then the vhost configuration was successful!</p>
  </body>
</html>

Creating the Nginx configuration

Name-based virtual servers in Nginx allow multiple websites to be served from the same IP address. This is achieved by using the server_name directive to define the domain names associated with each server block. When a request is received, Nginx uses the server_name to determine which server block should process the request. This allows for efficient hosting of multiple websites on a single server. In contrast, IP-based virtual servers require a different IP:port combination for each website, which can be less efficient in terms of resource utilization.

/etc/nginx/sites-available/prodsite

server {
    listen80;
    listen [::]:80;

    server_name prodsite.com;

    root /var/www/prodsite.com/public_html/;
    index index.html;

    location / {
        try_files$uri$uri/ =404;
    }
}

/etc/nginx/sites-available/testsite

server {
    listen80;
    listen [::]:80;

    server_name testsite.org;

    root /var/www/testsite.org/public_html/;
    index index.html;

    location / {
        try_files$uri$uri/ =404;
    }
}

Enabling the sites

ln -s /etc/nginx/sites-available/prodsite /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/testsite /etc/nginx/sites-enabled/

Validating the configuration

nginx -t

Restarting the Nginx server

systemctl restart nginx

Checking the sites with Curl

If our DNS configuration is right we can navigate to the URLs in a browser or we can use Curl to verify our settings.

curl http://prodsite.com
curl http://testsite.org

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Leave a comment