Adding security headers on each host
A security header reaches the browser only if some layer on the response's path adds it. Hosting platforms read header rules from a file or a dashboard and apply them to the responses they serve themselves, which on Netlify and Cloudflare Pages means static files only. A page built by a function, or content passed through from another server, then leaves without the headers, so a static home page can carry them while a server-rendered login page does not.12 What each header does is in HTTP security headers, Content Security Policy, HTTP Strict Transport Security and Clickjacking.
The header set
Baseline values:3
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), camera=(), microphone=()
Strict-Transport-Security: max-age=63072000; includeSubDomainsThe list has no X-XSS-Protection. The filter it switched on in Internet Explorer, Chrome and Safari can create cross-site scripting holes in otherwise safe pages, so the advice is to omit it or send 0.3 X-Powered-By and the version in Server name the software behind the site and are removed.3 A Content-Security-Policy differs per site; sending it first as Content-Security-Policy-Report-Only reports what it would block without blocking anything.4 The HSTS line carries the two-year final value. With includeSubDomains it binds every subdomain to HTTPS, so Certificates and TLS settings reaches that value in stages and leaves out preload.
| Host | Where the rules go | Responses the rules skip |
|---|---|---|
| Vercel | headers in vercel.json | None named; the rules cover static files and functions |
| Netlify | _headers in the publish directory, or [[headers]] in netlify.toml | Functions, edge functions, SSR pages, proxied content |
| Cloudflare Pages | _headers in the static asset directory | Pages Functions, _worker.js, SSR |
| Cloudflare proxy | Response Header Transform Rule | Requests outside the rule's filter |
| Caddy | header directive in the Caddyfile | Headers the app sets after them, unless the change is deferred |
| nginx | add_header | Error status codes without always; location blocks with their own add_header |
Steps
1. Set each header in one layer
When an app sends a header and a proxy in front adds it again, the response carries two copies, which can disagree, for example DENY from the app and SAMEORIGIN from the proxy.3 Inside a _headers file, Netlify and Cloudflare Pages join a repeated name into one comma-separated header.12 Choose the layer that sees every response: the app or framework when pages render on the server, the CDN or reverse proxy when every request passes through it.
2. Vercel: add the rules to vercel.json
A source of /(.*) matches every route, and Vercel applies the rules to static files and to functions.5 A Next.js app can set the same list in next.config.js instead, as HTTP security headers shows.
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" }
]
}
]
}Vercel already sends Strict-Transport-Security: max-age=63072000 on custom domains, without includeSubDomains, and a header in vercel.json replaces it. Vercel accepts no plain HTTP connections and sees no reason to shorten the max-age.6
3. Netlify: a _headers file or netlify.toml
Either file works; both apply to every deploy context, and branch-specific headers need a build step that copies a different file into place.1
# _headers, in the publish directory
/*
X-Content-Type-Options: nosniff
X-Frame-Options: DENY# netlify.toml
[[headers]]
for = "/*"
[headers.values]
X-Content-Type-Options = "nosniff"
X-Frame-Options = "DENY"The rules reach only files Netlify serves from its own store. A function, an edge function or an SSR page has to return its headers itself, and so does a site Netlify proxies to. Netlify typically ignores a custom Server value. Site generators that drop files whose names start with _, Jekyll among them, need a setting to keep _headers.1
4. Cloudflare Pages: a _headers file
The file uses the same format and goes in the static asset directory, often public/. Pages Functions and _worker.js responses ignore it, so a framework rendering on the server sets headers in its function code. A project may hold up to 100 rules, applied in file order; a line ! Name placed after a broader rule removes a header that rule added.2
5. Cloudflare proxy: a Response Header Transform Rule
For a site proxied through Cloudflare, go to Rules > Overview, select Create rule > Response Header Transform Rule, and apply it to all incoming requests. Use Set static for each header: it replaces a header of the same name from the origin, while Add static leaves that copy in place. One rule holds up to 30 headers. Select Deploy.7
The Managed Transform Add security headers also sends x-xss-protection: 1; mode=block,8 the header the value list leaves out, so a hand-written rule fits better. The Managed Transform Remove "X-Powered-By" headers strips that header.8
6. Caddy: the header directive
A field with no prefix is set, overwriting any earlier value, and a - prefix deletes it. With reverse_proxy, the > prefix defers the change until the response is written, so it lands after the app's own headers. A block that deletes a field is deferred as a whole.9
example.com {
header {
>X-Content-Type-Options nosniff
>X-Frame-Options DENY
>Referrer-Policy strict-origin-when-cross-origin
-X-Powered-By
}
reverse_proxy localhost:3000
}7. nginx: add_header with always
Without always, nginx adds the header only to responses with status 200, 201, 204, 206, 301, 302, 303, 304, 307 or 308, so error pages go out bare. A location block with any add_header of its own inherits none from the server block; from nginx 1.29.3, add_header_inherit merge; keeps them.10 proxy_hide_header drops the app's copy of a header before nginx adds its own,311 and server_tokens off removes the version from the Server header and error pages.12
server {
server_tokens off;
add_header_inherit merge; # nginx 1.29.3 or later
proxy_hide_header X-Powered-By;
proxy_hide_header X-Frame-Options;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}8. Check three kinds of response
Request a static page, a page that renders on the server, and an address that does not exist, since each can take a different path through the host. The -I option sends a HEAD request and prints only the headers:13
curl -sI https://www.example.com/
curl -sI https://www.example.com/login
curl -sI https://www.example.com/no-such-pagePrompt for a coding agent
Find where this project is hosted and every place that sets HTTP response headers: vercel.json, netlify.toml, a _headers file, next.config.js, Express middleware, a Caddyfile, nginx config. Pick one layer that sees every response, including pages rendered by functions, and set these there: X-Content-Type-Options: nosniff; X-Frame-Options: DENY (unless a page must be framed by another site); Referrer-Policy: strict-origin-when-cross-origin; Permissions-Policy: geolocation=(), camera=(), microphone=(), minus features the app uses. Remove X-Powered-By and any X-XSS-Protection other than 0. Remove duplicates of these headers from the other layers. On nginx, end each add_header with always and check location blocks. Do not change Strict-Transport-Security or add a Content-Security-Policy without asking me. List each file you changed and the curl commands to verify.
In SecFlash
On the Security page, SF-SEC-106, SF-SEC-107 and SF-SEC-109 report a missing X-Content-Type-Options, Referrer-Policy and Permissions-Policy, SF-SEC-105 a page that other sites can frame, SF-SEC-103 a missing content security policy, and SF-SEC-101 a missing HSTS header. SF-SEC-108 and SF-SEC-626 report a version in Server and an X-Powered-By header, and SF-SEC-110 shows the Mozilla Observatory grade. On the Cloud page, SF-CLD-311 reports a Cloudflare zone (a domain proxied through Cloudflare) that sends no HSTS.
See also
- HTTP security headers
- Content Security Policy
- HTTP Strict Transport Security
- Clickjacking
- Certificates and TLS settings
- Reverse proxy
- Category: Guides
References
- a b c d Netlify (read 22 November 2026). "Custom headers". Netlify Docs.
- a b c Cloudflare (read 22 November 2026). "Headers". Cloudflare Pages docs.
- a b c d e OWASP (read 22 November 2026). "HTTP Headers Cheat Sheet". OWASP Cheat Sheet Series.
- back Vercel (5 March 2026). "Content Security Policy". Vercel Docs.
- back Vercel (read 22 November 2026). "Project configuration (vercel.json)", section "headers". Vercel Docs.
- back Vercel (15 September 2026). "Encryption and TLS", section "Support for HSTS". Vercel Docs.
- back Cloudflare (read 22 November 2026). "Create a response header transform rule in the dashboard". Cloudflare Rules docs.
- a b Cloudflare (read 22 November 2026). "Managed Transforms reference". Cloudflare Rules docs.
- back Caddy (read 22 November 2026). "header (Caddyfile directive)". Caddy Documentation.
- back nginx (read 22 November 2026). "Module ngx_http_headers_module". nginx documentation.
- back nginx (read 22 November 2026). "Module ngx_http_proxy_module": proxy_hide_header. nginx documentation.
- back nginx (read 22 November 2026). "Module ngx_http_core_module": server_tokens. nginx documentation.
- back curl project (read 22 November 2026). "curl.1 the man page". curl.se.
External links
- HTTP Observatory, MDN Web Docs