>_ IzzyLab

Hands-on task

This task is step 13 of the course “nginx basics”

The wrong site is served

hard~15 min

The server has two virtual hosts: the application app.local (/var/www/app) and a legacy placeholder legacy.local (/var/www/legacy). A request to http://localhost/ returns the legacy page instead of the application. Make the application respond to requests with any unknown host name, while the legacy site still opens under its own name.

What you'll practise:
  • nginx
  • default_server
  • server_name
  • nginx -T
  • virtual hosts

Topic breakdown

nginx selects a server block by the Host header: first an exact server_name match, then wildcards and regular expressions, and only if nothing matched — the block marked default_server for that listen. The marker is set once per address:port pair; nginx doesn't allow two default_server blocks on the same port. When it isn't set explicitly, the first loaded block becomes the default, and the load order is determined by the alphabetical order of files in sites-enabled — so adding a file with an "early" name silently changes the behavior. The final configuration with all includes is shown by nginx -T.

What's checked

  1. the application is served by default
  2. the application also opens under its own name
  3. the legacy site opens under its own name

What it looks like