Skip to content

Security headers on Nginx — HSTS, CSP, X-Frame-Options and the rest

The Nginx add_header lines we use on every TLS site — HSTS, Content-Security-Policy, X-Frame-Options, Referrer-Policy, Permissions-Policy — and how to test what the browser actually sees.

Security headers are a low-effort, high-leverage layer of defence — they tell the browser what to trust, what to block, and how to behave around your site. The catch is that Nginx’s add_header directive has a specific inheritance behaviour that catches almost everyone, and Content-Security-Policy is non-trivial to author without breaking your own site. This article shows the headers we standardise on and how to validate them.

How to verify

See what your site emits today:

curl -sI https://example.com/ | grep -iE 'strict-transport|content-security|x-frame|x-content|referrer|permissions'
curl -sI https://example.com/ | grep -i 'server: '
# from a browser, open DevTools > Network > main document > Headers > Response

A modern site should answer with HSTS, X-Frame-Options (or CSP frame-ancestors), X-Content-Type-Options, Referrer-Policy, and (often) Permissions-Policy. Mozilla’s Observatory (https://observatory.mozilla.org/) grades the result and tells you what is missing.

What’s happening

add_header is the directive that emits a response header. The catch: add_header rules from an inner scope do not merge with outer scope — they replace. If you set headers in http {} and then set even one different header in a specific location {}, the location loses all the http-level headers. The pattern we use is: define headers in a single include snippet, and include it from every server block (and any location that overrides with its own add_header).

Also, the always argument matters. Without it, add_header only fires on 2xx, 3xx (some), and 1xx — so a 500 page from your app does not carry HSTS, which is wrong.

CSP is the heavy header. It declares what the page can load: scripts, styles, fonts, images, iframes, connections. Done right, it blocks a stored XSS payload. Done wrong, it blocks your own analytics and the page renders white.

The procedure

  1. Standard headers snippet. Drop this at /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 X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), camera=(), microphone=(), payment=()" always;
    add_header Cross-Origin-Opener-Policy "same-origin" always;
    Cross-Origin-Opener-Policy: same-origin is the headline cross-origin protection for browsers that support it.
  2. Include it from each server block:
    server {
        listen 443 ssl http2;
        server_name example.com;
        include snippets/security-headers.conf;
        # ...
    }
  3. Author a CSP iteratively. Start in report-only mode so you can find what breaks without blocking it:
    add_header Content-Security-Policy-Report-Only
      "default-src 'self'; script-src 'self' https://www.googletagmanager.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://www.google-analytics.com; frame-ancestors 'self'; report-to csp-endpoint;"
      always;
    Capture violations from report-to or browser DevTools. Once the report stream is clean for a week, flip from Report-Only to enforcing.
  4. If you use a frame-blocking strategy, prefer CSP’s frame-ancestors over X-Frame-Options. Modern browsers honour the CSP version preferentially, but ship both for compatibility.
  5. add_header does not merge across scopes. If a location block adds even one header (e.g. add_header Cache-Control …), it needs to re-include the security snippet:
    location / {
        include snippets/security-headers.conf;
        add_header Cache-Control "public, max-age=3600" always;
        # ...
    }
  6. Strip the Server header detail. Nginx version disclosure is a free recon clue:
    server_tokens off;
    Inside http {}. To remove the Server: nginx entirely you need a third-party module (headers_more), which lets you more_clear_headers Server;.
  7. Validate via https://securityheaders.com for the canonical headers and via Chrome DevTools > Application > Frames for CSP violation details.

Common pitfalls

  • A location block with one add_header silently dropping every header set in the outer scope. The browser sees almost no security headers; the operator sees them in nginx -T and assumes everything is fine.
  • HSTS preload submitted before you are confident every subdomain works on TLS. Removal takes weeks.
  • CSP with 'unsafe-inline' for scripts because that is how the page works today. Worth investing the rewrite — 'unsafe-inline' makes the whole CSP cosmetic against XSS.
  • Permissions-Policy: interest-cohort=() — the FLoC opt-out is obsolete; Chrome moved on. Use the current Privacy Sandbox opt-outs if relevant.
  • X-XSS-Protection: 1; mode=block is widely advised but actually deprecated and harmful in modern Chromium/Safari. Skip it.

In the engagements we run, the security-headers snippet is standardised across every host and CSP is owned per application — security shares the snippet; the app team owns its own CSP. Stack Harbor maintains both as code and validates them on each deploy in managed operations.