NGINX Configuration File Structure: Contexts, Directives and Inheritance

Understand nginx.conf: main, events, http, server and location contexts, simple vs block directives, include files and how directive inheritance really works...

Published November 27, 2026

An NGINX configuration looks simple until a setting mysteriously stops applying. The reason is almost always contexts and inheritance: which block a directive lives in decides where it applies, and some directives are replaced rather than merged when redefined. This guide maps out the structure so you can read any nginx.conf with confidence.

The concept

NGINX configuration is made of directives. A simple directive is a name, parameters and a semicolon: worker_processes auto;. A block directive has braces and contains other directives: http { ... }. A block directive that holds other directives is called a context.

Contexts nest in a fixed hierarchy. The main context (the top of the file) controls the process: user, worker processes, PID file. events configures connection handling. http contains everything related to HTTP: server blocks (sites), which contain location blocks (URL paths). There are also stream (TCP/UDP proxying) and mail contexts.

Directives are inherited downward: a value set in http applies to every server and location unless overridden. The critical subtlety is that array-like directives such as add_header and proxy_set_header are not merged. If a location defines even one add_header, it replaces all add_header directives inherited from server and http.

The context hierarchy

mainuser, worker_processes, error_logevents { }worker_connections, use epollhttp { }mime types, logging, gzip, upstreamsserver { }listen, server_name, ssl, rootlocation { }proxy_pass, try_files, headers
Each context nests inside the one above and inherits its settings.

A complete minimal nginx.conf

Most distribution configs follow this outline, with site-specific server blocks pulled in through include.

user  www-data;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout 65;
    gzip          on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Inheritance rules for common directives

DirectiveAllowed inInheritance behaviour
roothttp, server, locationInherited, overridable
add_headerhttp, server, locationReplaced entirely if redefined at a lower level
proxy_set_headerhttp, server, locationReplaced entirely if redefined at a lower level
gziphttp, server, locationInherited, overridable
worker_connectionseventsNot inherited (single context)
listen / server_nameserverNot inherited

Seeing the effective configuration

sudo nginx -T 2>/dev/null | grep -nE '^\s*(server_name|listen)' | head
41: listen 80 default_server;
44: server_name _;
63: listen 443 ssl;
64: server_name blog.example.com;
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx -T prints the full merged configuration, including every included file.

Practical examples

The add_header trap

The location defines its own header, so the security headers from server vanish for /api.

server {
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    location /api/ {
        add_header Cache-Control no-store;   # now the two headers above are NOT sent for /api/
        proxy_pass http://127.0.0.1:8000;
    }
}

Fix it with a shared snippet

Put common headers in a file and include it wherever you add headers.

# /etc/nginx/snippets/security-headers.conf
add_header X-Frame-Options DENY always;
add_header X-Content-Type-Options nosniff always;

location /api/ {
    include snippets/security-headers.conf;
    add_header Cache-Control no-store always;
    proxy_pass http://127.0.0.1:8000;
}

Use variables and map

map in the http context computes variables for later use.

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
}

Common mistakes

  • Placing a directive in a context where it is not allowed ("directive is not allowed here" error).
  • Redefining add_header or proxy_set_header in a location and silently losing inherited values.
  • Forgetting the semicolon at the end of a simple directive.
  • Editing a file that is not included at all; check with nginx -T.
  • Using if inside location for complex logic; many directives behave unexpectedly inside if.

Key takeaways

  • Contexts nest: main → events/http → server → location.
  • Most directives inherit downward, but add_header and proxy_set_header are replaced, not merged.
  • include lets you split config into per-site files and reusable snippets.
  • nginx -T shows the effective configuration; nginx -t validates it.

FAQ

Where is nginx.conf?

Usually /etc/nginx/nginx.conf. Run nginx -V and look for --conf-path to see the compiled-in location.

Does order of server blocks matter?

Only for choosing the default server when none is marked default_server: the first one for that listen address wins.

Can I use environment variables in nginx.conf?

Not directly. Use envsubst to template the file at startup (the official Docker image supports /etc/nginx/templates for this).

More Nginx articles