Server Configuration
Nginx configuration is clean, hierarchical, and powerful — but the location matching rules and directive inheritance can be surprising at first. This module covers everything.
Direct Config Editing
Nginx config files are plain text. Edit them directly with vim or nano. No GUI needed. Apply changes with sudo nginx -s reload.
What You Will Learn
- The three-level config structure: main →
http→server→location - How to write server blocks (the Nginx equivalent of virtual hosts)
- How location block matching works and what order rules are evaluated
- How to manage multiple sites cleanly
- How to use
includeand snippets to keep config DRY
Topics in This Module
- nginx.conf Structure — Main, events, http contexts and their directives
- Server Blocks — Defining sites,
server_name,listen,root,index - Location Blocks — Exact, prefix, regex matching; priority order; nested locations
- Directive Inheritance — How child contexts inherit from parents;
add_headergotchas - Multi-Site Organization —
sites-available+sites-enabled+conf.d/patterns - Variables and Maps — Built-in variables,
mapmodule for conditional logic
Configuration Hierarchy
/etc/nginx/nginx.conf ← Main config (global settings)
└── http { } ← HTTP settings (includes most sites)
└── server { } ← One per site / virtual host
└── location { } ← URL path matching rules
Each nested level inherits directives from its parent, and can override them.
Best Practices
- Put each site in its own file under
conf.d/orsites-available/ - Use
include /etc/nginx/snippets/ssl.conf;for reusable SSL config - Test every change with
sudo nginx -tbefore reload - Use
$hostin logs and conditionals rather than hardcoded domain names - Avoid deeply nested locations — flat is easier to debug
Success Checkpoint
By the end of this module you should be able to write a complete working server block from scratch, explain how location matching priority works, and organize a multi-site Nginx server cleanly.