This site contains affiliate links — we may earn a commission if you sign up through them. Details

HTTP Security Headers: Complete Guide to CSP, HSTS, X-Frame-Options and More

HTTP Security Headers: Complete Guide to CSP, HSTS, X-Frame-Options and More

HTTP security headers are powerful mechanisms that protect your website from attacks like XSS, CSRF, and clickjacking. Despite their importance, most websites don't implement them properly, leaving user data vulnerable. This guide explains each header in detail, how to configure them, and how to verify they're working correctly for your site.

What Are HTTP Security Headers and Why Every Website Needs Them

HTTP security headers are directives that servers send in HTTP responses to tell browsers how to handle security concerns. They protect against XSS attacks, MIME sniffing, clickjacking, and referrer leakage. These headers act as an additional security layer even if you've already hardened your code, giving extra confidence that attackers can't easily compromise your users.

Content-Security-Policy (CSP) – Fine-Grained Content Control

Content-Security-Policy is the most powerful header for preventing XSS attacks. It lets you specify which domains can load scripts, stylesheets, images, fonts, and other resources. CSP can enforce restrictions or run in report-only mode to warn you about violations without breaking functionality. Start with report-only mode to test policies before enforcing them.

HSTS (Strict-Transport-Security) – Enforce HTTPS Always

HSTS tells browsers to always connect to your site via HTTPS, even if users type HTTP URLs or click old links. It prevents downgrade attacks and man-in-the-middle interception. The header includes max-age (how long browsers remember this policy) and an optional includeSubDomains directive that extends the policy to all subdomains.

X-Content-Type-Options and X-Frame-Options – Prevent MIME Sniffing and Clickjacking

X-Content-Type-Options: nosniff tells browsers to trust the Content-Type header you send and not guess file types. Without it, browsers might interpret files as HTML despite your explicit content type. X-Frame-Options: DENY prevents your site from being embedded in iframes on other websites, blocking clickjacking attacks where hidden buttons trick users into unintended actions.

Referrer-Policy and Permissions-Policy – Control Referrer Data and Browser Features

Referrer-Policy controls how much referrer information (the URL the user came from) is sent to other websites. For example, strict-origin-when-cross-origin sends only the domain name, not the full URL or query parameters. Permissions-Policy (formerly Feature-Policy) restricts scripts and third-party iframes from accessing sensitive browser APIs like camera, microphone, location, and payment requests.

RecommendedAsiaGB.com — Web Hosting & VPS we recommend. Servers in Thailand & Singapore, SSD storage, DirectAdmin control panel, 24/7 Thai-language support, 99% uptime.

Based in Thailand, ideal for Thai websites and businesses.

Visit AsiaGB →

How to Add Security Headers – Apache .htaccess and Nginx Examples

Adding headers to your site is straightforward. On Apache, add directives to your .htaccess file in the root directory. On Nginx, edit your nginx.conf or virtual server config file. If you use PHP, you can add headers with the header() function, but must do so before any HTML output. Start with simple headers like X-Content-Type-Options and X-Frame-Options, then gradually add the more complex CSP.

Test Your Headers with Online Scanning Tools

After setting up headers, test to ensure browsers actually receive them. Several free online tools scan your headers and provide security ratings. The easiest method is opening your browser's developer tools (F12), going to the Network tab, and checking the Response headers sent by your site. You can also use the curl command-line tool to fetch headers without loading a full page.

Common CSP Mistakes That Break Your Website

Overly strict CSP policies often break parts of your site – scripts won't load, styles disappear, or iframes fail to render. Common mistakes include forgetting to whitelist public CDN domains in script-src, using 'unsafe-inline' which reduces security, forgetting to allow data: URLs for base64-encoded images, or shutting down default-src but forgetting other directives. When broken, use report-only mode to diagnose issues via console errors, then gradually relax the policy.

Security Headers vs SSL Certificate – What Each One Actually Protects

SSL certificates and security headers protect against different threats. SSL encrypts data between browser and server, preventing eavesdropping and network-level man-in-the-middle attacks. Security headers work after data arrives safely by telling browsers to prevent XSS, CSRF, clickjacking, and feature abuse. You need both: SSL for encryption, security headers for application-level defense. Without both, your site has holes.

Frequently Asked Questions

Do security headers affect my SEO ranking?

Not directly, but headers like HSTS can improve performance by reducing redirect chains, which helps SEO. CSP and other headers signal site security, something search engines like Google may consider slightly when ranking similar pages. The primary benefit is user safety, and any SEO benefit is secondary but welcome.

Will adding CSP break my website?

It can if misconfigured. That's why you start with report-only mode, which logs violations without actually blocking content. Review console errors, adjust your policy, then switch to enforce mode. Following this gradual approach prevents breaking your site.

What's the difference between HSTS and an SSL certificate?

An SSL certificate encrypts traffic at the transport layer. HSTS tells browsers to always connect via HTTPS. They work together—you need a valid SSL certificate first, then HSTS ensures browsers stay on HTTPS. Without SSL, HSTS has no effect.

How do I test which security headers my site already has?

Open your browser's developer tools (F12), go to the Network tab, reload your page, and inspect the Response headers section. You'll see Content-Security-Policy, Strict-Transport-Security, and other headers you've configured. Free online header scanner tools will also crawl your site and score its security comprehensively.

Do security headers slow down page load?

No. Headers are simple text directives requiring minimal server resources. Even complex CSP affects only the browser, not server performance. The security benefits far outweigh any performance concerns. In fact, HSTS can reduce redirect chains, actually speeding up page loads.