All important HTTP security headers explained: HSTS, CSP, X-Frame-Options, and more. With example configurations for Apache and Nginx.
HTTP security headers are one of the simplest and most effective methods to protect your website against common attacks. They are sent by the server as HTTP response headers and instruct the browser to enforce specific security policies.
Beyond direct security benefits, they also serve as a signal for Google: websites with HTTPS and proper security headers are considered more trustworthy. This can positively affect your rankings.
HSTS enforces that the browser only accesses your website via HTTPS. This prevents man-in-the-middle attacks and SSL stripping.
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Important: Only enable HSTS once you are sure your entire website works correctly over HTTPS. Rolling back is not straightforward.
CSP is the most powerful security header. It defines which sources the browser is allowed to load resources from. This prevents Cross-Site Scripting (XSS) and data injection attacks.
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com; connect-src 'self' https://api.example.com; frame-ancestors 'none'
'unsafe-inline' and 'unsafe-eval' when possible.Tip: Start with Content-Security-Policy-Report-Only to log violations without blocking resources. This lets you gradually adjust the policy.
Prevents clickjacking attacks by stopping the page from being embedded in an <iframe>.
X-Frame-Options: SAMEORIGIN
Note: frame-ancestors in CSP is the modern alternative and offers more control.
Prevents MIME type sniffing. The browser only accepts the declared Content-Type and does not try to guess the type on its own — which can be exploited in attacks.
X-Content-Type-Options: nosniff
This header has only one valid value and should be set on every website.
Controls what referrer information is sent with navigation and requests. Protects your users' privacy.
Referrer-Policy: strict-origin-when-cross-origin
Controls which browser features and APIs your website is allowed to use. This prevents embedded third-party content from accessing sensitive APIs.
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(self)
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
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=()"
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; frame-ancestors 'none'"
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" 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;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; frame-ancestors 'none'" always;
After configuration, you should test your headers:
curl -I https://your-domain.com shows the response headers in the terminal.Check your website's security headers now with PageScore — free and in seconds.