Skip to main content
In this lesson you’ll configure Nginx to host two different websites on the same server using host-based virtual hosting. Nginx inspects the HTTP Host header and routes requests to the matching server block (site). We’ll create two site configs (example1 and example2), enable them, and test using curl with a custom Host header.
A presentation slide that reads "Configure Multiple Sites" on the left and shows a teal gradient shape on the right with the word "Demo." The bottom-left corner includes "© Copyright KodeKloud."

Overview

  • Client (browser or curl) sends an HTTP request to the server.
  • Nginx reads the Host header and selects the server block with a matching server_name.
  • If no match is found, Nginx serves the default server for that address/port (the first matching server block).

Initial state — remove the default site

Remove the default site so it doesn’t interfere with our example sites, then reload Nginx:
Explanation: after removing the default server block that listened on port 80, Nginx no longer has a listener for that address/port. Connections to localhost:80 are refused until you enable a site that listens on port 80.
Removing the default site will stop responses on port 80 until at least one valid site is enabled. Ensure you add and enable your site configurations before relying on the server in production.

Create site configurations

  1. Copy the default configuration to use as a template for example1, then edit it:
A minimal, cleaned server block for example1:
Notes:
  • try_files attempts to serve a file or directory; if nothing matches, it returns a 404.
  • Use a dedicated root directory per site to avoid accidentally serving /var/www/html content.
  1. Create example2 by copying example1 and updating root and server_name:

Prepare site content

Create site directories and simple index.html pages:

Enable the sites, validate, and reload

Create symlinks in sites-enabled, validate the configuration, and reload Nginx:

Test using curl with a Host header

Simulate requests for different domains by setting the Host header with curl:
Quick mapping reference: Behavior detail:
  • When no server_name matches the Host header, Nginx falls back to the default server for that address/port. With multiple enabled files, the fallback is typically the first server block encountered (often determined by alphabetical order of files in /etc/nginx/sites-enabled).

Alternative: multiple server blocks in one file

You can place multiple server { ... } blocks in a single configuration file. Example:
Operational considerations:
  • If both sites are in the same file and that file is deleted or misconfigured, both sites will be affected.
  • Best practice: keep one site per file in /etc/nginx/sites-available and enable sites with symlinks in /etc/nginx/sites-enabled to limit the blast radius and simplify rollbacks.

Final reminders

  • Always run nginx -t after editing configuration files to validate syntax.
  • After a successful test, reload Nginx with nginx -s reload to apply changes.
  • Creating/editing files under /etc/nginx/sites-available alone does not activate a site — remember to create corresponding symlinks in /etc/nginx/sites-enabled.
Keep each site’s configuration separate (one file per site) and use symlinks in /etc/nginx/sites-enabled for better isolation and safer rollbacks.
That’s it for this lesson.

Watch Video

Practice Lab