Security headers are instructions your server sends alongside every page, telling the browser to switch on protections it would otherwise leave off. They are unusually good value: most are one line of configuration, they apply to your whole site at once, and they turn off entire categories of attack rather than patching individual bugs. They are also the thing most often missing, because nothing visibly breaks when they are absent.
Here is what each one actually does, in the order worth adding them.
Strict-Transport-Security (HSTS)
Strict-Transport-Security: max-age=15552000; includeSubDomains
You already redirect http to https. The problem is the request before the redirect: someone typing your domain, or clicking an old link, makes one unencrypted request that can be intercepted and answered by somebody else. HSTS closes that gap. Once a browser has seen the header, it refuses to make plain-http requests to your domain for the duration of max-age — it rewrites them to https before sending anything.
The catch: it applies to the whole domain, and with includeSubDomains to every subdomain, for however long you set. If some subdomain cannot do HTTPS, it becomes unreachable in browsers that have seen the header. Roll it out in stages: a short max-age such as 300 first, confirm everything including every subdomain works, then raise it to six months or a year. Treat the preload directive as permanent — removal from the browser preload list takes months.
Content-Security-Policy (CSP)
Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'none'
CSP is the most valuable header on this list and the only one that takes real work. It tells the browser which sources of script, style, image and frame are legitimate for your site. When an attacker manages to inject a <script> tag — through a comment field, a vulnerable plugin, a compromised third-party widget — the browser checks it against your policy and simply declines to run it. The injection still happens; the exploitation does not.
How to roll it out without breaking your site:
- Send it as Content-Security-Policy-Report-Only first. The browser reports what would have been blocked and blocks nothing.
- Collect violations for a week or two across real traffic. You will find third-party scripts you forgot about.
- Decide for each one: allow the specific host, or remove it.
- Switch to the enforcing header once the report is quiet.
Two honest warnings. Inline scripts and inline event handlers (onclick="…") violate a strict policy, and most themes and page builders are full of them — expect to move code into files or use nonces. And a policy containing 'unsafe-inline' for scripts provides very little protection, because that is exactly the thing an injection relies on. A permissive CSP is still better than none, but do not mistake it for the real thing.
Clickjacking protection: X-Frame-Options and frame-ancestors
X-Frame-Options: DENY — or in CSP: frame-ancestors 'none'
Without this, any website can load your pages inside an invisible frame, position it under their own buttons, and have visitors click things on your site while believing they are clicking something else. It matters most on pages where a click does something: logging in, confirming, paying, changing a setting.
Use DENY unless you embed your own pages in your own pages, in which case use SAMEORIGIN. If a partner genuinely needs to embed you, frame-ancestors in CSP is the modern option because it can name specific allowed sites, which X-Frame-Options cannot do reliably. Sending both is fine and sensible while older clients are still around.
X-Content-Type-Options
X-Content-Type-Options: nosniff
Browsers historically guessed the type of a file when the declared type looked wrong. That guessing can be manipulated: a file uploaded as an image but containing script can end up executed as script. nosniff tells the browser to trust the declared Content-Type and nothing else.
This is the easiest win on the list. It is one line, it has essentially no compatibility cost, and there is no staged rollout to plan. If you only do one thing after reading this, do this one — then come back for CSP.
Referrer-Policy
Referrer-Policy: strict-origin-when-cross-origin
By default, when a visitor clicks a link off your site, the full address of the page they were on is sent to the destination. If your URLs carry anything meaningful — a search term, an order reference, a password-reset token, an internal path — you are handing it to every external site your visitors reach, and to their analytics providers.
strict-origin-when-cross-origin sends the full address within your own site, only your domain name to other sites, and nothing at all when leaving HTTPS for http. It is the sensible default and modern browsers now behave this way even without the header — but sending it explicitly means you do not depend on the visitor's browser version.
Permissions-Policy
Permissions-Policy: camera=(), microphone=(), geolocation=()
This controls which powerful browser features your page and anything embedded in it may request. The practical value is about embedded third-party content: an advertising frame or a chat widget inherits your page's permission to ask for the camera, the microphone or the visitor's location unless you say otherwise. Switch off what you genuinely do not use. If you do use one — a store locator needs location — allow it narrowly rather than leaving everything open.
Two headers you can stop worrying about
- X-XSS-Protection. This controlled a filter that has been removed from current browsers. Setting it does nothing useful, and some older guidance recommended values that introduced problems of their own. CSP is what replaced it.
- Public-Key-Pins (HPKP). Deprecated and removed. It was easy to lock yourself out of your own domain for months, and it never gained enough careful operators to be worth the risk. Certificate Transparency monitoring covers the same ground without the foot-gun.
Where to actually set them
Set headers in one place if you can. Options, roughly in order of how much you control:
- Your web server: add_header in nginx, Header always set in Apache. Most reliable, applies to everything including static files.
- Your CDN or proxy: convenient, and worth knowing that it sits in front of your origin — a header set here will also hide the fact that your origin is not setting it, which matters if traffic can ever reach the origin directly.
- Your application or CMS: fine for dynamic pages, but easy to miss static assets and error pages.
Whichever you choose, verify from outside afterwards. The common failure is setting a header in two places with different values, or setting it on the CDN and assuming the origin is covered. Check the real response your visitors get, then check it again after your next hosting or CDN change — that is when headers quietly disappear.