Website security headers: a practical setup guide

What HTTP security headers do, which ones every website should have, how to set them up without breaking features, and how to test them.

By the UK Technology Hub teamPublished 4 minute read

The short answer

Security headers are instructions your server sends with every page, telling browsers how to behave more safely: always use HTTPS, don't let other sites frame your pages, don't guess file types, limit what scripts can run and restrict access to device features. They're quick to add, cost nothing and close off several common attacks. Content Security Policy needs careful testing so it doesn't block legitimate features.

When a browser loads your website, your server sends HTTP headers alongside the page. Some of these headers tell the browser to enable extra security protections. They don't replace secure code, updates and good passwords, but they add a valuable layer of defence.

The essential headers

Strict-Transport-Security (HSTS)

Tells browsers to only connect to your site over HTTPS for a set period, even if someone types "http." This prevents downgrade attacks on public networks. Enable it once your whole site works properly over HTTPS. Adding the "includeSubDomains" option covers subdomains too, so make sure they all support HTTPS first. If you're seeing HTTPS errors, read our guide to fixing mixed content warnings.

Content-Security-Policy (CSP)

The most powerful header. It tells the browser which sources of scripts, styles, images, fonts and other content are allowed. A good CSP makes cross-site scripting attacks much harder, because injected scripts from unapproved sources won't run.

CSP takes care to set up because modern sites load content from many places: analytics, chat widgets, payment providers, fonts and video. A policy that's too strict breaks features; one that's too loose provides little protection.

X-Content-Type-Options

Set to "nosniff," this stops browsers guessing the type of a file, which prevents certain attacks where an uploaded file is treated as a script.

Frame protection

Prevents other sites embedding your pages in frames, which protects against clickjacking, where users are tricked into clicking hidden buttons. Use the CSP "frame-ancestors" directive, and the older X-Frame-Options header for wider compatibility.

Referrer-Policy

Controls how much information about the previous page is sent when visitors click links. A setting like "strict-origin-when-cross-origin" protects sensitive URL details while keeping useful analytics.

Permissions-Policy

Restricts which browser features your site and embedded content can use, such as camera, microphone, geolocation and payment. Turn off anything you don't use.

How to add headers

Depending on your setup:

  • Hosting platforms like Vercel, Netlify and Cloudflare let you set headers in a configuration file or dashboard.
  • Web servers such as Apache and Nginx use configuration files.
  • CDNs can add headers at the edge. See our CDN guide.
  • WordPress sites can use server configuration or security plugins.
  • Application frameworks can set headers in code or middleware.

Rolling out CSP safely

  1. Inventory everything your site loads: scripts, styles, fonts, images, frames and connections.
  2. Start in report-only mode, which logs violations without blocking anything.
  3. Review reports for a week or two and adjust the policy.
  4. Remove inline scripts where possible, or use nonces or hashes for those you need.
  5. Switch to enforcing mode once reports are clean.
  6. Retest after adding any new tool or widget.

Testing your headers

Free online scanners check which headers your site sends and grade the configuration. Browser developer tools also show headers for each request. Test key pages, including forms, checkout and logins, after any change.

A sensible starting set

If you're starting from nothing, a reasonable first step is HSTS (once HTTPS is solid everywhere), X-Content-Type-Options set to nosniff, frame protection, a sensible Referrer-Policy and a Permissions-Policy that disables unused features. Then work on Content Security Policy in report-only mode. Keeping third-party scripts to a minimum makes CSP much easier; our guide to privacy-friendly analytics shows one way to reduce them.

  • HTTPS with a valid certificate on every page.
  • Secure cookies with Secure, HttpOnly and SameSite attributes.
  • Removing headers that reveal software versions, which give attackers clues.
  • Subresource Integrity for scripts loaded from third-party CDNs, so tampered files are rejected.

Common mistakes

  • Enabling HSTS before every subdomain supports HTTPS.
  • Copying a strict CSP from another site and breaking your own features.
  • Allowing "unsafe-inline" and wildcard sources, which weakens CSP considerably.
  • Forgetting headers on error pages, APIs or subdomains.
  • Never revisiting headers after adding new tools.

Headers are one layer

Security headers complement, but don't replace, keeping software updated, strong authentication, secure coding, backups and monitoring. Think of them as seatbelts: essential, cheap and most effective alongside everything else.

How we help

Every site our web development team builds ships with security headers configured and tested. Our maintenance plans review headers when tools change, and our web app team implements CSP, secure cookies and other protections in custom applications.

Frequently asked questions

Do security headers affect SEO?

Not directly, but they protect your site and users, and HTTPS enforcement supports trust signals.

Can security headers break my website?

Content Security Policy can block legitimate scripts if set too strictly. Use report-only mode first and test carefully.

Which security header is most important?

HSTS and a well-configured Content Security Policy provide the most protection, alongside basic headers like X-Content-Type-Options.

This guide is general information, not legal, tax or financial advice, and details such as prices, features and rules can change. Product and company names mentioned are trademarks of their respective owners and are used only to identify those products; UK Technology Hub is not affiliated with or endorsed by them.

Related guides

All websites & web design guides