Перейти к содержимому
Skip to content
← All articles

Security Headers: CSP, HSTS, X-Frame-Options and More

HTTP security headers are the first line of defense for your website against common web attacks: XSS, clickjacking, MIME-sniffing, and traffic interception. Proper configuration takes minutes, yet it protects against most attacks from the OWASP Top 10.

Content-Security-Policy (CSP)

The most powerful security header. CSP controls where the browser can load resources from — scripts, styles, fonts, images, frames, and more:

Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com; frame-ancestors 'none'

Core CSP Directives

DirectiveWhat It Controls
default-srcDefault source for all resource types
script-srcWhere JavaScript can be loaded from
style-srcWhere CSS can be loaded from
img-srcWhere images can be loaded from
font-srcWhere fonts can be loaded from
connect-srcWhere AJAX/Fetch/WebSocket requests can be sent
frame-srcWhere iframes can be loaded from
frame-ancestorsWho can embed your page in an iframe
base-uriAllowed URLs for the <base> tag
form-actionWhere forms are allowed to submit

Source Values

Report-Only Mode

Start with report-only mode to avoid breaking your site:

Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-report

The browser will log violations but will not block resources.

Strict-Transport-Security (HSTS)

Forces the browser to always use HTTPS for your domain, even if the user types http://:

Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

Important: before enabling HSTS, make sure your entire site works over HTTPS. Once enabled, rolling it back is difficult — browsers will remember the directive.

X-Frame-Options

Protects against clickjacking — an attack where your site is embedded in an invisible iframe:

X-Frame-Options: DENY
ValueDescription
DENYCompletely prevents embedding in an iframe
SAMEORIGINAllowed only from the same domain
ALLOW-FROM uriAllowed from a specified URL (deprecated, not supported in Chrome)

The modern replacement is the CSP directive frame-ancestors.

X-Content-Type-Options

Prevents the browser from "guessing" the MIME type of content (MIME-sniffing):

X-Content-Type-Options: nosniff

Without this header, the browser may interpret a text file as JavaScript and execute it — a classic XSS attack vector.

Referrer-Policy

Controls what information is sent in the Referer header when navigating to other sites:

Referrer-Policy: strict-origin-when-cross-origin
ValueDescription
no-referrerReferer is not sent at all
originOnly the domain is sent (no path)
strict-origin-when-cross-originFull URL for same domain, only origin for others (recommended)
same-originReferer only for same-domain requests
no-referrer-when-downgradeDo not send when navigating from HTTPS to HTTP

Permissions-Policy

Controls access to browser API документацию — camera, microphone, geolocation, fullscreen mode, and more:

Permissions-Policy: camera=(), microphone=(), geolocation=(self), fullscreen=(self)

Empty parentheses () completely disable the feature. (self) allows it only for the current domain.

X-XSS-Protection

Activates the browser's built-in XSS filter (deprecated, but useful for older versions of IE):

X-XSS-Protection: 1; mode=block

Modern browsers rely on CSP instead of this header, but it is still worth enabling for backward compatibility.

Recommended Header Set

The minimum set of security headers for any website:

# Nginx
add_header Strict-Transport-Security "max-age=63072000; 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'" always;
# Apache (.htaccess)
Header always set Strict-Transport-Security "max-age=63072000; 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'"

How to Check Security Headers

Use our HTTP header checker tool — it will display all response headers from your website, including security headers, and help you identify any that are missing:

Check your website right now

Check now →
More articles: Security
Security
Open Server Ports: How to Check and Why It Matters for Security
13.03.2026 · 11 views
Security
Web Server Security Hardening Checklist: Nginx and Apache
16.03.2026 · 30 views
Security
HSTS — What It Is and Why Your Website Needs It
12.03.2026 · 14 views
Security
HSTS and Preload List: Complete Implementation Guide
16.03.2026 · 13 views