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.
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 freePlain-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.