How to Set Up Multiple Nginx Virtual Hosts on a Single Server

This guide explains what virtual hosts are, how they work in Nginx, and provides step‑by‑step commands to create directory structures, test pages, server block configuration files, enable and disable sites, and verify the setup on a Linux server.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Set Up Multiple Nginx Virtual Hosts on a Single Server

What is a virtual host?

A virtual host allows a single server to host multiple domain names. In Apache this is called a virtual host, while Nginx refers to the same concept as a server block.

How virtual hosts work

When a web server such as Nginx starts, it binds to one or more network ports (typically 80 for HTTP and 443 for HTTPS). When a client requests a website, the server receives the request, examines the destination port, and then uses the HTTP Host header to determine which configured virtual host should handle the request.

Creating Nginx virtual host files

Two directories under /etc/nginx store configuration files: sites-available – holds all virtual‑host definitions. sites-enabled – contains symbolic links to the active definitions.

First, create a directory for each site’s web files under /var/www:

sudo mkdir -p /var/www/rumenz
sudo mkdir -p /var/www/tooltt

Then create a simple index.html for each site:

sudo vim /var/www/rumenz/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Welcome to Website 1!</title>
  </head>
  <body>
    <h1>入门小站!</h1>
  </body>
</html>
sudo vim /var/www/tooltt/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Welcome to Website 2!</title>
  </head>
  <body>
    <h1>tooltt.com!</h1>
  </body>
</html>

Create a server‑block file for the first site in sites-available:

sudo vim /etc/nginx/sites-available/rumenz.conf
server {
    listen 80;
    server_name www.rumenz.com;
    root /var/www/rumenz;
    access_log /var/log/nginx/rumenz-access.log;
    error_log /var/log/nginx/rumenz-error.log;
}

Copy it to create the second site and edit the domain name:

sudo cp /etc/nginx/sites-available/rumenz.conf /etc/nginx/sites-available/tooltt.conf
sudo vim /etc/nginx/sites-available/tooltt.conf
server {
    listen 80;
    server_name www.tooltt.com;
    root /var/www/tooltt;
    access_log /var/log/nginx/tooltt-access.log;
    error_log /var/log/nginx/tooltt-error.log;
}

Enable the virtual hosts

Create symbolic links from sites-available to sites-enabled:

sudo ln -s /etc/nginx/sites-available/rumenz.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/tooltt.conf /etc/nginx/sites-enabled/

Test the configuration syntax: sudo nginx -t If the test passes, reload Nginx:

sudo systemctl restart nginx

Test the virtual hosts (optional)

Add the domain names to /etc/hosts pointing to the server’s IP (e.g., 192.168.122.101):

vim /etc/hosts
192.168.122.101 rumenz.com
192.168.122.101 tooltt.com

Then open a browser and navigate to http://www.rumenz.com and http://www.tooltt.com to see the respective index pages.

Disable a virtual host

Remove the symbolic link for the site you want to disable and restart Nginx, for example:

sudo rm -rf /etc/nginx/sites-enabled/tooltt.conf
sudo systemctl restart nginx
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxNginxWeb serverVirtual Hostserver block
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.