Nginx Security Headers Disappear Inside Nested `location` Block

Defining any `add_header` in a `location` block silently drops all parent `server` `add_header` rules.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Nginx Security Headers Disappear Inside Nested `location` Block Platform: Nginx
Common Root Causes:
  • Defining any `add_header` in a `location` block silently drops all parent `server` `add_header` rules.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Include a shared `security-headers.conf` snippet inside every location block that sets `add_header`.
Safe Diagnostic Command:
curl -sI https://<domain>/assets/app.mjs
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Defining any `add_header` in a `location` block silently drops all parent `server` `add_header` rules. Recommended diagnostic: `curl -sI https://<domain>/assets/app.mjs`. Remediation: Include a shared `security-headers.conf` snippet inside every location block that sets `add_header`.

Worked Examples

  • Run `curl -sI https://<domain>/assets/app.mjs` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-nginx-add-header-inheritance-lost · Independently verified: 2026-11-25

Related Resources