Skip to main content
In this guide you’ll configure Nginx to serve two separate websites from the same server using distinct server blocks (virtual hosts) and Host header routing. This is commonly called name-based virtual hosting and lets one IP address handle multiple domains.
A presentation slide that says "Configure Multiple Sites" with a large turquoise shape on the right containing the word "Demo." A small "© Copyright KodeKloud" notice appears at the bottom left.
Overview
  • A client (browser or CLI such as curl) sends an HTTP request to Nginx.
  • Nginx inspects the Host header and selects the matching server block (for example, example1 or example2).
  • Each server block serves files from its own document root (e.g. /var/www/example1 and /var/www/example2).
Prerequisites
  • You have a Linux server (Ubuntu/Debian recommended) with Nginx installed.
  • You can run commands with sudo.
  • Basic familiarity with editing files (e.g. nano or vim).
Quick reference — important file locations Step 1 — Prepare the environment
  1. Remove the default enabled site so it does not interfere with this demo:
  1. Verify no service responds on port 80:
Step 2 — Create two site configuration files
  1. Copy the default Nginx site to create example1 and then duplicate it for example2:
  1. Edit /etc/nginx/sites-available/example1 to serve the first site. A minimal server block looks like:
  1. Edit /etc/nginx/sites-available/example2 for the second site:
Step 3 — Create document roots and sample pages Create directories and simple index.html files for each site so we can verify routing:
Step 4 — Enable the sites (create symbolic links) Enable each site by linking from sites-available into sites-enabled:
Step 5 — Validate and reload Nginx Always test Nginx configuration before reloading:
Step 6 — Test with curl using the Host header Use curl to simulate requests for each domain by setting the Host header:
If the Host header does not match any server_name, Nginx will serve the default server for that listen socket — typically the first enabled server block for that address:port (often determined by the alphabetical order of files in /etc/nginx/sites-enabled):
Note about the default server selection
If a request’s Host header doesn’t match any server_name, Nginx uses the default server for that listen socket. The default is the first server block for that address:port — commonly the first file (alphabetically) in /etc/nginx/sites-enabled/. To explicitly set a default server, use the default_server flag on the listen directive (for example listen 80 default_server;).
Disabling sites temporarily
  • To disable a site without deleting its configuration, remove the symlink from /etc/nginx/sites-enabled/ and reload Nginx:
This keeps the file in /etc/nginx/sites-available/ for later reuse. Alternative: combine multiple server blocks in one file (not recommended)
  • You can place multiple server { ... } blocks in a single file and enable that one file with a single symlink. Example combined file:
Warning about combining server blocks
Keeping each site in its own file under /etc/nginx/sites-available/ and enabling them with separate symlinks reduces risk. If a single combined file is accidentally edited or removed, multiple sites could be taken down at once.
Summary — best practices
  • Create one server block per site and set a unique server_name.
  • Point each server block to its own document root.
  • Enable sites with symlinks in /etc/nginx/sites-enabled/.
  • Always validate config with sudo nginx -t then reload with sudo nginx -s reload.
  • Prefer separate files per site for isolation and safer management.
Links and references

Watch Video

Practice Lab