CSP deployment: _headers, Nginx, Apache and meta tags
In one line: How to ship a CSP in Cloudflare Pages _headers, Nginx add_header, Apache Header always set and an HTML meta tag, and the traps in each.
The same policy behaves differently depending on where it is placed. In particular, some directives do not work in a meta tag.
1. Cloudflare Pages — _headers
/* Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none' X-Content-Type-Options: nosniff
A _headers file at the site root maps path patterns to headers. Include it in the build output and it applies on deploy. Per-path policies make it easy to separate the admin area from API routes.
2. Nginx — add_header
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-$request_id'" always;
Quote the value and end with a semicolon. Without always, the header is missing on 4xx and 5xx responses. Also, if a child location has any add_header, the parent's add_header directives are not inherited — a common reason a policy silently disappears.
3. Apache — Header always set
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'"
always also sets the header on error responses. In .htaccess it needs mod_headers.
4. HTML meta tag
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; object-src 'none'">
This is the last resort when you cannot control headers, and its limits are real.
| Directive | meta tag |
|---|---|
| default-src, script-src, style-src, img-src, connect-src… | Works |
| frame-ancestors | Ignored |
| report-uri, report-to, sandbox | Ignored |
A meta tag alone cannot prevent clickjacking or collect reports. X-Frame-Options can cover framing, but reporting needs a header.
5. Report-Only in each format
Change the header name to Content-Security-Policy-Report-Only in every format. In a meta tag, that becomes the http-equiv value.
6. Verify after deploy
curl -sI https://app.example.com/ | grep -i content-security-policy
Confirm the header is present and that no proxy stripped it. CspLab's converter produces all four formats and can download the _headers file.
Paste the same policy into the tool to see the score and the directives to change.
Open CspLabFrequently asked questions
Why does `frame-ancestors` not work in a meta tag?
The CSP specification requires meta tags to ignore `frame-ancestors`, `report-uri` and `sandbox`. Use a response header or `X-Frame-Options` for framing protection.
My CSP vanished from Nginx.
If a child `location` has any `add_header`, the parent block's `add_header` directives are not inherited. Repeat the policy in each location or include a shared file.
What if both _headers and the server set a CSP?
Both headers are sent, and browsers apply all CSP headers. Remove one or manage the policy in a single place.
Sources
Keep reading
- CSP directive structure and default-src inheritanceHow a policy string splits into directives and sources, which directives inherit default-src, and what an empty value actually does.
- nonce, hash and strict-dynamic: allowing inline scripts safelyHow nonce and hash let you drop unsafe-inline without breaking inline scripts, how strict-dynamic disables the host allowlist, and the correct combinations.
- Nine CSP bypass patterns and the attacks they enableunsafe-inline, wildcards, data:, blob:, http:, missing base-uri, sandbox escapes and nonce-less strict-dynamic: the combinations that defeat a policy.