Module command tables and config merge
Locate the command owner and allowed context
A configuration directive is registered by a module. The command table links the directive name, legal contexts, argument rules and setter to module configuration storage. The module descriptor also carries its module type and context pointer. For HTTP modules, main, server and location configuration have different lifetimes and storage. A directive rejected in a location may simply be legal at another level. Read the table rather than guessing from another directive. This schematic record is not valid C and is not a new Nginx directive.
command: example_flag (hypothetical)context: http / server / locationargument: on or offsetter: flag parser -> location config fieldend of table: sentinelCreate unset values before merging
Configuration creation allocates storage, sets appropriate unset sentinels, and reports allocation failure. Parsing stores explicit values. Merge logic then resolves a child value against the parent and a default. A zero value can mean explicitly off, not absent. Actual Nginx APIs use type-appropriate sentinels and merge helpers; this standalone C model illustrates just one boolean setting without using Nginx headers. It is not a loadable module. With child unset, parent on, the result is on; with child off the result is off.
enum { UNSET = -1, OFF = 0, ON = 1 };static int merge_flag(int parent, int child) { if (child != UNSET) return child; return parent == UNSET ? OFF : parent;}Observe real module-specific inheritance
Inheritance is implemented by individual modules and differs by directive. On the local 1.26.3 baseline, add_header directives from a higher level are inherited only if the child has no add_header of its own. This fragment returns a parent marker at /inherit and only a child marker at /override. Newer releases have additional inheritance controls; consult the target release. The lesson does not infer a universal rule from this one module. The localhost lab checks the actual response headers.
add_header X-Parent on always;location = /inherit { return 200 "ok"; }location = /override { add_header X-Child on always; return 200 "ok";}Keep lifecycle and memory ownership explicit
Configuration-time data belongs to the configuration pool, and request-specific data belongs to a request pool. Pools release their storage as a group when their owner ends; do not keep a request-owned pointer for later unrelated requests. Check allocation return values, register cleanup when a resource needs it, and avoid shared mutable globals as a substitute for per-request context. A reload creates a new configuration cycle while old workers may still finish requests. The following is an ownership checklist, not executable C. Review the C and systems programming tracks for pointer lifetime, structs, callbacks and event loops before writing a real module.
configuration pool -> directive settingsrequest pool -> per-request stateexternal resource -> explicit cleanup ownershipreload -> new cycle; old workers may finishApply what you learned
Practice: Run the standalone C model and predict the result when child is unset, explicitly off or on. Compare parent and child add_header responses in the local lab. The manual exercise and quiz are staged separately. Related references and the next lesson are listed in the chapter authored map.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Module command tables and config merge
Assess module architecture, build compatibility and runtime reasoning.
What does the command table describe?
Child OFF and parent ON resolve to what in the model?
Both child and parent UNSET resolve to what in the model?
On 1.26.3, a child with its own add_header receives what in the example?
Where should transient per-request state live?