Security headers are HTTP response headers your web server sends with every page to tell the browser how to handle that page safely. They switch on protections against cross-site scripting, clickjacking, MIME-sniffing and protocol downgrade attacks. The essential set is HSTS, Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy and secure cookie attributes.
Key takeaways
- Security headers are instructions your server sends with each page; the browser enforces them, so they cost almost nothing to add.
- HSTS and Content-Security-Policy do most of the work — HSTS forces HTTPS, CSP controls what scripts and resources may load.
- Set them with safe values first, test with a headers checker, and roll out CSP in report-only mode before enforcing it.
Your website sends more than a page to every visitor. With each response, your server can also send a short set of instructions that tell the browser how to treat that page — and those instructions stop entire classes of attacks before they start.
What are security headers?
Security headers are HTTP response headers that your web server sends with every page, telling the browser to switch on extra protections for that page. They cost nothing to add, need no code changes in your app, and they are enforced by the browser itself.
Think of them as house rules printed on the door. The browser reads the rules when it loads your page and then refuses to do unsafe things: it will not run a script from an unknown source, will not let another site frame your page, and will not downgrade the connection to plain HTTP.
Headers are not a firewall and they do not fix vulnerabilities in your code. They are defense in depth: when an attacker finds a flaw, headers limit what the flaw can be used for. The OWASP HTTP Headers Cheat Sheet calls them "a great booster for web security with easy implementation" — and OWASP is right. Most sites can add the core set in under an hour.
How do security headers work?
When a browser requests a page, the server replies with headers first and the page body second. Security headers ride along in that header block, so the browser knows the rules before it renders anything, runs any script, or loads any resource.
For example, a response that includes Content-Security-Policy: default-src 'self' tells the browser to load scripts, styles and images only from your own domain. If an attacker injects a script tag pointing at their server, the browser blocks it — even though the vulnerable code is still there.
Two details matter. First, headers only protect pages the browser renders; a command-line API client ignores most of them. Second, headers are instructions, not guarantees: an old browser that does not understand a header simply skips it, which is why you set several overlapping protections rather than relying on one.
Which security headers should you set?
OWASP lists more than a dozen security headers, but nine do nearly all of the work for a typical website. Set the first seven on every HTML page, add secure cookie attributes, and remove the headers that leak information about your stack.
| Header | What it stops | Recommended value (OWASP) |
|---|---|---|
| Strict-Transport-Security | Protocol downgrade, SSL stripping | max-age=63072000; includeSubDomains; preload |
| Content-Security-Policy | Cross-site scripting, data injection | default-src 'self'; script-src 'self' (tighten from there) |
| X-Content-Type-Options | MIME-sniffing attacks | nosniff |
| X-Frame-Options | Clickjacking | DENY (or CSP frame-ancestors) |
| Referrer-Policy | Referrer data leaking private URLs | strict-origin-when-cross-origin |
| Permissions-Policy | Abuse of camera, microphone, geolocation | geolocation=(), camera=(), microphone=() |
| Cross-Origin-Opener-Policy | Cross-window attacks (Spectre class) | same-origin |
| Cross-Origin-Resource-Policy | Cross-origin resource theft | same-site |
| Set-Cookie attributes | Cookie theft, cross-site request forgery | Secure; HttpOnly; SameSite=Lax |
The sections below explain the four that cause the most confusion, and the ones you should never set at all.
Strict-Transport-Security (HSTS)
HSTS tells the browser to load your site over HTTPS only, even if the user types http:// or clicks an old HTTP link. It is your main defense against SSL-stripping attacks, where an attacker on a public Wi-Fi network downgrades the connection to plain HTTP.
OWASP recommends Strict-Transport-Security: max-age=63072000; includeSubDomains; preload. The max-age of 63,072,000 seconds is two years. Before you commit to that, start with a short value such as max-age=86400 (one day) and raise it over a few weeks. If your certificate expires or a subdomain has no HTTPS, a long HSTS policy can lock real visitors out until it expires.
The preload flag lets you submit your domain to the HSTS preload list built into browsers, so even a first-time visitor gets HTTPS. MDN notes that preload requires max-age of at least one year and includeSubDomains. Only submit when you are certain every subdomain supports HTTPS, because removal from the list is slow.
Content-Security-Policy (CSP)
CSP is the most powerful header and the hardest to get right. It is an allowlist: it tells the browser exactly which sources may supply scripts, styles, images, fonts and frames on a page. A good CSP turns a cross-site scripting hole from "attacker runs any script" into "attacker runs nothing".
Start from a restrictive baseline and add only what your site actually uses:
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; upgrade-insecure-requests
OWASP warns that CSP is complex to configure and maintain, and that it is meaningless on a JSON API response that no browser renders. The safe rollout is to publish the same policy as Content-Security-Policy-Report-Only first, watch the violation reports, fix what breaks, and only then switch to enforcing. Use nonces or hashes for inline scripts rather than 'unsafe-inline', which defeats much of the protection.
X-Frame-Options and clickjacking
X-Frame-Options stops other sites from embedding your page in a frame, iframe or object element. Without it, an attacker can layer your real login or payment page invisibly under a fake button, so the visitor clicks something they cannot see — a clickjacking attack.
OWASP recommends X-Frame-Options: DENY. The modern replacement is CSP's frame-ancestors 'none' directive, which does the same job and is understood by current browsers. Send both: CSP covers modern browsers and X-Frame-Options covers the older ones. OWASP notes the header only helps on responses a user can interact with; it does nothing on a redirect or a JSON API response.
Secure cookie attributes
Set-Cookie is not a security header itself, but its attributes decide whether a stolen cookie is useful to an attacker. Set all three on session cookies: Secure (only sent over HTTPS), HttpOnly (JavaScript cannot read it, so XSS cannot steal it) and SameSite=Lax (not sent on cross-site requests, which blunts cross-site request forgery).
Use SameSite=Strict only where it will not annoy users: with Strict, a cookie is not sent even when the user follows a normal link from another site to yours, so they arrive logged out. Lax is the usual compromise, and it is also the default in modern browsers when no SameSite value is set.
Which headers should you stop sending?
Three once-common headers are now deprecated and should be removed or switched off: X-XSS-Protection, Expect-CT and Public-Key-Pins. Sending them can weaken security rather than improve it, so a modern headers audit flags them as problems, not wins.
X-XSS-Protection switched on an old browser XSS filter that could itself create XSS vulnerabilities in otherwise safe sites. OWASP recommends setting it to X-XSS-Protection: 0 or not sending it at all, and relying on CSP instead. Expect-CT is unnecessary now that browsers require Certificate Transparency, and MDN recommends removing it. Public-Key-Pins (HPKP) could permanently brick a site if a key changed, and OWASP lists it as deprecated.
Also remove the headers that advertise your stack: Server, X-Powered-By and X-AspNet-Version tell an attacker exactly which software and version to look up exploits for. OWASP recommends removing them or replacing them with generic values. Attackers can fingerprint a server other ways, but there is no reason to hand them the answer.
How do I check my website's security headers?
Checking takes less than a minute: scan your site with a headers checker, read the report, fix the missing headers, and scan again. Repeat the check after every server or CDN change, because a new proxy or caching layer can strip headers silently.
Run our free security headers checker against your domain to see exactly which headers your site sends and which are missing. For a second opinion, Mozilla's HTTP Observatory (now part of MDN) grades the same set of headers and explains each finding. You can also check manually: open your browser's developer tools, load a page, open the Network tab, click the first request and read the response headers.
Check more than the homepage. Headers are often set per location, so a marketing page can pass while the login page or the checkout flow fails. Test the pages that handle passwords, payments and personal data, and test both the www and bare domains.
How do I set security headers on my server?
You set headers in your web server or CDN configuration, not in your application code. Add them once at the server level and they apply to every page. The examples below use OWASP's recommended values.
On nginx, add the headers inside the server block and reload:
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), camera=(), microphone=()" always;
add_header Content-Security-Policy "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'" always;
On Apache, the same set goes in your virtual host or .htaccess with Header always set. If you use Cloudflare, AWS CloudFront or another CDN, set the headers there instead — and confirm the CDN does not strip the headers your origin sends. A free SSL checker will also confirm your certificate is valid, which HSTS depends on.
What are the common mistakes with security headers?
The most common mistakes are a CSP that is too permissive to help, an HSTS policy that is too aggressive to undo, and headers that only exist on some pages. All three are easy to avoid once you know what to look for.
- A CSP full of
'unsafe-inline'and*. Allowing inline scripts and every origin makes CSP decorative. If a third-party script needs inline code, move it to a file or use a nonce instead. - HSTS with a two-year max-age on day one. If anything goes wrong with HTTPS, visitors cannot reach you for two years. Start at one day, then a week, then a month, and only then two years.
- Headers on the homepage but not the login page. Attackers target the pages that handle credentials. Set headers at the server level so they apply everywhere, and scan the sensitive pages separately.
- Forgetting the CDN. A CDN can add, replace or strip headers. After any CDN change, re-scan and confirm the headers your visitors actually receive.
- Setting headers in a
<meta>tag. Some headers, including CSP, can be set in a meta tag, but meta-based CSP cannot useframe-ancestorsor reporting, and other headers cannot be set that way at all. Use real HTTP headers.
What can't security headers do?
Security headers limit damage; they do not remove the underlying weakness. A CSP blocks an injected script from running, but the injection hole is still there for an attacker to probe. Headers are one layer, not a substitute for fixing the code.
They also cannot protect data once it leaves the browser, stop an attacker who has already stolen a valid session cookie, or help a visitor whose browser is years out of date. And they do nothing for non-browser clients: a mobile app or a script calling your API will ignore most of them, which is why APIs need their own authentication and rate limiting.
Think of headers as part of a baseline, alongside HTTPS, patched software, strong authentication and backups. Our small business cybersecurity checklist covers the rest of that baseline in one place.
The bottom line
Security headers are the cheapest meaningful security upgrade most websites can make: a few lines in your server configuration, no code changes, and the browser does the enforcement for free. Start with HSTS, X-Content-Type-Options, X-Frame-Options, Referrer-Policy and Permissions-Policy, then work through CSP carefully in report-only mode.
Scan your site today with the free security headers checker to see exactly what is missing, fix the easy headers first, and re-check after every change. A site with the full set of headers is a much harder target — and it takes less than an hour to get there.
Frequently asked questions
What are security headers in simple terms?
Security headers are extra instructions your web server attaches to every page it sends. They tell the visitor's browser to switch on protections — for example, to load the site only over HTTPS, to block the page from being framed by another site, or to refuse scripts from unknown sources.
Do security headers slow down a website?
No. Headers add a few hundred bytes to each response, which is negligible. They do not run code on the server and they do not change how fast a page renders. The only header that can cause trouble is a badly written Content-Security-Policy, which can block your own scripts if it is too strict.
Are security headers a replacement for HTTPS?
No. Security headers build on HTTPS; they do not replace it. HSTS, for example, only works when it is sent over an HTTPS connection, and browsers ignore it over plain HTTP. Get a valid TLS certificate first, then add headers.
What is the difference between HSTS and CSP?
HSTS (Strict-Transport-Security) controls the connection: it tells the browser to use HTTPS only for your domain. CSP (Content-Security-Policy) controls the content: it tells the browser which scripts, styles, images and frames are allowed to load on a page. You need both.
How do I check my website's security headers?
Use a free headers checker: enter your domain and it lists every security header your server sends, flags the missing ones and grades the result. You can also open your browser's developer tools, load a page, and read the response headers in the Network tab.
Can security headers break my website?
Yes, if they are too strict. A CSP that blocks your analytics script, payment widget or fonts will break visible features. HSTS with a long max-age can lock visitors out if your certificate expires. Roll out CSP in report-only mode first and start HSTS with a short max-age.
Do APIs need security headers too?
Mostly yes, but a smaller set. API responses that return JSON are not rendered as pages, so framing and script headers matter less. OWASP and MDN recommend HSTS, X-Content-Type-Options and a minimal CSP (default-src 'none'; frame-ancestors 'none') on API responses.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the OWASP HTTP Headers Cheat Sheet, the OWASP Secure Headers Project and MDN's HTTP header documentation.
How we write and fact-check →