Blog · Security

nginx: the add_header that wipes out your security headers

· 3 min read · by

Originally written in Portuguese. Português

Dark cover with the title nginx: the add_header that wipes out your security headers and a server block with add_header

3 min read

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_header directives are inherited from the level above only if the current level has no add_header of its own.
  • A single add_header inside a location wipes out all the ones in server.
  • Fixes: put the common headers in a file and include it in every block, or use add_header_inherit merge on nginx 1.29.3 or newer.
  • Test with curl -I on 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

Want a review of the headers and nginx in your environment? Talk to us.