# nginx add_header not working? Inheritance and HSTS sent twice

> One add_header in an nginx location drops every header set above it. Here is the inheritance rule, a curl check, and how we ended up sending HSTS twice.

- Author: Amin Norollah (https://norollah.com)
- Publisher: toyfox (https://toyfox.app)
- Published: 2026-12-04
- Language: en (also in ar, es, fa, sv)
- Category: programming; tags: nginx, security-headers, hsts
- Canonical URL: https://toyfox.app/en/blog/nginx-add-header-not-working/
- Cite as: Amin Norollah, "nginx add_header not working? Inheritance and HSTS sent twice", toyfox blog, 2026-12-04, https://toyfox.app/en/blog/nginx-add-header-not-working/

You open the network tab on one of your static files, say `/assets/app.css`, and the `Strict-Transport-Security` header you set in the `server` block is not there. The HTML page next to it has it. The config file says both should carry it, and `nginx -t` says the config is fine. The only thing that changed last week is one `add_header Cache-Control` line in the assets location.

Or you see the opposite. You run `curl -sI` against a feed and the same response lists `strict-transport-security` twice, although every file you wrote sets it only once.

How can one harmless caching line make other headers disappear? How does a header end up in a response twice when no file sets it twice? And how do you find out what each location really sends, instead of what the config seems to say? Both symptoms come from the same rule in how nginx inherits `add_header`, and we ran into the second one on this site, toyfox.app, at the end of November 2026.

This post explains the rule, shows how to check every location with curl, walks through the mistake we made, and ends with four fixes that hold up. If your security headers are missing on some URLs or doubled on others, the cause is almost certainly here, and the check takes a minute.

## Why add_header in a location removes your server headers

The short answer is that **add_header is inherited from the level above only when the current level has no add_header of its own.** The nginx documentation for [add_header](https://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header) says it in one sentence, "These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level." There is no merging. A location either inherits the whole list from `server`, or it uses its own list and nothing else.

```nginx
server {
    add_header Strict-Transport-Security "max-age=15552000" always;
    add_header X-Content-Type-Options "nosniff" always;

    location / {
        # no add_header here: inherits both headers above
    }

    location /assets/ {
        add_header Cache-Control "public, max-age=604800" always;
        # only Cache-Control is sent: HSTS and nosniff are gone here
    }
}
```

Nothing warns you. `nginx -t` passes, the site works, and the only symptom is a header missing from some URLs. That is why this bug tends to live in production until somebody runs a scanner or reads the headers by hand.

> `include` is plain text substitution. An `add_header` inside an included snippet counts as an `add_header` of the block where the `include` line sits. A location that includes a proxy snippet containing one `add_header` has its own list, even if nobody ever typed `add_header` in that location.

The same rule works one level up. A `server` block with its own `add_header` ignores the ones in `http`, and an `if` block inside a location is a level as well.

### Headers on 404 and 500 responses need always

Without `always`, `add_header` only adds the field when the status is 200, 201, 204, 206, 301, 302, 303, 304, 307 or 308, as the same documentation page lists. A 404 page, a 429 from `limit_req` or a 502 from a backend that is down goes out without your security headers. With `always`, available since nginx 1.7.5, the header is added whatever the status code. We put `always` on every `add_header`, and that is why the 404 in the curl output below carries the full set.

## How to check which headers a location really sends

Do not trust the config file, ask the server. `curl -sI` sends a HEAD request and prints only the response headers. Pick one URL per location block, because each block can answer differently.

```bash
for path in /en/ /api/health /en/no-such-page/; do
  echo "== $path"
  curl -sI "https://toyfox.app$path" | grep -iE '^HTTP/|strict|x-frame|x-content|referrer|permissions|cache-control'
done
```

This is what it printed on 2026-12-01. The site sits behind Cloudflare, which passes these headers through unchanged.

```text
== /en/
HTTP/1.1 200 OK
x-content-type-options: nosniff
referrer-policy: strict-origin-when-cross-origin
x-frame-options: SAMEORIGIN
strict-transport-security: max-age=15552000
permissions-policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()
Cache-Control: no-cache
== /api/health
HTTP/1.1 200 OK
Cache-Control: no-store
x-content-type-options: nosniff
referrer-policy: no-referrer
strict-transport-security: max-age=15552000
== /en/no-such-page/
HTTP/1.1 404 Not Found
x-content-type-options: nosniff
referrer-policy: strict-origin-when-cross-origin
x-frame-options: SAMEORIGIN
strict-transport-security: max-age=15552000
permissions-policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()
Cache-Control: no-cache
```

On the API route only the `strict-transport-security` line comes from nginx, inherited from the `server` block. `Cache-Control`, `nosniff` and `no-referrer` are set by our NestJS backend. To find duplicates, count the header names.

```bash
curl -sI https://toyfox.app/en/blog/feed.xml | cut -d: -f1 | tr 'A-Z' 'a-z' | sort | uniq -c | sort -rn | head -4
```

Any line that starts with 2 is a header sent twice. Keep that command in mind, because it comes back later in this post.

## How we sent HSTS twice

Our landing config has a snippet, `toyfox-headers.conf`, with five headers. They are `X-Content-Type-Options`, `Referrer-Policy`, `X-Frame-Options`, `Strict-Transport-Security` and `Permissions-Policy`, all with `always`. Every location that sets its own `Cache-Control` includes it first, exactly because of the inheritance rule. That part has been right since the first version of the config in October 2026.

The API locations were different. They proxy to the backend and had no `add_header` at all, and the TLS `server` block had none either, so API responses carried no HSTS. On 2026-10-19 we fixed that the quick way, with one `add_header Strict-Transport-Security ... always;` at the end of the shared proxy snippet that every API location includes. It worked, and it planted the bug.

On 2026-11-30 we wanted the RSS feeds out of Google's index. The feed location is a proxy location too, so it got `X-Robots-Tag: noindex`. Because a location with its own `add_header` loses inheritance, we also gave it the header snippet, the way every page location has it. Now the feed included two files that both set HSTS.

```nginx
location ~ "^/(en|sv|es|fa|ar)/news/feed\.xml$" {
    include snippets/toyfox-headers.conf;     # five headers, HSTS among them
    add_header X-Robots-Tag "noindex" always;
    rewrite ... break;
    include snippets/landing-api-proxy.conf;  # proxy_pass, and HSTS again
}
```

`add_header` appends. It never checks whether a field with that name is already in the list, so the feed answered with two `Strict-Transport-Security` lines. We caught it on 2026-12-01 while checking the live headers of the new blog release.

![A diagram of one server block that sets HSTS and three locations. A location without add_header inherits it, a page location with its own add_header gets only its snippet, and the feed location of 2026-11-30 sent HSTS twice.](https://toyfox.app/media/blog/nginx-add-header-not-working/diagram-41ca5427.svg "One server block, three locations, and what each response carried, with the feed as it was on 2026-11-30")

Was it dangerous? Not really. [RFC 6797, section 8.1](https://www.rfc-editor.org/rfc/rfc6797.html#section-8.1) says a browser that receives more than one STS header field "MUST process only the first such header field", and both lines had the same value. But two copies mean the config does not do what its authors think it does. The next person who edits one of them gets two different policies, and only the first one counts.

## What our nginx config does now

The fix, in commit `3e91930`, gives HSTS exactly one source per kind of location. The proxy snippet has no `add_header` any more, with a comment that says why, and HSTS for the API moved to the `server` block, where only the locations without headers of their own inherit it.

```nginx
server {
    listen 443 ssl;
    server_name toyfox.app;
    # For locations without add_header of their own (the API);
    # page locations send it with snippets/toyfox-headers.conf
    add_header Strict-Transport-Security "max-age=15552000" always;

    include snippets/landing-locations.conf;
}
```

| Location | add_header of its own | HSTS comes from |
|---|---|---|
| `/api/...`, proxy only | none | the `server` block, inherited |
| pages, assets, `/media/`, `/admin/` | snippet plus `Cache-Control` or `X-Robots-Tag` | `toyfox-headers.conf` |
| feed, blog list and blog posts, proxied | snippet plus extras | `toyfox-headers.conf` |

The snippet and the `server` line never apply to the same location, so no response can carry HSTS twice. Our infrastructure doc now lists "HSTS once" on the feed as a check after every nginx sync.

The same trap catches CORS headers, by the way. A browser app on another origin that sends a custom request header, such as the Idempotency-Key from our post on [making POST safe to retry](https://toyfox.app/en/blog/idempotency-key-rest-api-nestjs-postgres/), needs `Access-Control-Allow-Headers` on the preflight response. Add that with `add_header` in one location and every inherited security header is gone from it.

### What curl found while we wrote this post

Running the duplicate check above on the live feed for this post, before the fix, showed something we had missed.

```text
      2 x-content-type-options
      2 referrer-policy
      1 x-robots-tag
      1 x-frame-options
```

HSTS is there once, as it should be. The two pairs come from two layers. Our NestJS backend sets `X-Content-Type-Options: nosniff` and `Referrer-Policy: no-referrer` on every API response, and the nginx snippet adds its own copies on top. `add_header` does not look at upstream headers either. For `Referrer-Policy` the browser takes the last valid value in the list, as the parsing steps in the [Referrer Policy spec](https://www.w3.org/TR/referrer-policy/#parse-referrer-policy-from-header) describe, so the feed ends up with the nginx value. For an RSS file this is harmless, but it is the same mistake as the double HSTS, two layers that each think they own a header. So we fixed it the same day, with one owner per header. The header snippet now ends with two more lines.

```nginx
# A page the backend builds (blog list, post, feed) already carries nosniff and its own Referrer-Policy: hide the
# backend's copies so each header is sent once, with the values above (no effect on files nginx serves itself)
proxy_hide_header X-Content-Type-Options;
proxy_hide_header Referrer-Policy;
```

[proxy_hide_header](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header) removes a header from the upstream response before nginx passes it on. It only acts on responses that come through `proxy_pass`, so in the locations where nginx serves a file from disk it does nothing at all. That is why it can live in the snippet that every location includes. The backend still sets both headers, which matters for the plain API routes that do not include the snippet. After the reload, the same count on the feed starts with a 1 on every line.

```text
      1 x-robots-tag
      1 x-frame-options
      1 x-content-type-options
```

`referrer-policy` now appears once, as `strict-origin-when-cross-origin`, and `/en/blog/` and `/en/` show the same.

## Four ways to fix add_header inheritance

### Repeat the full set through one snippet

Put every security header in one file and include it in each location that has its own `add_header`. This is what we do. The cost is discipline. A new location that forgets the include sends nothing, and no other include may set the same names. `grep -rn "add_header Strict" /etc/nginx/` should return the lines you expect and no more.

### Keep add_header at the server level only

If your locations only need different caching, the [expires](https://nginx.org/en/docs/http/ngx_http_headers_module.html#expires) directive sets `Expires` and `Cache-Control` without being an `add_header`, so inheritance stays intact. `expires 7d;` gives `Cache-Control: max-age=604800` and `expires -1;` gives `no-cache`. It only applies to the same success and redirect codes as `add_header` without `always`, and values such as `immutable` or `no-store` still need `add_header`.

### Use the headers-more module

[headers-more-nginx-module](https://github.com/openresty/headers-more-nginx-module) is a third-party module, bundled with OpenResty. Its `more_set_headers` replaces a header instead of appending one, and its `more_clear_headers` removes headers, including those from the upstream. It is the right tool when you must override what a backend sends. It is also one more module to build or install and keep in step with your nginx version, which is why we have not added it for a five-line problem.

### Use add_header_inherit on nginx 1.29.3 or later

Newer nginx has a directive that changes the rule itself. With [add_header_inherit](https://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header_inherit) set to `merge`, the values from the previous level are appended to the ones defined at the current level, and `off` cancels inheritance. The documentation says it "appeared in version 1.29.3". Check `nginx -v` before you rely on it. Distribution packages are often older, and on an older nginx the directive is unknown and `nginx -t` fails.

## Checklist for nginx security headers

- Every `add_header` for a security header has `always`.
- Every location with at least one `add_header`, including one that arrives through an `include`, also includes the full security snippet.
- No header name is set in two files that can end up in the same location. Grep for each name.
- For proxied locations, check what the backend already sends, then hide it with `proxy_hide_header` or leave that header to the backend.
- After each reload, run `curl -sI` against one URL per location block, including a 404 and an API route, and count the names with `sort | uniq -c`.
- Run `nginx -t` before every reload, knowing it catches syntax, not missing or doubled headers.

If you do one thing after reading this, list your location blocks, pick one URL for each, and run the curl loop above against production. It takes a minute, and it is the only check that shows what a browser actually receives. The config file only shows what you meant.

---

Source: https://toyfox.app/en/blog/nginx-add-header-not-working/ (toyfox, https://toyfox.app). When you use this article, please name toyfox and link the URL above.
