PHPCheck.net

Set security headers: HSTS, CSP & co. for Apache, nginx and .htaccess

HTTP security headers tell the browser how to behave on your website – for example "only load via HTTPS" or "do not embed in other sites". They cost nothing, are quick to set up and protect against whole classes of attacks such as clickjacking or protocol downgrades.

The most important headers at a glance

HeaderProtects againstRecommended value
Strict-Transport-SecurityFalling back to unencrypted HTTPmax-age=31536000; includeSubDomains
Content-Security-PolicyXSS, injected codedepends on the site, e.g. start with default-src 'self'
X-Frame-OptionsClickjackingSAMEORIGIN or DENY
X-Content-Type-OptionsMIME sniffingnosniff
Referrer-PolicyData leaking via the referrerstrict-origin-when-cross-origin
Permissions-PolicyMisuse of camera, microphone, locationcamera=(), microphone=(), geolocation=()

Apache / .htaccess

<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
  Header always unset X-Powered-By
  # Observe the CSP first (see below):
  Header always set Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; frame-ancestors 'self'"
</IfModule>

nginx

server {
    # ...
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    add_header Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; frame-ancestors 'self'" always;
    server_tokens off;
}

Careful with nginx: an add_header in a location block replaces all add_header directives from the parent block. Set the headers there as well or pull them in with an include.

What to watch out for

HSTS

Only enable it once HTTPS works reliably on the domain and all subdomains – browsers remember the setting for the given duration. Start testing with a small max-age (e.g. 300). Only add preload if you are staying on HTTPS for good.

Content-Security-Policy

The CSP is the most effective but also the most demanding header: a policy that is too strict blocks scripts, fonts or embedded services (e.g. analytics, YouTube, payment providers). So first set it as Content-Security-Policy-Report-Only, check in the browser console what would be blocked, extend the policy – and only then enforce it.

X-Frame-Options vs. frame-ancestors

Modern browsers prefer the CSP directive frame-ancestors. Setting X-Frame-Options as well does no harm and covers older browsers.

Check the result

After setting them up, check your site with PHPCheck.net – the "Headers" section shows for each header whether it is set correctly.

Sources

More guides