Developer Tools • 5 min read

HTTP Security Headers and Content-Security-Policy for Beginners

What HSTS, CSP, X-Frame-Options and the other security headers do, how to roll them out without breaking your site, and how to check them.

Published: 2026-11-12 • Last Updated: 2026-11-12 • By Mohammed Zawwar Hussain

Interactive Online Tools Referenced in this Guide

What security headers are for

A security header is an instruction your server sends along with a page, telling the browser to apply an extra rule. They do not fix bugs in your code, and they do not replace careful handling of user input. What they do is limit the damage when something goes wrong: a script injected into a page, your page loaded inside someone else’s frame, or a visitor tricked into using an unencrypted connection.

Because the browser enforces them, headers are one of the cheapest improvements you can make. Most are one line of server configuration. The risk is that a strict rule can also block things you meant to allow, so each header below comes with a note on rolling it out safely.

The headers that matter most

HeaderWhat it doesA common starting value
Strict-Transport-SecurityTells browsers to use HTTPS only for your domainmax-age=31536000
Content-Security-PolicyLists where scripts, styles, images and frames may load fromdefault-src 'self' (then add what you need)
X-Frame-OptionsStops other sites from putting your page in a frame (clickjacking)DENY or SAMEORIGIN
X-Content-Type-OptionsStops the browser guessing a file’s typenosniff
Referrer-PolicyControls how much of your URL is sent to other sitesstrict-origin-when-cross-origin
Permissions-PolicyTurns off browser features you do not use, such as the cameracamera=(), microphone=(), geolocation=()

HSTS is the one to be careful with. Once a browser has seen it, it will refuse plain HTTP for your domain until max-age runs out. Make sure every subdomain you add the directive for really supports HTTPS, and start with a short max-age such as a day or a week before moving to a year.

Understanding Content-Security-Policy

CSP is the most powerful header and the one most often misconfigured. It is a list of directives, each naming a type of resource and the sources allowed for it:

Content-Security-Policy: default-src 'self'; img-src 'self' https:; script-src 'self' https://cdn.example.com
  • default-src is the fallback for any type you do not list separately.
  • script-src controls JavaScript. This is the directive that stops injected scripts.
  • style-src, img-src, font-src, connect-src, frame-src, media-src cover the other resource types. connect-src covers fetch and XHR requests.
  • object-src is best set to 'none' unless you use plugins.
  • form-action limits where your forms can submit to.

Avoid 'unsafe-inline' and 'unsafe-eval' in script-src where you can. Allowing inline scripts removes much of the protection, because an injected script is also inline. If your pages need inline scripts, use nonces or hashes instead.

The CSP Generator lets you switch directives on and off, choose the sources for each, and see a strictness rating as you go. It can export the finished policy for Nginx, Apache and Cloudflare, so you can paste it into the right place.

Roll out CSP without breaking your site

  1. Start in report-only mode. Send the policy as Content-Security-Policy-Report-Only. The browser then logs violations in the developer console but blocks nothing.
  2. Browse your own site. Open every kind of page, log in, submit forms and use the checkout. Note every violation in the console.
  3. Add the sources you actually use. Analytics, fonts, embedded video and payment widgets are the usual ones.
  4. Remove inline code where you can. Move inline scripts into files or give them a nonce.
  5. Switch to enforcing mode. Change the header name to Content-Security-Policy once the console is quiet.
  6. Keep checking. Whenever you add a third-party script, add its source to the policy.

A policy that is too loose is only a little better than none, and a policy that is too tight breaks pages. Report-only mode is how you find the balance.

Check what your server really sends

It is easy to add a header in one place and find it missing because a proxy, CDN or error page overrides it. To check, open your browser’s developer tools, go to the Network tab, reload the page, click the first request and copy the response headers. Paste them into the Security Headers Checker, which reports which headers are present, scores the set out of 100 and suggests a value for each one that is missing or weak.

The checker analyses only the text you paste, in your browser. It does not fetch your site, so test the pages that matter: the home page, a login page and an error page can all behave differently. For a quick look at how your server responds, the Website Response Time Checker shows the HTTP status, time to first byte and whether the response was compressed.

What headers cannot do

  • A full score does not mean a site is secure. Headers are one layer alongside updated software, validated input, strong passwords and access control.
  • CSP does not fix cross-site scripting bugs. It reduces what an attacker can do with one.
  • X-Frame-Options is being replaced by the frame-ancestors directive in CSP. Sending both is fine.
  • HTTPS itself depends on a valid certificate. You can read a certificate’s dates and fingerprints with the SSL Certificate Decoder, but it does not check the chain or revocation.

Treat headers as a checklist you revisit after every significant change to your site, not as a one-off task.

Frequently Asked Questions

Which security header should I add first? ↓
X-Content-Type-Options: nosniff and a Referrer-Policy are low risk. Add HSTS once you are sure the whole site works on HTTPS, and roll out CSP in report-only mode.
Will CSP stop all cross-site scripting? ↓
It greatly limits what injected scripts can do when the policy is strict, but it is a second line of defence. You still need to escape and validate input.
Do these headers affect SEO? ↓
Not directly. HTTPS is a lightweight ranking signal, and a broken CSP that blocks your own scripts can harm the page, so test before enforcing.
Where do I add the headers? ↓
In your web server or CDN configuration, for example an add_header line in Nginx, a Header directive in Apache, or a response header rule in Cloudflare.