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.

Overview
- Client (browser or
curl) sends an HTTP request to the server. - Nginx reads the
Hostheader and selects theserverblock with a matchingserver_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: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
- Copy the default configuration to use as a template for
example1, then edit it:
server block for example1:
try_filesattempts to serve a file or directory; if nothing matches, it returns a 404.- Use a dedicated
rootdirectory per site to avoid accidentally serving/var/www/htmlcontent.
- Create
example2by copyingexample1and updatingrootandserver_name:
Prepare site content
Create site directories and simpleindex.html pages:
Enable the sites, validate, and reload
Create symlinks insites-enabled, validate the configuration, and reload Nginx:
Test using curl with a Host header
Simulate requests for different domains by setting theHost header with curl:
Behavior detail:
- When no
server_namematches theHostheader, 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 multipleserver { ... } blocks in a single configuration file. Example:
- 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-availableand enable sites with symlinks in/etc/nginx/sites-enabledto limit the blast radius and simplify rollbacks.
Final reminders
- Always run
nginx -tafter editing configuration files to validate syntax. - After a successful test, reload Nginx with
nginx -s reloadto apply changes. - Creating/editing files under
/etc/nginx/sites-availablealone 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.