Reference Guide

What Is a CSP Header? Examples by Platform

A CSP header is an HTTP response header that tells the browser which scripts and other resources a page may use. Copy and adapt the examples below for your server, host, or framework.

Generate Header
Deploy

Where to set a CSP header

Set Content-Security-Policy on the HTTP response that serves the page. Use the matching example below for deployment syntax; use CSP policy examples for policy design and CSP Report-Only for staged testing.

Caveat

When to use a CSP meta tag

HTTP response headers are preferred for a production Content Security Policy header. A meta CSP can help in narrow cases, but it cannot support every directive and should not be treated as a full replacement for server headers.

Workflow

How to deploy a CSP header example

Generate a policy from scan evidence, choose the deployment format for your host or framework, test with report-only CSP, then ship the enforced Content-Security-Policy header when the policy is stable.

HTTP

Plain HTTP CSP header

Meta

Meta tag caveat

nginx

nginx CSP header

Apache

Apache CSP header

Netlify

Netlify CSP _headers

Vercel

Vercel CSP header config

Cloudflare Pages

Cloudflare Pages CSP _headers

Express or Fastify

Express and Fastify CSP middleware

Next.js or Remix

Next.js and Remix CSP headers

Render

Render Go or Gin middleware

CloudFront or S3

CloudFront Function CSP header

WordPress

WordPress or Apache .htaccess

Rails

Rails CSP initializer

Django

Django CSP middleware

Laravel

Laravel CSP middleware

Spring Security

Spring Security CSP header

Helmet.js

Express CSP with Helmet.js

Cloudflare Workers

Cloudflare Worker CSP header

Framework Integration

Framework CSP deployment notes that prevent breakage

Copying the header syntax is only the transport step. Framework-generated scripts, streaming payloads, development tooling, static rendering, and caches determine whether a nonce- or hash-based policy can work.

Next.js per-request nonces Generate a fresh unpredictable nonce before rendering, place the same value in the request and response policy where the framework can read it, and expect nonce-based pages to use dynamic rendering rather than ordinary static generation or CDN caching.
Next.js development versus production Development tooling may require 'unsafe-eval'; do not carry that allowance into production merely because the development server needed it.
Remix and React Router streaming Pass the same nonce to the CSP header, document script components such as <Scripts> and <ScrollRestoration>, and the server renderer so streamed hydration scripts receive it.
Express, Fastify, and Helmet Set the policy before the response body begins. If templates contain authorized inline code, generate one nonce per response and pass it to both the header builder and the rendered elements.
Static hosts and CDNs A nonce placeholder in a static file is not a nonce strategy. Externalize inline code, use build-time hashes that match exact bytes, or add an edge/server response layer that can render a fresh nonce safely.
Hydration and third-party scripts Test production builds with analytics, consent, authentication, lazy routes, and error boundaries. Framework boot scripts and third-party loaders often appear only in those states.

See the maintained Next.js CSP guide and React Router security guidance for version-specific nonce integration details.

Warning

Common CSP header formatting errors

  • Splitting one CSP header across multiple conflicting headers.
  • Forgetting semicolons between directives.
  • Using curly quotes around 'self' or 'none'.
  • Deploying only a meta tag when the server can set an HTTP response header.
  • Copying a generated header without testing it in report-only mode first.

CSP Header Example FAQ

Should I deploy CSP as an HTTP header or a meta tag?

Use an HTTP response header whenever possible. Meta CSP has limitations and is best treated as a fallback for narrow cases.

Can I deploy more than one CSP header?

You can, but multiple policies can combine in surprising ways. Most teams should deploy one reviewed policy per response.

Where should I put report-only CSP?

Deploy report-only CSP with the Content-Security-Policy-Report-Only header, not the enforced Content-Security-Policy header.

Next Step

Generate a CSP header from real scan evidence

Use the CSP scanner and header generator to produce a deployment-formatted candidate, then review and adapt the matching platform syntax from this reference.

Run CSP Scanner