Article
Security headers in nginx: why your add_header disappears (and the config that actually works)
Published 2026-11-29
Security headers in nginx: why your add_header disappears (and the config that actually works)
You added add_header X-Frame-Options "DENY"; to your server block, reloaded nginx, ran a headers scan, and the header is not there. Or it is there on the homepage but missing on half your URLs. Nothing errored. nginx -t said the config was fine. This is the single most common nginx security-header failure I see, and it has nothing to do with the header you chose. It is two nginx behaviors that are documented but easy to miss: add_header only applies to a short list of response codes, and a nested add_header silently cancels every header inherited from above.
This article walks the config that works: what to put in it, where to put it, the two traps that delete your headers, how to check the result with curl, and which headers I would actually bother with in 2026.
The two ways nginx deletes your headers
The first trap is the response code. The add_header directive from the ngx_http_headers_module only adds a header when the response code is one of a specific set: 200, 201, 204, 206, 301, 302, 303, 304, 307, or 308. That is the documented default. A 404, a 403, a 500, or a 4xx from your app gets no security headers at all. Your error pages are a real surface with real HTML, so a scanner hits one of them and reports the header missing, and it is right. The fix is one word: always. Add it to every security-header line. It has existed since nginx 1.7.5, so there is no version excuse for skipping it:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
The second trap is inheritance, and it deletes headers in a way nobody expects. nginx documentation states the rule verbatim: add_header directives "are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level." Read that twice. The moment any location block has even one add_header of its own, it stops inheriting all the headers set in the server block. One innocent line like add_header Cache-Control "no-store"; inside a location does not add to your server-level headers, it replaces them. Your HSTS, your X-Frame-Options, everything gone for that location, and nginx -t is perfectly happy because the config is syntactically valid.
Since nginx 1.29.3 (released late 2025) there is a proper fix: the add_header_inherit directive. With add_header_inherit merge; at the server level, headers from the parent and the current level are combined instead of the child wiping the parent. If you are on a distribution that ships a recent nginx, use it:
server {
...
add_header_inherit merge;
add_header X-Frame-Options "DENY" always;
location /api/ {
add_header Content-Security-Policy "default-src 'self'" always;
}
}
If your nginx is older than 1.29.3, and most long-lived production servers are, the only reliable pattern is to put every add_header at the server level and put none in any location. When a location genuinely needs its own header, re-declare the whole set there. The nginx blog itself describes the pre-1.29.3 behavior as something that "silently [broke] security headers."
The block I would actually use
One file, included once per server block, is the whole trick. Put it in snippets/security-headers.conf and include it from each HTTPS server block:
# /etc/nginx/snippets/security-headers.conf
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Frame-Options "DENY" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'" always;
Each line earns its place:
Strict-Transport-Security tells browsers to stick to HTTPS for max-age seconds. This one only works on HTTPS responses and only starts counting once a browser has seen it, so the first visit still travels the HTTP redirect. Start with a smaller max-age on a site new to HSTS, then raise it. Do not add preload until you understand what the preload list commits you to; removing a domain from it takes months. (We cover the rollout order in detail in our HSTS article.)
X-Content-Type-Options "nosniff" stops browsers second-guessing a Content-Type. Without it, a file you serve as text/plain can be reinterpreted as script, which turns any file-upload folder into a script-execution risk. This is the cheapest header in the list; there is no reason to omit it.
Referrer-Policy "strict-origin-when-cross-origin" is the browser default already, but setting it pins the behavior instead of leaving it to the browser's choice. It sends full URLs same-origin and only the origin cross-origin, so query strings (which often carry tokens and IDs) do not leak to third-party sites.
X-Frame-Options "DENY" blocks the page being framed. In 2026 the modern way to say this is the CSP frame-ancestors directive, which has effectively replaced it; when both are present, browsers that support frame-ancestors ignore the X-Frame-Options header entirely. Old browsers (mainly legacy IE) still need X-Frame-Options, so the belt-and-braces approach is to set both. If your pages must be framable by your own pages, use frame-ancestors 'self' and SAMEORIGIN instead of none/DENY. The trade-offs are covered in frame-ancestors vs X-Frame-Options.
Permissions-Policy closes browser features (camera, microphone, geolocation, and about a dozen more) for the page and everything embedded in it. If your site uses none of them, denying all is free. If you embed maps or video calls, name the feature and allow it selectively instead.
Content-Security-Policy is the strongest header on the list and the one that breaks sites when copy-pasted blind. A starter default-src 'self' will break any page pulling scripts from a CDN, analytics, or using inline handlers. My honest advice: do not bolt a CSP onto a site you have not audited. Roll it out with Content-Security-Policy-Report-Only first, watch the reports, then enforce. The full procedure is in our CSP explained article.
I deliberately do not send X-XSS-Protection "0". Modern browsers have removed the XSS auditor entirely; sending 0 is a tombstone for a feature that no longer exists, and any header that exists only to say "ignore me" is noise. If your scanner nags about it, the honest reading is that the scanner's checklist is dated, not that your site is missing protection.
Where the include goes
Only in the HTTPS server block. A few placement rules keep this boring:
server {
listen 443 ssl;
server_name example.com;
include snippets/security-headers.conf;
# ... the rest of your site
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
include at the server level means every location inside that block inherits the headers, as long as no location declares its own add_header. If you have location blocks that add headers (a common one is location /api/ adding a CORS line), you are on the pre-1.29.3 treadmill: re-declare the whole security set inside that location too, or move the CORS header to the app and keep nginx clean.
Do not put HSTS in the port 80 block. The spec (RFC 6797) says a UA MUST ignore HSTS received over plain HTTP, so it does nothing there except confuse anyone reading the config later. The plain-HTTP block should do exactly one thing: redirect.
And reload properly after every edit:
sudo nginx -t && sudo systemctl reload nginx
nginx -t catches syntax errors, reload picks up the new config without dropping connections. Skipping the test is how a typo takes every site on the box down.
Checking the result with curl
Never trust the reload alone. A header that appears on https://example.com/ can still be missing on the 404 page, on a proxied API path, or on a site with multiple server blocks where one block got the include and the other did not. Check deliberately:
curl -sI https://example.com/ | grep -iE 'strict|x-frame|referrer|content-security'
Then check the routes people forget:
curl -sI https://example.com/nonexistent-page-404 | grep -i x-frame
curl -sI https://example.com/wp-login.php | grep -i strict
The 404 check catches the missing always parameter. If the header shows on a 200 and disappears on the 404, you forgot always, exactly the default-response-code behavior described above. The second command catches the inheritance trap: a location ~ \.php$ block that proxies to PHP-FPM and has one add_header of its own will strip every server-level header from all your dynamic pages, which is most of the site.
The lazy version is a free scan that checks all of this at once: scan your site on checkwebsecurity.com and read the headers section, plus TLS and the redirect chain, in one pass.
Questions people ask
Why do my nginx security headers disappear on some pages?
Because add_header only fires on a fixed list of response codes (200, 201, 204, 206, 301, 302, 303, 304, 307, 308) unless you add always, and any location block with its own add_header cancels all headers inherited from the server block.
What is the nginx add_header_inherit directive?
Introduced in nginx 1.29.3, it changes the inheritance rule: add_header_inherit merge; combines headers from the parent level with those defined locally instead of the local ones replacing them.
Should X-Frame-Options be DENY or SAMEORIGIN?
DENY if nothing on your site frames your own pages, SAMEORIGIN if your pages embed each other in iframes. Modern browsers prefer the CSP frame-ancestors directive, so set both to be safe.
Is X-XSS-Protection still needed?
No. Chrome, Edge, and Firefox have removed the XSS auditor; sending X-XSS-Protection: 0 is the documented way to switch off a filter that no longer exists, so the header is safe to omit on modern sites.
Headers are the perimeter of a web application, and the perimeter is what the free scan on this site looks at. If you just inherited an nginx config or want a second opinion on your own, run a scan and see exactly which headers your site presents on every route, including the error pages.