Guides / Web servers and domains
HTTP security headers: CSP, HSTS, frame-ancestors, Referrer-Policy and more
A few response headers tell browsers to refuse plain HTTP, to block your pages from being framed by other sites, to run only the scripts you allow, and to keep full URLs out of the logs of other sites. They cost a line of configuration each. Each header below comes with a safe starting value and a tested snippet for nginx, Apache and Caddy.
The headers
| Header | What it does | Start with |
|---|---|---|
Strict-Transport-Security |
the browser uses only HTTPS for the domain for max-age seconds |
max-age=63072000; includeSubDomains |
Content-Security-Policy |
limits where scripts, styles, frames and other resources may come from | see below |
X-Content-Type-Options |
stops the browser from guessing content types | nosniff |
Referrer-Policy |
how much of the URL is sent to other sites | strict-origin-when-cross-origin |
Permissions-Policy |
turns off browser features the site does not use | camera=(), microphone=(), geolocation=() |
X-Frame-Options |
older way to block framing; superseded by CSP frame-ancestors |
DENY, only for old browsers |
Without a Referrer-Policy header, current browsers use strict-origin-when-cross-origin; the header makes the value explicit, and a stricter one such as same-origin sends nothing to other sites.
Two headers to leave out: X-XSS-Protection and Expect-CT (obsolete). MDN marks X-XSS-Protection as deprecated: the filter it controlled was a feature of Internet Explorer, Chrome and Safari, and could itself open cross-site scripting holes. Use a Content-Security-Policy instead.
Strict-Transport-Security (HSTS)
After a browser has seen this header over HTTPS, it rewrites every http:// link to the domain to https:// until max-age runs out, and it does not let users click through certificate errors. It closes the gap where the first request goes out over plain HTTP and can be intercepted.
max-age: a number of seconds. 63072000 is two years, the value in the Mozilla guidelines. Start lower (300, then a week, then a month) if you are not sure every page and subdomain works over HTTPS.includeSubDomains: applies the rule to all subdomains. Check internal hosts such asintranet.example.comfirst.preload: a request to be built into browsers. The list at hstspreload.org requiresmax-ageof at least 31536000 (one year),includeSubDomainsandpreload, a redirect from HTTP to HTTPS on the same host, and HTTPS on all subdomains. Removal from the list takes a long time; addpreloadonly when you are sure.
Send the header only on HTTPS responses; browsers ignore it over HTTP.
Content-Security-Policy (CSP)
CSP tells the browser which sources it may load. It is the strongest defence against cross-site scripting that a header can give, because injected <script> tags and inline event handlers do not run unless the policy allows them.
A strict starting policy for a site that serves its own scripts and styles:
default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'
default-src 'self': everything from your own origin only, unless a more specific directive says otherwise.frame-ancestors 'none': no site may put your pages in a frame (clickjacking). Use'self'if you frame your own pages.base-uri 'self'andform-action 'self': an injected<base>tag or form cannot redirect links or form posts elsewhere.object-src 'none': no plugins.
Real sites load fonts, analytics, maps or payment widgets from elsewhere. Add only those origins to script-src, style-src, img-src, connect-src or frame-src. Avoid 'unsafe-inline' for scripts: it allows the injected scripts CSP is meant to stop. Inline scripts can be allowed with a nonce or a hash instead.
Roll it out in report-only mode first. Content-Security-Policy-Report-Only with the same value makes the browser report violations in the developer console (and to a report-to endpoint if you set one) without blocking anything. Browse the site, fix what it reports, then switch to the enforcing header.
Server version banners
Server: nginx/1.22.1 or X-Powered-By: PHP/8.2.7 tell everyone which version to look up in the CVE list. Hiding versions does not fix anything, but it removes a free hint and keeps your server out of searches for one vulnerable version.
nginx
server {
# ... listen, TLS ...
server_tokens off;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" 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;
}
alwaysadds the header to error responses too; without it nginx adds it only to 2xx and 3xx responses.add_headerlines are inherited by alocationblock only if that block does not contain anadd_headerof its own. Oneadd_headerin a location drops all the headers above for that location. Repeat them, or put them in a file youincludein each place.server_tokens offreduces the header toServer: nginx.
Apache httpd
With mod_headers enabled (sudo a2enmod headers on Debian and Ubuntu):
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
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=()"
For the version banner, set ServerTokens Prod and ServerSignature Off. On Debian and Ubuntu, change them in /etc/apache2/conf-available/security.conf. A separate file with the same directives does not work if it sorts before security.conf: in the test, a file named hardening.conf was overridden and the header still read Server: Apache/2.4.71 (Debian). After the change it reads Server: Apache.
Caddy
example.com {
header {
Strict-Transport-Security "max-age=63072000; includeSubDomains"
Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
Permissions-Policy "camera=(), microphone=(), geolocation=()"
-Server
}
# ... root, file_server or reverse_proxy ...
}
-Server removes the Server: Caddy header.
Applications behind the server
Frameworks and languages add their own headers. PHP sends X-Powered-By unless expose_php = Off is set in php.ini. Express sends X-Powered-By: Express unless the app calls app.disable('x-powered-by'). Headers set by the application and by the proxy can also end up twice in a response; check the result.
Check the result
curl -sI https://example.com/ | grep -i -E 'strict-transport|content-security|x-content-type|referrer-policy|permissions-policy|x-frame|^server|x-powered-by'
On the nginx test server the full set came back:
server: nginx
strict-transport-security: max-age=63072000; includeSubDomains
content-security-policy: default-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'
x-content-type-options: nosniff
referrer-policy: strict-origin-when-cross-origin
permissions-policy: camera=(), microphone=(), geolocation=()
Check an error page too (curl -sI https://example.com/does-not-exist), and a few locations with their own configuration.
Sources
- RFC 6797: HTTP Strict Transport Security and hstspreload.org (submission requirements, staged max-age)
- Content Security Policy Level 3, W3C (directives, report-only, frame-ancestors)
- MDN: HTTP headers (X-Content-Type-Options, Permissions-Policy); Referrer-Policy (the default without the header); X-XSS-Protection (deprecated, the browsers that had the filter, use CSP)
- Mozilla server side TLS guidelines 6.0 (HSTS max-age 63072000)
- nginx: add_header (
always, inheritance) and server_tokens - Apache: mod_headers, ServerTokens; Caddy: header directive
- PHP: expose_php and Express: security best practices
- Headers tested on 2026-11-26 (Permissions-Policy in the Apache and Caddy snippets checked with curl on Debian 13) with nginx 1.26.3 (Debian 13), Apache 2.4.71 (Debian 12 and 13) and Caddy 2.6.2 (Debian 13)
Check your servers every day
The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.