All guides

HTTP Security Headers Guide: Hardening HSTS, CSP, X-Frame-Options, and Cross-Origin Policies

Configure HSTS, Content-Security-Policy, nosniff, Referrer-Policy, COOP, and CORP properly—with real Nginx, Apache, and Laravel header examples.

Deploying HTTPS with a valid TLS certificate encrypts traffic in transit, yet leaves a surprising number of browser-side attacks wide open: reflected or stored XSS execution, clickjacking overlays inside invisible iframes, MIME-type confusion, protocol downgrade attacks, and cross-origin data leaks.

HTTP security response headers instruct the visitor's browser to lock down those exact execution paths—at virtually zero runtime cost. Better yet, strong HTTPS + HSTS hygiene also feeds technical trust signals checked across modern web audits. Let's inspect the 8 core HTTP security headers you should audit on every production web app, what each prevents, and safe configurations that won't break your frontend scripts.

The 8 Core Security Headers Every Production Site Should Audit

When you inspect a public URL with Security Headers Checker, the audit evaluates these eight response headers:

  1. Strict-Transport-Security (HSTS)
  2. Content-Security-Policy (CSP)
  3. X-Content-Type-Options
  4. X-Frame-Options
  5. Referrer-Policy
  6. Permissions-Policy
  7. Cross-Origin-Opener-Policy (COOP)
  8. Cross-Origin-Resource-Policy (CORP)

Let's break down each one from highest security impact to defense-in-depth hardening.

1. Strict-Transport-Security (HSTS)

Without HSTS, a user typing yourdomain.com into a browser bar hits http:// first before receiving a 301 redirect to https://. An attacker on public Wi-Fi can intercept that initial plaintext HTTP request before the redirect happens (SSL stripping).

HSTS (RFC 6797) tells browsers to remember—for a full year—that your domain must only ever be contacted over HTTPS:

Strict-Transport-Security: max-age=31536000; includeSubDomains

Important rollout warnings:

  • Never deploy HSTS on http:// responses—browsers ignore it on plaintext connections; send it strictly over HTTPS after verifying your TLS certificate chain with SSL Checker.
  • Test before adding includeSubDomains or preload: Adding includeSubDomains forces every subdomain (staging., oldblog., mail.) onto HTTPS; if an internal or legacy subdomain lacks a valid certificate, visitors will hit hard unbypassable browser errors. Start with max-age=300, verify all subdomains, then raise to 31536000 (1 year).

2. Content-Security-Policy (CSP)

Content-Security-Policy is your strongest defense against Cross-Site Scripting (XSS) and unauthorized script injection. It declares an explicit allowlist of origins from which scripts, styles, fonts, images, frames, and fetch() requests may load or execute.

A strict starter policy for an application without inline scripts (or using per-request nonces):

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data: https:; font-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; upgrade-insecure-requests

Common CSP mistakes that create false security:

  • Adding 'unsafe-inline' and 'unsafe-eval' to script-src: That permits injected <script> tags and eval() payloads to execute, defeating CSP's primary XSS protection. Instead, move JavaScript into external static files (which also speeds up caching) or attach cryptographic per-request 'nonce-<random>' attributes.
  • Forgetting object-src 'none' and base-uri 'self': Omitting object-src 'none' can leave legacy plugin injection paths open, while missing base-uri 'self' permits <base href="https://evil.test/"> injection to hijack relative script paths.
  • Rolling out directly in blocking mode: Always deploy new policies first with Content-Security-Policy-Report-Only and a report-uri / report-to endpoint so you spot broken third-party analytics, payment widgets, or ad slots before locking out real visitors.

Draft and customize policy directives interactively with CSP Generator.

3. X-Content-Type-Options

Browsers historically tried to be helpful by "MIME-sniffing" files—guessing that a file served as text/plain might actually contain executable JavaScript or HTML. Attackers exploited this by uploading malicious SVG/HTML payloads disguised as harmless avatars or text uploads.

Lock down MIME sniffing globally with one header:

X-Content-Type-Options: nosniff

Make sure your web server and CDN also send accurate Content-Type headers on .js (application/javascript), .css (text/css), and .json (application/json) assets, or nosniff will rightfully block mismatched files.

4. X-Frame-Options (and CSP frame-ancestors)

Clickjacking tricks users into clicking invisible buttons loaded inside an attacker's transparent <iframe> overlaid on top of a harmless game or prize button.

Block unauthorized framing completely:

X-Frame-Options: DENY

Or allow embedding strictly on your own origin (e.g., if you use same-origin preview frames):

X-Frame-Options: SAMEORIGIN

Keep CSP frame-ancestors 'none' (or 'self') alongside X-Frame-Options—frame-ancestors supersedes X-Frame-Options in modern browsers and supports multiple explicit partner domains if needed, while X-Frame-Options covers older user agents.

5. Referrer-Policy

When a user clicks an external link on your site—say from an authenticated admin URL /reset-password?token=abc or private customer dashboard—the browser sends a Referer header revealing that full URL to the destination server unless restricted.

The ideal privacy-preserving default that still gives analytics referrals at the domain level:

Referrer-Policy: strict-origin-when-cross-origin

This sends the full path on same-origin navigations, sends only the scheme+host (https://yourdomain.com/) across HTTPS-to-HTTPS external links, and sends nothing when downgrading from HTTPS to HTTP.

6. Permissions-Policy

Even if an embedded third-party script or ad frame loads on your page, it shouldn't silently request sensitive hardware APIs your application doesn't use. Permissions-Policy explicitly disables browser features site-wide:

Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()

If your application legitimately includes a camera QR scanner or microphone recorder on a specific tool route, scope (self) for that capability while keeping everything else locked to ().

7. Cross-Origin-Opener-Policy (COOP)

COOP isolates your top-level browsing context from cross-origin windows opened via window.open() or links without rel="noopener", mitigating Spectre-style side-channel leaks and cross-window window.opener tampering:

Cross-Origin-Opener-Policy: same-origin

(Note: If your site uses third-party OAuth login popups such as "Sign in with Google" that communicate back via window.opener, use same-origin-allow-popups on login routes.)

8. Cross-Origin-Resource-Policy (CORP)

CORP prevents arbitrary third-party websites from embedding your internal images, scripts, or JSON assets via <img> or <script> tags unless permitted:

Cross-Origin-Resource-Policy: same-site

(For truly public CDN/static icon assets meant to load on external sites or feeds, use cross-origin; for sensitive user/admin assets, set same-origin.)

Copy-Paste Configurations for Nginx, Apache, and Laravel

Nginx (server block)

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Resource-Policy "same-site" always;

(Important Nginx gotcha: add_header directives in a nested location block override all parent add_header directives in the server block! Either define shared security headers inside an included snippet repeated across location blocks or apply them in your application middleware.)

Apache (.htaccess or VirtualHost)

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
    Header always set Cross-Origin-Opener-Policy "same-origin"
    Header always set Cross-Origin-Resource-Policy "same-site"
</IfModule>

Laravel Global HTTP Middleware

Applying headers inside a Laravel middleware guarantees consistent coverage across dynamic routes even if Nginx rewrites or reverse proxies sit upstream:

$response->headers->set('X-Content-Type-Options', 'nosniff');
$response->headers->set('X-Frame-Options', 'SAMEORIGIN');
$response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin');
$response->headers->set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
$response->headers->set('Cross-Origin-Opener-Policy', 'same-origin');

Watch Out for Overly Permissive CORS Headers

Security headers restrict what browsers execute on your pages, while CORS (Access-Control-Allow-Origin) controls which external sites can read responses from your APIs. Never combine Access-Control-Allow-Origin: * (or dynamically reflect arbitrary Origin headers) alongside Access-Control-Allow-Credentials: true on authenticated routes. Explicitly allowlist trusted origins using CORS Header Generator.

Frequently Asked Questions

Do HTTP security headers improve SEO rankings directly?

HTTPS itself is a confirmed lightweight ranking signal, and security headers protect site integrity, prevent malicious ad/script injection that triggers Google Safe Browsing warnings, and lift technical trust signals evaluated by domain audits (see our Technical SEO Audit Checklist).

Why does a security header checker show fewer headers on some sites?

When inspecting a page directly from browser JavaScript, CORS hides non-safelisted response headers unless the request routes through a server-side HTTP relay. Tools backed by a server-side fetch relay—like SmartWebToolz's Security Headers Checker and HTTP Headers Checker—read the full raw response headers sent by the origin server.

Can setting Cross-Origin-Resource-Policy: same-origin break Google AdSense or embeds?

Yes, if applied globally to pages or third-party integrations improperly—or conversely, Cross-Origin-Embedder-Policy: require-corp will block third-party ads, fonts, and external iframes that don't send CORP headers. Test third-party embeds carefully after tightening cross-origin headers.

Test Your Live Security Headers Right Now

Check what your server actually returns in production—not what you think your config says:

Put this guide to work

Try the tools mentioned here: all free tools or browse by category.