Skip to content

Lesson 3 of 9 · 22m

Configuration Architecture for Real Systems

Structuring nginx config with includes, maps, shared snippets, environment boundaries, and safe change procedures that survive team growth.

The first nginx config is always one file. The tenth application on the same host is where archaeology begins: half-defined include chains, copy-pasted TLS blocks, mysterious if statements, and nobody willing to reload because the last person who did caused an outage. Good nginx administration is mostly configuration architecture — making the correct change obvious and the dangerous change hard.

Build a stable layout

/etc/nginx/
├── nginx.conf      # small: workers, events, includes
├── conf.d/         # zones, upstreams, shared settings
├── snippets/       # proxy-headers.conf, tls-modern.conf
└── sites/          # one file per public service

nginx.conf stays small: user, worker settings, events, and include lines. Shared snippets carry policy. Site files express routing. Do not let every service reimplement TLS, headers, logging, and forwarding rules in slightly different dialects.

Use map for policy tables, not nested if

map evaluates during configuration-driven variable lookup and is the right tool for host or request classification. Nested if inside locations is famously hazardous because it interacts badly with rewrite behavior and inherited directives.

map $host $upstream_name {
    default                 web_upstream;
    api.sudosecurity.org    api_upstream;
    learn.sudosecurity.org  learn_upstream;
}

The important habit is not the exact table. It is that routing policy lives in one visible place instead of being smeared through every site file.

Scope shared snippets narrowly

A snippet should state a complete policy with an unambiguous name:

# snippets/proxy-headers.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;

Site files opt into it with include snippets/proxy-headers.conf; inside the locations that need it. Do not hide location or server blocks inside snippets unless the snippet truly defines a reusable endpoint such as /healthz; hidden routing makes diffs hard to reason about.

Change procedure is part of the architecture

sudo nginx -t
sudo systemctl reload nginx
sudo tail -n 80 /var/log/nginx/error.log

Validate first, reload second, inspect logs immediately after. In configuration management, test a rendered candidate with nginx -t -c /path/to/nginx.conf before copying it into place. nginx configuration is code: review it, diff it, and deploy it with rollback in mind.

Field notes

  • Includes are globbed in lexical order; use numeric prefixes when order matters.
  • Variables in set are evaluated at request time and can hide expensive logic; prefer map for static classification.
  • Keep secrets out of shared snippets; nginx config is often broadly readable to administrators and backup systems.
  • A reload can still fail operationally if a referenced certificate or log path is unreadable.

Was this lesson helpful?

Sign in to rate