Security · Free guide

Cloudflare Pages security headers: an Astro and HTML guide

Cloudflare Pages supports response header rules for static assets through a _headers file in the published directory. In a conventional Astro project, place it in public/ so it is copied to dist/ during the build.

By StaticLaunch · Four Media Group LLC

Start with a small configuration

The example is suitable for a simple website that does not need to be framed by other origins. If your website is intentionally embedded elsewhere, review the frame policy instead of applying DENY blindly.

Example
/*
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  X-Frame-Options: DENY

Understand each rule

The content-type header discourages MIME sniffing. Referrer-Policy limits what referrer information is shared on cross-origin requests. The frame header prevents other sites from embedding this page. These controls address specific browser behaviors rather than all security threats.

Add Content Security Policy deliberately

Inventory fonts, images, JavaScript, analytics, embeds and form destinations before defining a policy. Consider report-only testing before enforcing it. Avoid copying a broad policy that defeats the purpose of restricting script sources, and avoid a policy that silently breaks important visitor actions.

Verify the actual response

Build the website, inspect the output and deploy a preview. Test a real URL, not only the repository configuration. Substitute your own preview address in this example.

Example
npm run build
ls -l dist/_headers
curl -I https://your-site.pages.dev/

Check functions separately

Static asset header rules do not replace the response headers of a Pages Function. Inspect form or API responses independently. Keep a reviewed record of changes and a working rollback deployment.

Common questions

Where does the file go?

In the folder published to Pages. For a standard Astro static project, public/_headers becomes dist/_headers.

Do headers guarantee security?

No. They help with particular browser risks and must be combined with account, dependency and API safeguards.

References

Provider interfaces and limits can change. Consult the original documentation before changing production settings.