How to add security headers to your site — in one evening, without breaking it

Security headers are that rare case where protection costs fifteen lines of config and zero changes to your code. You're not rewriting the app: you're adding a few lines to every server response, and the browser starts behaving more strictly.
The order below is deliberate, so nothing breaks: first the headers that are nearly impossible to get wrong, and only at the end CSP — and even then in safe mode.
Step 1. See what you have right now
One command shows your starting point:
curl -sI https://your-site.com | grep -iE 'strict-transport|content-security|x-content-type|referrer-policy|permissions-policy|x-powered-by'
Empty output is normal — most fresh projects look like that. Also note if x-powered-by shows up: it tells the world for free what your site is built with. We'll drop it at the end.
Step 2. Add three headers that break nothing
Start with these three — zero risk, immediate benefit:
X-Content-Type-Options: nosniff— stops the browser from guessing a file's type by its content. Without it, a user-uploaded "image" with JavaScript inside can be executed as a script.Referrer-Policy: strict-origin-when-cross-origin— when someone leaves for another site, only your domain travels, without the path and query. Modern browsers default to this anyway, but frameworks and proxies sometimes override it, so set it explicitly.Strict-Transport-Security: max-age=15768000— the browser remembers you're HTTPS-only and stops trying plain http. Six months is the floor; two years is fine later.
⚠ One warning about HSTS: switch it on only when HTTPS truly works on every subdomain. The browser will remember the rule for the whole max-age, and rolling it back quickly isn't possible. Unsure about subdomains? Skip includeSubDomains, and don't touch preload at all — getting into the browsers' preload list is close to a one-way street.
Step 3. Ban embedding your site in someone's iframe
So nobody draws their page on top of your buttons:
Content-Security-Policy: frame-ancestors 'self'
X-Frame-Options used to do this and still works — but its ALLOW-FROM variant is obsolete and modern browsers ignore it. frame-ancestors does the same and more, so new projects go straight to it. Want both for very old browsers? Keep X-Frame-Options: DENY alongside; they don't conflict.
Step 4. Turn off the capabilities you don't use
Permissions-Policy controls what the page may ask of the device. If your site doesn't need the camera, microphone or location, say so:
Permissions-Policy: camera=(), microphone=(), geolocation=()
Empty parentheses mean "nobody, including me." The point isn't protection from yourself but from third-party code on the page: an embedded widget can no longer pop up "this site wants to use your microphone" in your name.
Step 5. Turn on CSP — but in report mode
The most powerful header and the only one that can genuinely take your page down. So the first pass is observation only:
Content-Security-Policy-Report-Only: default-src 'self'
It blocks nothing: the browser just logs what it would have blocked. Open the site, click everything, collect the list of violations — usually a couple of CDNs, fonts and your own inline scripts. Then add sources one at a time and only afterwards switch to the enforcing Content-Security-Policy. How the directives read and why nonces don't work on static hosting is in what is CSP.
Step 6. Put it in your hosting config
Where depends on where the site lives. The three most common cases.
Vercel — a vercel.json in the root:
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
{ "key": "Strict-Transport-Security", "value": "max-age=15768000" }
]
}
]
}
Netlify — a block in netlify.toml:
[[headers]]
for = "/*"
[headers.values]
X-Content-Type-Options = "nosniff"
Referrer-Policy = "strict-origin-when-cross-origin"
Your own nginx — inside the relevant server:
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=15768000" always;
That always in nginx matters: without it the headers won't be attached to error responses — and error pages need protection just as much.
What you get
Run the command from step 1 again — now it prints four or five lines instead of nothing. What actually changed: the browser won't execute a planted file as a script, won't hand a foreign site your full URL, refuses to open you over http, won't let anyone frame you, and won't let a third-party widget reach the camera. All of it in config, zero changes in application code.
Next, walk the rest of the list before you ship: pre-launch security checklist. And if the page stopped working after you enabled CSP — that's expected, and it's diagnosed by symptom in why CSP blocks my own scripts.
Will headers slow the site down?
No. It's a few hundred bytes per response. Next to a single image, invisible.
Are headers enough instead of decent code?
They're not. They close a whole class of browser attacks, but they don't validate data or fix logic. Treat them as a seatbelt: always useful, never a reason to drive badly.
Where do I check the result?
In devtools: the Network tab, the first document request, Response Headers. That shows everything that actually reached the browser — including headers your host or CDN added on top of yours.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





