🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

The Astro _headers File for Cloudflare and Netlify

How the Astro _headers file works on Cloudflare and Netlify: put it in public/, path patterns and splats, removing headers, limits, and why SSR routes skip it.

#Astro#Deployment#_headers#Cloudflare Pages

The _headers file is a plain-text file that Cloudflare Pages and Netlify read from your build output to add HTTP response headers to static files. In an Astro project you put it in public/_headers, Astro copies it to dist/_headers unchanged, and the host applies the rules on deploy. Each rule is a URL pattern on one line followed by indented Header-Name: value lines.

Key Takeaways

  • Put the file in public/, not src/; it must end up at the root of dist/.
  • A line starting at column one is a path pattern; indented lines below it are headers.
  • * matches any characters; on Cloudflare, :name placeholders capture a path segment.
  • The file applies to static assets. Responses from server-rendered routes need headers set in code.

The basic format

# public/_headers
/*
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin

/_astro/*
  Cache-Control: public, max-age=31536000, immutable

/downloads/*
  X-Robots-Tag: noindex

/sw.js
  Cache-Control: no-cache

Lines starting with # are comments. Blank lines between blocks are optional but make the file easier to read. After npm run build, confirm the file is at dist/_headers; if it is missing, it was not in public/.

How patterns match

Pattern Matches
/* Every path
/blog/* Everything under /blog/
/_astro/* Astro’s hashed build assets
/sw.js Exactly that file
https://:project.pages.dev/* Cloudflare: requests on the pages.dev hostname

When several rules match a request, headers from all of them are applied. If two rules set the same header, Cloudflare joins the values with a comma, which can produce an invalid Cache-Control. Keep one rule per header for each path, or remove the earlier value as shown below.

The hostname pattern is handy for keeping preview URLs out of search:

# public/_headers
https://:project.pages.dev/*
  X-Robots-Tag: noindex

Our noindex tip explains when a header beats a meta tag.

Removing a header on Cloudflare

Cloudflare Pages lets a more specific rule remove a header that a broader rule added, using ! before the name:

# public/_headers
/*
  X-Frame-Options: DENY

/embed/*
  ! X-Frame-Options

This keeps the site-wide protection while allowing /embed/ pages to be framed elsewhere. Netlify’s file uses the same basic syntax, but check its documentation before relying on Cloudflare-only features such as ! and placeholders.

Limits to know

  • Cloudflare Pages accepts up to 100 header rules and 2,000 characters per line. A long Content Security Policy can hit the line limit; move it into Astro’s security.csp option instead.
  • Netlify also reads [[headers]] blocks from netlify.toml. Use one place, not both, to avoid confusion.
  • Vercel and GitHub Pages ignore _headers. Vercel uses headers in vercel.json; GitHub Pages does not support custom headers.

Static files only

The host applies _headers to files it serves from the build output. On Cloudflare, responses generated by Pages Functions or a Worker, which is where Astro’s server-rendered routes run with the Cloudflare adapter, do not get these headers. Set headers on those responses in middleware:

// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware(async (_context, next) => {
  const response = await next();
  response.headers.set('X-Content-Type-Options', 'nosniff');
  return response;
});

Check the result

Deploy, then inspect the headers of a page and an asset:

# Terminal
curl -sI https://www.example.com/ | grep -iE 'x-content|referrer'
curl -sI https://www.example.com/_astro/index.abc123.css | grep -i cache-control

Use a real file name from your dist/_astro/ folder for the second command. For the values to put in the file, see the security headers tip and the cache headers tip. The Cloudflare deploy docs cover publishing a theme from our catalog with this file in place.

FAQ

Where does the _headers file go in an Astro project?

In public/_headers. Astro copies everything in public/ to the root of dist/, which is where Cloudflare Pages and Netlify look for it.

Why are my _headers rules not applied?

Usually the file is not in dist/, the pattern does not match the path, or the response comes from a server-rendered route. Check dist/_headers after building and test with curl -I.

Does Vercel support the _headers file?

No. On Vercel, set headers in vercel.json with the headers array, or in middleware for server-rendered routes.

Can I use _headers for redirects?

No. Redirects go in a separate _redirects file on Cloudflare Pages and Netlify, or in Astro’s redirects config option.