What are security headers, and which ones does my site need?
By Purlock · Updated 2 min read
The short answer
Security headers are short instructions your server sends with every page, telling browsers how to protect visitors. The ones most sites need are Strict-Transport-Security (always use https), Content-Security-Policy (limit which scripts can run), X-Frame-Options or frame-ancestors (stop clickjacking), X-Content-Type-Options, Referrer-Policy and Permissions-Policy. They cost nothing to add, and Purlock's free security headers checker grades your site and gives you each line to copy.
Key points
- Headers stop whole types of attack for the price of a few lines of config.
- Add Content-Security-Policy last, and test it in report only mode first.
- A good grade is one layer of security, not proof the whole site is secure.
Which security headers should every site have?
| Header | What it protects against | A common value |
|---|---|---|
| Strict-Transport-Security | Connections downgraded to plain http | max-age=31536000; includeSubDomains |
| Content-Security-Policy | Injected scripts and content | default-src 'self' |
| X-Frame-Options | Your site hidden in another site's frame (clickjacking) | DENY |
| X-Content-Type-Options | Browsers guessing a file's type | nosniff |
| Referrer-Policy | Full addresses leaking to other sites | strict-origin-when-cross-origin |
| Permissions-Policy | Pages using camera, microphone or location unasked | camera=(), microphone=(), geolocation=() |
Security headers checker
Get a grade for your site's security headers, with the exact lines to fix them.
How do I add security headers?
Where they go depends on your host. Each of these supports setting headers for every page.
- Vercel: a headers section in vercel.json, or headers() in next.config.
- Netlify: a _headers file in your published folder.
- Cloudflare: Transform Rules, under Modify Response Header.
- Apache: Header set lines in .htaccess.
- Nginx: add_header lines in the server block.
- WordPress and site builders: a security plugin, or your host's settings.
How do I add a Content Security Policy without breaking my site?
- Start with Content-Security-Policy-Report-Only, which reports what would be blocked without blocking anything.
- Browse every part of your site, including checkout and forms, and look at the browser console for reports.
- Add each domain your site really needs, like your analytics or payment provider, to the right directive.
- Switch to the enforcing Content-Security-Policy header once nothing legitimate is reported.
A policy that allows 'unsafe-inline' and every domain is easy to add but protects very little.
Do security headers affect SEO?
Not directly. https itself matters to search engines, and HSTS keeps visitors on it, but headers are there for safety. Their indirect value is trust: a hacked site loses rankings, and headers make some hacks harder.
How do I check my security headers?
- Run the security headers checker on your homepage. It grades the site from A to F and lists each header that's missing or weak.
- Copy the line it gives you for each missing header into your host's settings.
- Deploy, then run the check again to confirm the new headers are sent.
- Check one inner page and your login page too, since some hosts set headers per path.
- Set up a monitor, so a hosting change or new plugin that quietly removes a header gets noticed.
More questions
- Is X-XSS-Protection still needed?
- No. Modern browsers removed the feature it controlled. Content-Security-Policy is the replacement.
- Can headers break my site?
- Content-Security-Policy and frame protection can, if your site embeds outside scripts or is shown in frames on purpose. Test before enforcing.
- What's HSTS preload?
- A list built into browsers of sites that must always use https. Add preload to the header and submit your domain, but only when every subdomain supports https.