Handlers and filters
Distinguish a request handler from an output filter
A content handler supplies a response, while handlers in other phases can participate in routing or access decisions. A header or body filter transforms an outgoing response before passing it onward. Request phases and the output chain are related but separate. Built-in stub_status generates status text; add_header augments response metadata. In a loopback-only lab use both and observe the difference. Do not expose status counters publicly. Configuration below is a fragment for an already isolated server.
location = /status { stub_status; }location = /demo { add_header X-Lab-Filter enabled always; return 200 "demo";}Review a filter chain rather than replacing it
Filters typically save the current next filter during registration and install themselves at the head. During processing they call the saved next filter, propagate return codes, and preserve buffer ownership and end-of-stream markers. Registration order affects processing order, so consider compression, partial responses, subrequests and bodyless responses. A change to body size can invalidate response length or representation metadata. This pseudocode is a design sketch for a benign optional module adding a fixed diagnostic header, not a compilable addon or a security control.
postconfiguration: save next header filter; register lab filteron response: add fixed diagnostic marker if enabledon allocation failure: return appropriate errorotherwise: call next filter and return its resultAvoid blocking a worker and log safe evidence
Workers service many connections through event callbacks. A synchronous wait in one handler delays unrelated work on that worker. Use supported nonblocking event APIs and bounded timers; design callback cleanup around the correct lifetime. An operation that cannot progress immediately may need to resume later, not busy-loop. Nginx logging APIs use the appropriate log object and severity. Record fixed diagnostic text and identifiers when needed, not authorization headers, secrets or raw user content. This pseudocode is an event-review outline only; precise return and finalization contracts depend on the target API.
on ready: perform bounded nonblocking workon would-block: arrange next event and return appropriatelyon timeout: clean up; log safe diagnosticon completion: finalize once with the correct APIUse independent release gates for an optional module
Design a minimal optional addon that emits only a constant response header when enabled, with default off and no network calls or persistent state. Have its code and dependency provenance reviewed first. In a disposable build lab compile against the pinned target, run the upstream test suite with documented skips, then load only that reviewed output in an isolated configuration and test on/off, inheritance, HEAD, ordinary GET and errors. The local lab does not build or load such an addon; it uses installed built-in modules as observable analogues. Do not deploy from this lesson. Roll back by restoring a tested matching binary/config/module artifact set, not by mixing versions.
G1 source review + build manifestG2 actual compilation of reviewed addonG3 regression suite + syntax checkG4 isolated HTTP checks + rollback drillAll required gates must have evidence.Apply what you learned
Practice: Sketch a fixed diagnostic-header filter with default off, ownership and next-filter behavior. Write independent build, suite, syntax, HTTP and rollback gates. 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
Handlers and filters
Assess module architecture, build compatibility and runtime reasoning.
Which built-in feature generates status text?
What should a typical header filter do after its own work?
Why avoid synchronous waiting inside a worker callback?
Which evidence demonstrates addon compilation?
What is a safe rollback unit for a native module?