With a few lines of server configuration, you can switch on browser protections against clickjacking, content sniffing, leaking URLs and injected scripts. This website itself sends a full set via a Cloudflare _headers file.
The essential headers
| Header | Protects against | Typical value |
|---|---|---|
| Strict-Transport-Security | Downgrade to HTTP | max-age=31536000; includeSubDomains |
| X-Content-Type-Options | MIME sniffing | nosniff |
| Referrer-Policy | Leaking full URLs to other sites | strict-origin-when-cross-origin |
| Content-Security-Policy | Injected scripts (XSS), clickjacking | Site-specific |
| Permissions-Policy | Unwanted access to camera, microphone, location | camera=(), microphone=(), geolocation=() |
| X-Frame-Options (legacy) | Clickjacking | SAMEORIGIN |
Examples
# Apache (.htaccess)
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header set X-Content-Type-Options "nosniff"
Header set Referrer-Policy "strict-origin-when-cross-origin"
Header set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>
# Nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
# Cloudflare static assets (_headers file)
/*
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-originMore on Apache configuration in .htaccess basics.
Content-Security-Policy step by step
- List every source your pages load: scripts, styles, fonts, images, embeds.
- Start with
Content-Security-Policy-Report-Onlyand watch the browser console. - Tighten until nothing legitimate is blocked.
- Switch to the enforcing header.
Content-Security-Policy: default-src 'self'; img-src 'self' data:; frame-src https://www.youtube-nocookie.com; frame-ancestors 'self'; base-uri 'self'; form-action 'self'Frequently asked questions
Can security headers break my site?
A strict Content-Security-Policy can block scripts, fonts or embeds you rely on. Test it in report-only mode first. The other basic headers rarely cause problems.
Do security headers help SEO?
Not directly. They protect visitors and your reputation, and HTTPS (which HSTS enforces) is a lightweight ranking signal.