HTTP Security Headers: A Beginner's Guide with Ready Values
Security headers are instructions your server sends the browser with every page, closing doors that common attacks exploit. Six headers make a strong start.
HTTP security headers are short lines your server sends to the browser with every page, telling it how to handle that page: always use the encrypted connection, only load scripts from certain places, and never let another site display your page inside itself. Set correctly, they don't change how your site looks or how fast it loads, but they close doors used by common attacks such as script injection and clickjacking. Six headers are enough for a strong start, and this guide explains each one with a simple example, gives you the ready value, and shows exactly how to add and verify it.
Key takeaways
- 1Security headers are set once on the server and protect every page.
- 2Start with HSTS, X-Content-Type-Options, framing protection and a referrer policy.
- 3Roll out Content-Security-Policy gradually, in report-only mode first.
- 4Don't rely on X-XSS-Protection; modern browsers removed it.
- 5HSTS only works if your site already runs on HTTPS with a valid certificate.
- 6Headers are an extra layer; they don't fix injectable code or protect the server itself.
What are HTTP security headers?
When you open a page, a short conversation happens between your browser and the server: the browser sends a request, and the server replies with a response. That response isn't just the page content; it starts with a set of lines called headers — data about the data: what type the content is, how big it is, whether it can be stored, and how the browser should treat it.
Think of an envelope: the paper inside is the page, and what's written on the envelope ("deliver by hand", "do not photograph") is the header. The browser reads those instructions and follows them.
Here's a simplified real response. The security headers are the lines starting with names like strict-transport-security:
HTTP/2 200
content-type: text/html; charset=utf-8
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
referrer-policy: strict-origin-when-cross-origin
x-frame-options: SAMEORIGIN
The key idea for beginners: you write the instructions, the browser enforces them. Headers don't run inside your server and don't clean user input; they're an agreement between you and your visitor's browser. That's why results differ slightly across browsers, and why they never replace writing secure code. For the full list of headers and their meanings, see the MDN HTTP headers reference.
What do security headers protect?
Each header closes one specific door. Together they form layers around the page, from the connection itself down to what the browser is allowed to do inside it:
The protection layers security headers add
- 1Permissions
Permissions-Policy blocks camera, microphone and location unless you need them
- 2Sources
Content-Security-Policy defines where scripts, styles and images may load from
- 3File types
X-Content-Type-Options: nosniff stops the browser guessing a file's type
- 4Framing
frame-ancestors or X-Frame-Options stops your page being displayed inside another site
- 5Connection
Strict-Transport-Security forces the browser to use HTTPS on every visit
The six headers and the attacks they map to
This is a quick index of what each of the HTTP security headers protects against. The ready-to-use values sit directly under each header below, and they're common safe values for a company site — adjust them to your needs:
| Header | Protects against |
|---|---|
| Strict-Transport-Security | Downgrading a visitor to the unencrypted site and intercepting the connection |
| X-Content-Type-Options | Running a file uploaded as an image or text as if it were a script |
| X-Frame-Options / frame-ancestors | Clickjacking by hiding your page inside another site |
| Referrer-Policy | Leaking internal URLs or tokens in the address to other sites |
| Permissions-Policy | Using camera, microphone or location without any need |
| Content-Security-Policy | Script injection (XSS) and loading resources from untrusted sources |
1) Strict-Transport-Security: forcing the browser to use HTTPS
The problem in plain terms: a visitor types example.com, and the browser tries http:// first before redirecting to https://. That first moment is unencrypted, and an attacker on a public network can intercept it and keep the visitor on the unencrypted version without them noticing.
The fix: after the first successful HTTPS visit, the browser stores the instruction "this domain is HTTPS only" for a duration you choose, and upgrades any later HTTP attempt internally before sending it.
Strict-Transport-Security: max-age=31536000; includeSubDomains
max-age is the duration in seconds: 31536000 equals one year. includeSubDomains extends the rule to all your subdomains.
Two conditions without which it does nothing: it must arrive over HTTPS with a valid certificate (the browser ignores it entirely over HTTP), and every domain it covers must actually work on HTTPS.
Because the instruction lives inside the visitor's browser, start short and raise it gradually:
Week one: max-age=300 (5 minutes)
Week two: max-age=86400 (one day)
Week three: max-age=2592000 (30 days)
Once sure: max-age=31536000; includeSubDomains
To roll back immediately, send max-age=0; that clears the instruction from the browser on its next visit. Adding your domain to the browsers' built-in preload list at hstspreload.org makes protection work from the very first visit, but it's a long commitment that's hard to reverse and requires HTTPS on the domain and every subdomain. Only do it after your setup has been stable for months.
2) X-Content-Type-Options: stop guessing file types
The problem in plain terms: an older browser may "sniff" a file's type instead of trusting what the server said. If an attacker uploads a file named photo.jpg that actually contains HTML and script, the browser may render it as a page and run what's inside.
The fix: one word that tells the browser never to guess:
X-Content-Type-Options: nosniff
It also blocks loading a script or stylesheet with the wrong content type — a common mistake that breaks sites silently. This header is very safe and rarely breaks anything, so it's the first one to add.
3) Framing protection: stopping clickjacking
The problem in plain terms: any site can display your page inside an iframe, make that iframe transparent, and place a tempting button on top. The visitor thinks they're clicking "Claim your prize", but they're actually clicking a button on your site because they're logged into it:
- 1A tempting page
The attacker publishes a "prize" or "watch the video" page on social media.
- 2A transparent iframe over the button
Your page (say, delete account or transfer funds) sits in an invisible iframe beneath the visible button.
- 3The click
The visitor clicks the tempting button, and the click passes through to your real button.
- 4The result
An action runs as that visitor without their knowledge, because their browser sent their session along.
The fix: two headers with the same goal, each with its own syntax:
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-Frame-Options: values areDENY(nobody at all) orSAMEORIGIN(your own origin only).ALLOW-FROMis obsolete and unsupported by modern browsers.frame-ancestorsinside CSP: values are'none','self', or explicit origins such ashttps://partner.example. It's newer and more flexible.
Important: if you send both, modern browsers honour frame-ancestors. Keep both to cover older browsers. And if your page genuinely is embedded in another platform (a payment gateway or partner dashboard), add that platform's origin explicitly rather than disabling the protection entirely.
4) Referrer-Policy: controlling what your page address reveals
When a visitor leaves your page for another site, or when your page loads an image from an external domain, the browser sends the address of the originating page in a header called Referer. That's useful for analytics, but it can leak things you didn't intend.
A real example: a password reset link looks like https://example.com/reset?token=abc123. If that page loads any third-party resource, the full URL — token included — can end up in that party's logs.
Referrer-Policy: strict-origin-when-cross-origin
This value means: within your own origin the full URL is sent; to another origin only the origin is sent (https://example.com/) with no path or tokens; and nothing is sent when going from HTTPS down to HTTP. It's already the default in modern browsers, but sending it explicitly guarantees the behaviour everywhere. If your pages routinely carry sensitive tokens, no-referrer is the stricter choice.
5) Permissions-Policy: switching off features you don't need
The problem in plain terms: your page — or a third-party script inside it — can request the camera, microphone or geolocation. On a brochure site or a blog you almost certainly need none of them.
Permissions-Policy: camera=(), microphone=(), geolocation=()
The empty parentheses () mean "nobody": neither the page nor any iframe inside it. If you need a feature on one page, allow only yourself: camera=(self).
A point many people get wrong: this header does not grant permission; it only denies. Final permission still belongs to the visitor through the browser's consent prompt. And if you allow a feature inside a third-party iframe, you also need the allow attribute on that iframe. (This header's old name was Feature-Policy, now replaced.)
6) Content-Security-Policy: the strongest and most precise
It defines where each type of resource may load from, and it's the last line of defence against script injection. It gets its own section next, because it needs a gradual rollout.
Content-Security-Policy: the strongest and most precise
CSP is the strongest protection against script injection, but it's also the easiest way to break your site if applied without testing, because any script or font from a source you didn't list gets blocked. So:
Here's a simple example of why it matters. Suppose your comment field doesn't clean what visitors write, and an attacker posts a comment containing a script tag. With no policy, the browser runs that script in the session of every visitor who reads the comment, so it can steal their login session or act as them. With a policy that only allows scripts from your own origin, the browser refuses to run it:
Without a CSP
- Any script injected into the page executes
- Any external domain can serve a script to your page
- Inline script in the HTML runs unrestricted
- The damage happens in the visitor's browser, not your server logs
With a tuned CSP
- The injected script is blocked and reported
- Unlisted sources are refused automatically
- Inline script needs a random token (nonce) to run
- The attempt reaches you as a report before it harms a visitor
The most common directives and what they control (each directive governs one resource type):
| Directive | What it controls |
|---|---|
| default-src | The fallback for any type you didn't list |
| script-src | Where scripts may come from (the security-critical one) |
| style-src | Where CSS files may come from |
| img-src | Where images may come from |
| font-src | Where fonts may come from |
| connect-src | Addresses your code may call via fetch or WebSocket |
| frame-src | Sites you may embed in iframes inside your page |
| frame-ancestors | Sites allowed to embed your page inside them |
| object-src | Legacy plugins such as Flash; set it to 'none' |
| base-uri | Prevents changing the base address for relative links |
| form-action | Limits where forms may be submitted |
'self' means the same origin: same protocol, host and port. So https://example.com does not cover https://cdn.example.com; you must list it explicitly.
A reasonable starting point for a content or company site:
Content-Security-Policy: default-src 'self'; img-src 'self' data: https:; font-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests
upgrade-insecure-requests is a useful directive that automatically turns any http:// resource into https://, fixing mixed-content warnings without editing every link.
What if your site uses inline script in the HTML? The answer isn't unsafe-inline — it's a nonce: a long, unguessable random token your server generates per response, placed in both the header and the script tag:
Content-Security-Policy: script-src 'nonce-Zk9xR2hTbm9uY2U' 'strict-dynamic'; object-src 'none'; base-uri 'self'
<script nonce="Zk9xR2hTbm9uY2U">
// The inline script your site needs
</script>
The browser only runs what carries the correct token. The attacker doesn't know it, so their injected script is blocked. 'strict-dynamic' means a script you trusted via nonce can load what it needs, freeing you from maintaining long domain lists. For scripts that never change, the alternative is a hash: a fingerprint computed from the script's own content.
How do you read the reports? In report-only mode nothing is blocked, but the browser sends you what would have been blocked, and the console shows a message like:
Refused to load the script 'https://cdn.example-chat.com/widget.js'
because it violates the following Content Security Policy directive: "script-src 'self'".
Read three things from it: the blocked source, the directive that refused it, and the page. Then add that source to that directive if it's trusted. To collect reports automatically instead of opening every visitor's console, add a header naming the reporting destination:
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy-Report-Only: default-src 'self'; report-to csp-endpoint
You then receive a compact report like this:
{
"csp-report": {
"document-uri": "https://example.com/pricing",
"blocked-uri": "https://cdn.example-chat.com/widget.js",
"violated-directive": "script-src-elem",
"disposition": "report"
}
}
disposition is report while monitoring and enforce once enforcing — a quick way to know which stage you're in. (The older report-uri directive still works in many browsers, but report-to is the modern direction.)
Steps to apply them in a safe order
You don't need to add every header in one day. This order minimises the risk of breaking anything:
- 1Check the current state
From the Network tab in developer tools, or with a free scanning tool.
- 2Add the safe headers first
nosniff, Referrer-Policy and framing protection rarely break anything.
- 3Enable HSTS with a short duration
Confirm every subdomain works on HTTPS, then raise it to a year.
- 4Test CSP in report mode
Watch the blocked sources and fix them before enforcing.
- 5Review after every new addition
Any new external tool may need its source added to the policy.
Where do you add the headers?
On most sites they're added in the server configuration (such as the .htaccess file on Apache and LiteSpeed, or the Nginx configuration), or in the framework's own settings as in Next.js, or at your content delivery network if you use one. Security headers are one layer among several, so read our website security basics and password security guide alongside them. If you need to inspect the JWT tokens your site uses, try the JWT decoder, and to generate secure random keys use the random token generator.
Here are ready-made snippets for the most common environments. Start with the short HSTS duration as explained above:
Apache or LiteSpeed — in the .htaccess file at your site root (requires the mod_headers module):
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
Header always set Strict-Transport-Security "max-age=300" "expr=%{HTTPS} == 'on'"
</IfModule>
That last condition sends HSTS only when the connection is HTTPS, which is the correct behaviour. To enable the module on Debian/Ubuntu, run in Bash (Linux):
sudo a2enmod headers
sudo systemctl reload apache2
Nginx — inside the server block handling port 443:
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=300" always;
Then validate and reload in Bash:
sudo nginx -t && sudo systemctl reload nginx
A common Nginx trap: if an inner location block has any add_header of its own, the parent block's headers are not inherited into it. Repeat the headers inside that block, or put them in a shared file you include everywhere.
IIS (Windows) — in the web.config file at your site root:
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<add name="X-Content-Type-Options" value="nosniff" />
<add name="Referrer-Policy" value="strict-origin-when-cross-origin" />
<add name="X-Frame-Options" value="SAMEORIGIN" />
<add name="Permissions-Policy" value="camera=(), microphone=(), geolocation=()" />
<add name="Strict-Transport-Security" value="max-age=300" />
</customHeaders>
</httpProtocol>
</system.webServer>
Next.js — in next.config.js, then rebuild and redeploy:
const securityHeaders = [
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
{ key: 'X-Frame-Options', value: 'SAMEORIGIN' },
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
];
module.exports = {
async headers() {
return [{ source: '/:path*', headers: securityHeaders }];
},
};
If you use a CDN or managed hosting, the easiest place may be its dashboard (header modification rules). What matters is adding the headers in exactly one place; sending the same header from both your server and the network can produce two conflicting values that the browser ignores.
How do you verify your HTTP security headers work?
You have three methods, from easiest to most precise.
Method one: from the browser, no commands at all
- 1Open your site
Press F12 to open developer tools (or right-click and choose "Inspect").
- 2Go to the Network tab
Reload the page with F5 so the request is recorded.
- 3Click the first request in the list
It's the page document itself, not an image or script.
- 4Read the Response Headers section
Look for the six header names and their values.
Method two: from PowerShell (Windows)
Open PowerShell by searching for powershell in the Start menu, or press Win + X and choose Terminal. Paste these two lines and press Enter:
$response = Invoke-WebRequest -Uri "https://example.com" -UseBasicParsing
$response.Headers.GetEnumerator() | Sort-Object Key | Format-Table Key, Value -AutoSize
To check just the six headers by name, run:
'Content-Security-Policy','Strict-Transport-Security','X-Content-Type-Options','X-Frame-Options','Referrer-Policy','Permissions-Policy' |
ForEach-Object { "{0} = {1}" -f $_, $response.Headers[$_] }
Each line shows a header name and its value; an empty value means the header isn't being sent. Replace example.com with your domain, and check the http:// version, the https:// version, and at least one subdomain.
Method three: from CMD (the Windows Command Prompt)
Press Win + R, type cmd, press Enter. curl.exe is built into modern Windows 10 and Windows 11:
curl.exe -I https://example.com
To filter the output down to security headers:
curl.exe -sSI https://example.com | findstr /I "strict-transport content-security x-frame x-content-type referrer permissions"
An important difference between the two shells: in PowerShell 5.1 the bare word curl is an alias for Invoke-WebRequest, not the real tool, so write curl.exe explicitly as shown. In CMD, write curl.exe too, to avoid any ambiguity.
If a header looks missing but you're sure you added it, try a GET instead of HEAD — some servers and networks respond differently:
curl.exe -sS -D - -o NUL https://example.com
For an external overview: enter your domain at securityheaders.com for a quick grade showing which headers are present and missing. It's useful as a short report to share with your team, but it doesn't replace reading the values yourself — a header being present doesn't mean its value suits your site.
If something breaks after applying them, here's a quick diagnosis:
| Symptom | Likely cause and fix |
|---|---|
| Fonts or icons disappeared | font-src or style-src doesn't include the font's domain; add it |
| Chat or analytics stopped | script-src or connect-src is missing the tool's domain |
| Page won't load inside a partner platform | frame-ancestors blocks it; add the partner origin explicitly |
| Header doesn't show in the check | Added in the wrong place, stripped by the network, or HEAD responds differently |
| Mixed-content warning | A resource still loads over HTTP; enable upgrade-insecure-requests |
Wrong practices to avoid
- Relying on
X-XSS-Protection: it was removed from modern browsers and in some cases opened a vulnerability instead of closing one. Use CSP instead. - Adding
unsafe-inlineandunsafe-evalto silence errors: this empties CSP of its meaning. Use a nonce or a hash. - Raising
max-ageto a year on day one: any subdomain without HTTPS will stop working for your visitors for a year. - Confusing CORS with security headers: a header like
Access-Control-Allow-Origin: *relaxes restrictions so other sites can read your resources. It isn't a protection header, and misconfiguring it leaks data. - Leaving headers that reveal details: such as
ServerandX-Powered-Bywith version numbers. Removing them doesn't stop a breach, but it reduces the free information an attacker gathers. - Checking only once: every new plugin or marketing tool may need a policy review.
Conclusion
Six security headers added once protect every page: HSTS for the connection, nosniff for file types, framing protection, a referrer policy, a permissions policy, and a content security policy you roll out gradually in report-only mode. Check your site today and add the safe ones first, then verify the result with a single command in PowerShell or CMD — and always keep a rollback plan. If you'd like a website built on sound security foundations from day one, see our website development service.
Frequently asked questions
What are HTTP security headers?
Lines the server sends with every response telling the browser how to handle the page: always use HTTPS, where scripts may load from, and whether the page may be shown in a frame on another site.
How do I check my site's security headers?
Open your browser's developer tools, go to the Network tab and select the page request to see the response headers. Free scanners also give you a grade and a list of what's missing.
Can a Content Security Policy break my site?
Yes, if applied all at once. Start in report-only mode (Content-Security-Policy-Report-Only) to see what would be blocked, then enforce it once sources are fixed.
Is X-XSS-Protection still useful?
No. Modern browsers removed the feature it controlled; real protection today comes from a Content Security Policy.
Sources & references

I believe technology is useful when it solves a clear problem — not when it's adopted because it's trendy. AI is a powerful tool for speeding up research, writing, analysis and automation, but it needs human expertise to review, guide and verify accuracy. That's why I prefer to start by understanding the business and its goals, then choose the simplest solution that delivers a measurable result, and build audience trust with honest, useful content.
- Reader rating
- 5.0(2)
- Published articles
- 81
- #security headers
- #csp
- #hsts
- #web security
How would you rate this article?
Be the first to rate
Comments
No comments yet. Share your thoughts!