Blog · Security
nginx: the add_header that wipes out your security headers

You configure HSTS, CSP and X-Content-Type-Options in nginx’s server block, test the home page and everything is there. Then you add a Cache-Control for static files and, without noticing, those files start going out without any security headers.
It is not a bug. It is the documented inheritance rule of add_header, and it catches a lot of people.
In short
add_headerdirectives are inherited from the level above only if the current level has noadd_headerof its own.- A single
add_headerinside alocationwipes out all the ones inserver. - Fixes: put the common headers in a file and
includeit in every block, or useadd_header_inherit mergeon nginx 1.29.3 or newer. - Test with
curl -Ion every type of response, not just the home page.
How the problem shows up
A common configuration for a static site:
server {
add_header Strict-Transport-Security "max-age=31536000" always;
add_header Content-Security-Policy "default-src 'self'" always;
add_header X-Content-Type-Options "nosniff" always;
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
}
The home page responds with all three security headers. But any file under /assets/ responds with only Cache-Control. The nginx documentation is explicit: add_header directives from the previous level apply only when the current level defines none.
In practice, CSS, JavaScript and fonts end up without nosniff and without CSP, which are exactly the files where the content type matters most.
Fix 1: a snippet included in every block
This is the solution we use, because it works on any nginx version. The common headers go into a file:
# /etc/nginx/snippets/security-headers.conf
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
And every block that defines any add_header also includes the file:
location /assets/ {
include /etc/nginx/snippets/security-headers.conf;
add_header Cache-Control "public, max-age=31536000, immutable" always;
}
The thing to watch here is not repeating in the block a header that is already in the snippet. If you do, the same header goes out twice with different values, and each browser handles it its own way.
Fix 2: add_header_inherit (nginx 1.29.3 or newer)
Recent nginx versions gained a directive that changes this behavior:
location /assets/ {
add_header_inherit merge;
add_header Cache-Control "public, max-age=31536000, immutable" always;
}
With merge, the headers from the level above are added to those of the current block. Check your version with nginx -v before using it. Our image runs 1.30.5, but we kept the snippet because it is explicit and works in any environment.
Do not forget “always”
Without the always parameter, nginx only adds the header to success and redirect responses. Error pages, such as the 404, go out without it. For security headers, always use always.
How to really test it
Testing only the home page does not catch the problem. Check one URL of each type:
for u in / /assets/app.css /og.jpg /pagina-que-nao-existe; do
echo "== $u"
curl -sI "https://seu-site.com$u" | grep -iE 'strict-transport|content-security|x-content-type'
done
If any URL comes back without the three headers, there is a location with its own add_header and no snippet. In our case, this check became part of the review of every nginx change.
References
- nginx: ngx_http_headers_module module (
add_header,alwaysandadd_header_inherit) - MDN: Strict-Transport-Security
- OWASP: HTTP Security Response Headers Cheat Sheet
Want a review of the headers and nginx in your environment? Talk to us.

