Header and body filters
Headers and bodies have different jobs
A content handler supplies a response; a filter can modify its metadata or payload as the response is sent. A header can describe representation type, encoding or caching. A body filter changes the bytes. This simplified diagram omits buffering and special responses such as HEAD or 304. Module order is determined by the build and registration, not the order of lines in a location. Request phases and response filters are related but distinct.
content handler → header processing → response headers → body processing → response bytesReplace a literal in a controlled response
The optional HTTP substitution module searches for text without regard to case. It is not a regular-expression engine or an HTML parser. By default it applies to HTML and substitutes each configured search string once. In the example we explicitly select text/plain and replace repeated markers. A file containing BRAND BRAND should yield Garden Garden. The whole response can contain more than one buffer; a textual filter is not a safe general parser for arbitrary user documents.
location = /labels.txt { root html; sub_filter_types text/plain; sub_filter BRAND Garden; sub_filter_once off;}Keep metadata consistent with transformed bytes
When bytes change, the original representation length and validators may no longer be valid. The substitution filter normally removes Last-Modified; keeping it is appropriate only when the transformed response shares a valid modification history. Use the actual response headers to confirm behavior rather than reusing a strong ETag or hardcoding a length. This example adds a policy marker; it does not manually compute a length. Use no-store for this toy endpoint to avoid confusing cache experiments.
location = /labels.txt { root html; sub_filter_types text/plain; sub_filter BRAND Garden; sub_filter_once off; sub_filter_last_modified off; add_header Cache-Control no-store always;}Test compression and transformations independently
A compressed upstream payload is not plain text. When the application is a cooperating trusted origin, an empty Accept-Encoding header can request an unencoded representation, but verify that the origin respects it. Here the local static example uses gzip on another route, not a combination of filters. Test identity and compressed variants, compare decoded bytes, verify content type and validators, and inspect cache behavior. If a composition must run with multiple filters, test the exact binary and module set; do not assume swapping directives changes order.
location = /long.txt { root html; gzip on; gzip_types text/plain; gzip_min_length 100; gzip_vary on;}Practice and next steps
Practice: explain why a body change can invalidate a strong validator; compare two repeated markers under sub_filter_once on and off. Next: nginx-ssi-and-internal-content-boundaries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Header and body filters
Identify which response stage changes content
What does sub_filter match?
How can repeated occurrences of a configured marker be replaced?
Why might a transformed body need different validators?
Does changing directive line order reliably change filter execution order?
What should be compared when testing gzip and identity variants?