Adding the six core security headers, with config for Apache, Nginx and WordPress

To add security headers, you set six HTTP response headers on the pages your server serves: HSTS, Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, Referrer-Policy and Permissions-Policy. You add them once in your web server config (Apache .htaccess or an Nginx server block) or, on a typical shared host, via a small WordPress snippet.

How to fix6 min read

Five of the six can be set safely in one go with sensible defaults. The sixth, Content-Security-Policy, needs a careful rollout, because a strict policy can block your own scripts, styles and fonts. Below are values you can copy and adapt for each platform.

The free EB Security scan reads these headers from your live responses and tells you which are present, which are missing and what each one does, so you can confirm your changes worked in about ten seconds.

The six headers and what each one does

Before you paste anything, it helps to know what you are switching on. These are the headers the scan looks for, and the job each one does.

  • HSTS (Strict-Transport-Security): tells browsers to only ever connect over HTTPS. Needs a real max-age (a year is normal).
  • Content-Security-Policy (CSP): controls which sources of scripts, styles, images and frames the browser is allowed to load. The strongest header, and the one to roll out carefully.
  • X-Frame-Options: stops other sites embedding yours in an iframe (clickjacking protection).
  • X-Content-Type-Options: nosniff, stops the browser guessing file types and running something unexpected.
  • Referrer-Policy: controls how much of your URL is sent when a visitor clicks away.
  • Permissions-Policy: switches off browser features you do not use, such as camera, microphone and geolocation.

Apache (.htaccess)

If your host runs Apache (common on shared hosting), add this to your site's .htaccess file. It needs mod_headers, which is enabled on almost every host. Set HSTS only once you are certain every page works over HTTPS, because it is hard to undo quickly.

  • Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
  • 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=()"
  • Add CSP last, in report-only mode first (see the CSP section below).

Nginx

On Nginx, add the headers inside the server block (or a location block) and reload the config. Using add_header with the always flag means the header is sent on error responses too. One catch with Nginx: if you add any add_header inside a location block, the inherited headers from the server block stop applying in that location, so repeat them where needed.

  • 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;
  • Reload after editing: run nginx -t to test, then reload the service.

How to add security headers in WordPress (no server access)

If you cannot edit the server config, you can send the headers from WordPress itself. Add this to your child theme's functions.php or a small site-specific plugin. It hooks send_headers so the headers go out on normal page loads.

This is the least robust option, because it only covers requests that reach PHP, not static files served directly, but on locked-down shared hosting it is often the only route.

  • add_action('send_headers', function () {
  • header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
  • header('X-Content-Type-Options: nosniff');
  • header('X-Frame-Options: SAMEORIGIN');
  • header('Referrer-Policy: strict-origin-when-cross-origin');
  • header("Permissions-Policy: camera=(), microphone=(), geolocation=()");
  • });

Free, instant, no account

See your site's security grade in seconds.

Free, instant, and no account needed to see your grade.

Scan my site free

Plain-English A to E grade in about 10 seconds. No account needed to see it.

Roll out Content-Security-Policy carefully

CSP is the one header that can break your own site, so treat it differently. A strict policy that forgets your analytics script, web font or embedded video will silently stop those things loading. The safe approach is to start in report-only mode, which logs what would have been blocked without actually blocking anything.

Begin with Content-Security-Policy-Report-Only, watch the browser console and any report endpoint for a week or two, add every legitimate source you see, and only then switch the header name to Content-Security-Policy to enforce it. Inline scripts and styles are the usual sticking point: you will either need to move them to files or use nonces or hashes.

  • Start: Content-Security-Policy-Report-Only: default-src 'self'; (report only, blocks nothing)
  • Add the real sources your pages use (script, style, font, image, frame).
  • Test every template, especially forms, checkout and embeds.
  • Switch to Content-Security-Policy: to enforce once the report log is quiet.

Check your work, and know the limits

After deploying, load your site and open the browser developer tools Network tab to read the response headers, or run the free EB Security scan, which reports each of the six headers as present or missing with a plain-English note.

One honesty point: headers are hardening, not a cure. The scan can confirm the headers are set, but missing headers only nudge a grade, they never sink it, and present headers do not prove the rest of your site is safe. Headers do nothing about an out-of-date plugin, a weak password or malware already on the server, and the scan does not read your server's files or check your plugins. They are one important layer, best added alongside updates, good passwords and backups.

Set five of the six security headers straight away with safe defaults in Apache, Nginx or WordPress, then roll out Content-Security-Policy in report-only mode before enforcing it. Verify with the free EB Security scan in about ten seconds.

Common questions

How do I add security headers, and which should I add first?
Add X-Content-Type-Options, X-Frame-Options, Referrer-Policy and Permissions-Policy first, as they are safe with sensible defaults. Add HSTS once every page reliably serves over HTTPS, and add Content-Security-Policy last because it needs careful testing.
Will adding security headers break my website?
Four of the headers are very unlikely to cause problems. HSTS is hard to reverse quickly, so only switch it on when HTTPS is solid. Content-Security-Policy is the one that can block your own scripts and fonts, which is why you roll it out in report-only mode first.
How do I add headers if I only have WordPress access?
Hook the send_headers action in your child theme's functions.php or a small site-specific plugin and call PHP's header() function for each one. It is less robust than server config because it only covers requests that reach PHP, but it works on locked-down shared hosting.
How do I check the headers are actually working?
Open your browser developer tools, go to the Network tab, click the main document request and read the Response Headers. Or run the free EB Security scan, which reads your live response headers and lists each of the six as present or missing.
Do security headers protect against malware and hacking?
They are one layer of defence, not a complete fix. Headers do nothing about an outdated plugin, a weak password or malware already on the server. The free scan checks the headers and external reputation signals such as blocklists, but it does not read your server files or scan your plugins, so keep software updated and backups current too.
What is the difference between Content-Security-Policy and the report-only version?
Content-Security-Policy-Report-Only logs what would have been blocked without blocking anything, so you can find every legitimate source your site uses first. Once the log is quiet, you switch to the plain Content-Security-Policy header to enforce the policy for real.

How safe is your website?

A free A to E grade in about 10 seconds. No account needed.

Check my site