Why security headers matter
Security headers are instructions your server sends with every page, telling the browser to protect your visitors. They cost nothing, they are a few lines of configuration, and they close whole classes of attacks. Most sites are still missing some of them.
The five that matter most
| Header | What it protects against |
|---|---|
Strict-Transport-Security |
Downgrades to unencrypted HTTP. It should last at least a year (max-age=31536000). |
Content-Security-Policy |
Cross-site scripting: it tells the browser which sources may load scripts, styles and frames. |
X-Frame-Options, or frame-ancestors in the policy |
Clickjacking: other sites showing your page in an invisible frame. |
X-Content-Type-Options: nosniff |
Browsers guessing a file's type and running it as a script. |
Referrer-Policy |
Leaking full URLs, including tokens in them, to other sites. |
See what your site sends today
curl -sI https://example.com | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer-policy"
Every line missing from the result is a header you have not set.
Add them
In nginx:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
Two of them need care:
- HSTS. Only add
includeSubDomainsonce every subdomain is served over HTTPS, and only request preloading when you are sure, because it is hard to undo. - Content-Security-Policy. A strict policy can break a page that loads scripts from other sources. Start with the
Content-Security-Policy-Report-Onlyheader, so violations are reported without breaking anything, then tighten the policy before you enforce it.
Keep them in place
Headers disappear when a server is rebuilt, a CDN rule changes or a deploy replaces the configuration. The security headers check requests your page once a day and warns you when a header is missing or HSTS is too short. It also checks that the plain http:// address redirects to HTTPS, and fails when it does not.