A security scanner says your site is missing Content-Security-Policy. A script on another of your domains is not allowed to read a JSON file from this one. A page of yours turns up inside somebody else's frame. Each of these is fixed with an HTTP response header — and your site is a folder of files, with no server code of yours to send one.
On a static host you set response headers in a file or a setting the host reads: a _headers file on Netlify and Cloudflare Pages, a headers array in vercel.json, add_header in nginx, Header set in Apache. GitHub Pages has no such setting. Where the host gives you nothing, a <meta> tag in the page can stand in for two of the headers people ask about: Content-Security-Policy, with parts missing, and Referrer-Policy. The others have no supported <meta> form.
The 30-second version
| Host | Where you set headers | What catches people out |
|---|---|---|
| Netlify | _headers in the publish folder, or [[headers]] in netlify.toml |
applies only to files Netlify serves itself, not to proxied or function responses |
| Cloudflare Pages | _headers in the build output |
100 rules; two matching rules that set the same header are joined with a comma; skipped for Pages Functions |
| Vercel | headers in vercel.json |
the pattern matches the request path, without the query string |
| GitHub Pages | nowhere | <meta> tags are the only way, and they cover very few headers |
| nginx | add_header in the server config |
not sent on a 404 without always; a block with its own add_header drops the inherited ones |
| Apache | Header set in the config or .htaccess |
not sent on error responses without always |
| dropto.run | nowhere — a fixed set is sent for you | no control over framing, HSTS or CORS headers on your files |
What a response header is
When a browser asks for a file, the answer comes in two parts: a block of headers, then the body. Your HTML, CSS and images are bodies. The headers are written by whatever answers the request, and they tell the browser how to treat the body: what type it is, how long to keep it, which other sites may read it, what the page is allowed to load.
That is why you cannot fix a missing header by editing index.html. On a server you run, headers are a line of config. On a static host they belong to the host's software, so the host gives you a file to describe them in. Every version of that file is the same idea — a path pattern, then the headers to add to responses that match:
/*
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
/assets/*
Cache-Control: public, max-age=31536000, immutableThat is the _headers format Netlify and Cloudflare Pages both read. The path goes at the start of a line and the headers are indented under it. The second rule is only safe when the file names under /assets/ carry a content hash, so that a changed file gets a new name.
The headers worth setting
| Header | What it does | A value to start from | From a <meta> tag? |
|---|---|---|---|
Content-Security-Policy |
limits what a page may load and run | default-src 'self', then widen it |
yes, with parts missing |
Strict-Transport-Security |
tells the browser to use only HTTPS for this host | a short max-age such as 300, raised once nothing breaks |
no |
X-Content-Type-Options |
stops the browser guessing a file's type | nosniff |
no |
X-Frame-Options |
says whether other pages may show yours in a frame | DENY or SAMEORIGIN |
no |
Referrer-Policy |
controls how much referrer information goes out with requests the page makes | strict-origin-when-cross-origin |
yes |
Permissions-Policy |
switches browser features such as the microphone off; MDN marks it experimental, with limited browser support | geolocation=(), microphone=() |
no |
Access-Control-Allow-Origin |
says whether code on another origin may read the response | one origin, or * for public files |
no |
Cache-Control |
says how long browsers keep a file | long for hashed assets, never long for HTML | no |
The last column follows MDN: its list of what <meta http-equiv> supports, plus <meta name="referrer">. Both are covered further down. Four of the headers need more than a line in a table.
Content-Security-Policy is the one that can break your site. MDN states it plainly: if a CSP contains either a default-src or a script-src directive, then inline JavaScript will not be allowed to execute unless you allow it with a nonce, a hash or 'unsafe-inline'. Many sites have an inline script somewhere — an analytics snippet, a theme switcher. So send the policy as Content-Security-Policy-Report-Only first. In that mode the policy is not enforced. MDN describes violations going to a reporting endpoint you name in the policy; in Chromium 153 we also saw each one logged to the console with no endpoint set, ending in "The policy is report-only, so the violation has been logged but no further action has been taken."
Strict-Transport-Security is the one that is slow to take back. The browser remembers it for max-age seconds, and if you stop sending it, the previous header remains in effect until its expiration time. includeSubDomains extends it to every subdomain of the host, including ones you forgot. Browsers ignore the header over plain HTTP, so it only counts once HTTPS already works.
Access-Control-Allow-Origin takes * or exactly one origin, not a list. MDN is explicit that allowing a set of origins requires code on the server side to check the value of the Origin request header — which a static host does not run. For fonts, images and public JSON, * is the usual answer; it cannot be combined with credentials.
Cache-Control has an article of its own: why a site still shows the old version after a deploy.
Host by host
Netlify
Save a plain text file called _headers to the publish directory — the folder that gets deployed, which is the repository root only if you publish the repository as it is. The same rules can go in netlify.toml:
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"Two limits. Custom headers apply only to files Netlify serves from its own store. For a path you proxy to another site, or one answered by a function or an edge function, the rules are not applied, and that code has to send the header itself. And a list of names is typically ignored because Netlify's servers set them — Content-Length, Location and Server among them.
Cloudflare Pages
The same file, placed so that it ends up in the build output; with a framework that usually means public/ or static/. Cloudflare documents the limits: up to 100 rules, and 2,000 characters per line — a long content security policy can reach that.
A request that matches several rules gets the headers of all of them, and if two rules set the same header the values are joined with a comma. For Cache-Control that can mean one value carrying two conflicting max-age directives, which Cloudflare's page warns about. To replace instead of add, remove the header first with ! in a later, narrower rule, then set it again:
/*
Content-Security-Policy: default-src 'self'
/embed/*
! Content-Security-Policy
Content-Security-Policy: default-src 'self'; frame-ancestors https://partner.exampleOrder matters: the ! only removes what an earlier rule added. And _headers is not applied to responses generated by Pages Functions; set those headers in the function.
Vercel
Headers go in vercel.json:
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" }
]
}
]
}source is a pattern matched against the request path, without the query string. Three response headers are reserved and cannot be modified: x-matched-path, server and content-length.
GitHub Pages
There is no header setting. In GitHub's community forum, the answer given to a request for a Content-Security-Policy header on Pages was: We don't support this feature today so a meta tag unfortunately is the only way. That was May 2023; a July 2024 reply in the same thread says it is not being prioritized. The next section covers what a <meta> tag can and cannot do.
nginx and Apache
On a server you control it is one line each:
add_header X-Content-Type-Options "nosniff" always;Header always set X-Content-Type-Options "nosniff"Write always on both. Without it, nginx adds the header only when the response code is 200, 201, 204, 206, 301, 302, 303, 304, 307 or 308 — so your 404 page goes out without it. Apache's default condition is onsuccess; the always table is the one added to the response even on error. Apache keeps the two as separate tables and notes that always is not a superset of onsuccess, so to change a header something else already set you may need the line in both forms.
nginx has a second trap. add_header lines are inherited from the level above if and only if there are no add_header directives defined on the current level. Add one add_header Cache-Control … line inside a location block and every security header set in the server block stops being sent for that location. Repeat them in the block, or on nginx 1.29.3 and later use add_header_inherit merge;.
On Apache, Header works in .htaccess when the server allows the FileInfo override.
dropto.run
There is no _headers file here. Every file is served with X-Content-Type-Options: nosniff and Referrer-Policy: strict-origin-when-cross-origin, and a Cache-Control chosen by file type and name. The default subdomain also sends X-Robots-Tag: noindex, which keeps it out of Google until the site has its own domain. For a content security policy, use the <meta> tag below.
When the host gives you no setting
HTML has one way to say a header from inside the page: <meta http-equiv>. It is narrower than it looks. MDN lists what it supports: only a subset of the HTTP headers. content-security-policy is the only header from the table above on that list. MDN also warns against putting other security headers in a <meta> tag, because a browser that ignores them leaves you with a false sense of security.
So a content security policy works:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' https:">with parts missing. The CSP specification says the Content-Security-Policy-Report-Only header is not supported inside a meta element, and neither are the report-uri, frame-ancestors and sandbox directives. It also does not apply to content that comes before the tag, so put it first in <head>.
frame-ancestors is the directive that says who may frame your page, and the older header for that has the same gap: setting X-Frame-Options inside a <meta> element has no effect. On a host with no header setting, neither a header nor a <meta> tag is available to stop other sites framing your pages.
Referrer-Policy has its own tag, <meta name="referrer" content="…">, and strict-origin-when-cross-origin is already the browser default when no policy is given. Strict-Transport-Security, Permissions-Policy, Access-Control-Allow-Origin and Cache-Control are not on MDN's list. If you need one of those, the choice is a host that lets you set headers, or a proxy of your own in front of the site that adds them.
Check it
Ask for the headers without the body:
curl -sI https://example.com/Then ask for three more addresses, because rules are per path and per status: an asset such as /assets/app.js, a page that does not exist, and any path answered by a function or a proxy. A header that is on the home page and missing from one of those is a rule that did not match, not a host that is broken. The browser's developer tools show the same thing under Network → a request → Headers.
Troubleshooting
The header is on every page except the 404 page. nginx without always, or Apache without always.
Security headers disappeared from one folder on nginx. A location block has its own add_header, so nothing is inherited from the level above.
The header arrives with two values joined by a comma. On Cloudflare Pages two rules matched and both set it. Remove it with ! Header-Name in the later rule, then set it again.
The _headers file is in the repository and nothing changed. It is not in the folder the host serves. With a framework, put it in the folder the build copies as-is — often public/ or static/ — and check that it is in the output.
The header is missing on one route only. That route is not a file. On Netlify the rules skip proxied and function routes; on Cloudflare Pages they skip Pages Functions.
The console says "The Content Security Policy directive 'frame-ancestors' is ignored when delivered via a <meta> element." That is Chromium 153's wording, and it means what it says: the directive needs the real header. The same browser answers an X-Frame-Options meta tag with "X-Frame-Options may only be set via an HTTP header sent along with a document."
The console says "Executing inline script violates the following Content Security Policy directive…" Chromium again. Your policy has default-src or script-src and the page has an inline script. Move the script to a file, or allow it with the hash the message prints.
A CORS error remains after listing two origins. Access-Control-Allow-Origin takes one origin or *, not a list.
When not to do this
- Don't enforce a content security policy you have not watched in report-only mode. An inline script you forgot about is enough to break a page.
- Don't send
Strict-Transport-SecuritywithincludeSubDomainsuntil every subdomain serves HTTPS. On a host that has sent it, a visitor who hits a certificate error gets no way to click through. Taking the header back means sendingmax-age=0, over HTTPS, to every browser that saw it. - Don't try to fix a CORS error from somebody else's API in your own headers.
Access-Control-Allow-Originhas to be on the response you are reading, so it has to come from their server. If the API also needs a key, call it through your own host instead. - Don't put a long
max-ageon HTML. A browser that has stored a page for a year will not ask you for the new one.
Questions
Can I set HTTP headers from inside an HTML file?
Only a few. <meta http-equiv> supports a short list, and the useful entry on it is Content-Security-Policy, minus frame-ancestors, sandbox, report-uri and report-only mode. Referrer-Policy has <meta name="referrer">. HSTS, CORS and caching headers are not on the supported list, and X-Frame-Options in a <meta> tag has no effect, so those have to be real response headers.
Does GitHub Pages support a _headers file?
No. The answer in GitHub's own forum, given in 2023 and not changed by a 2024 follow-up, is that Pages does not support custom headers and a <meta> tag is the only way. If you need a header a <meta> tag cannot carry, such as frame-ancestors, you need a different host or a proxy in front.
Where does the _headers file go?
In the folder that is published. With a build step, put it in the folder the build copies as-is — usually public/ or static/ — and confirm it lands in the output.
Why are my headers missing on the 404 page?
On nginx, add_header applies to a fixed list of success and redirect codes unless you add always. On Apache, Header set uses the onsuccess condition unless you write Header always set.
Is the same _headers file valid on Netlify and Cloudflare Pages?
The basic format — a path, then indented Name: value lines — is the same on both. The differences are in the details: Cloudflare Pages documents a cap of 100 rules and 2,000 characters a line, and a ! syntax for removing a header. Netlify's page documents neither, and does not say how two matching rules combine. Check with curl after moving a file from one to the other.
Can I allow several origins with Access-Control-Allow-Origin?
Not with a static rule. The header takes one origin or *. Allowing a list means reading the request's Origin header and answering with that value, which needs code on the server; when you do that, send Vary: Origin as well.
Should I use X-Frame-Options or frame-ancestors?
frame-ancestors, in a Content-Security-Policy header, is the more capable of the two: MDN's X-Frame-Options page points to it for more comprehensive options than offered by this header. Neither works from a <meta> tag.