Skip to main content

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 → httpserverlocation
  • 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 include and snippets to keep config DRY

Topics in This Module


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/ or sites-available/
  • Use include /etc/nginx/snippets/ssl.conf; for reusable SSL config
  • Test every change with sudo nginx -t before reload
  • Use $host in 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.