Cloudflare Pages Security Headers
Generate a production-ready Cloudflare Pages `_headers` file with strict CSP, 25/25 ReadyCheck header score, HSTS preload, and immutable `/assets/*` caching.
Configure Next.js App Router security headers in `next.config.mjs` with HSTS, X-Content-Type-Options, Permissions-Policy, and strict CSP guidance for hydration scripts.
HTTP Headers: 15/17 pts · TLS/CAA/security.txt: 8/8 pts · COOP/CORP Hardened
// next.config.mjs
const securityHeaders = [
{
"key": "Content-Security-Policy",
"value": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=31536000; includeSubDomains; preload"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Frame-Options",
"value": "DENY"
},
{
"key": "Referrer-Policy",
"value": "strict-origin-when-cross-origin"
},
{
"key": "Permissions-Policy",
"value": "camera=(), microphone=(), geolocation=(), payment=(), usb=()"
},
{
"key": "Cross-Origin-Opener-Policy",
"value": "same-origin"
},
{
"key": "Cross-Origin-Resource-Policy",
"value": "same-origin"
}
];
/** @type {import('next').NextConfig} */
const nextConfig = {
poweredByHeader: false,
async headers() {
return [
{
source: "/(.*)",
headers: securityHeaders
}
];
}
};
export default nextConfig;
Verify your deployed CSP, HSTS preload, 301 HTTP→HTTPS redirect, DNS CAA, security.txt, DMARC, and AI search readiness in seconds — or grab the $49 ReadyCheck Fix Pack for tailored config bundles.
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const csp = [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: https:",
"object-src 'none'",
"base-uri 'self'",
"frame-ancestors 'none'"
].join("; ");
const headers = new Headers(request.headers);
headers.set("x-nonce", nonce);
const res = NextResponse.next({ request: { headers } });
res.headers.set("Content-Security-Policy", csp);
return res;
}
/.well-known/security.txt after deploy.
Generate a production-ready Cloudflare Pages `_headers` file with strict CSP, 25/25 ReadyCheck header score, HSTS preload, and immutable `/assets/*` caching.
Generate valid `vercel.json` header arrays for Edge Network routes with HSTS preload, Permissions-Policy, Referrer-Policy, and clickjacking protection.
Copy-paste hardened Nginx `server {}` block directives using `always` so 4xx/5xx error responses also emit HSTS, CSP, nosniff, and frame protection.
Generate a compliant Netlify `_headers` file for static sites and Edge Functions with strict CSP, HSTS preload, Referrer-Policy, and Permissions-Policy.
Protect Astro static builds and SSR adapters (Cloudflare, Vercel, Node) with strict `script-src 'self'`, zero inline event handlers, and 25/25 security headers.
Configure strict CSP and HTTP security headers for client-side Vite bundles hosted on Cloudflare Pages, Netlify, Vercel, or Nginx without breaking modulepreload.
With HTTPS + 301 redirect + CAA + security.txt active, this Next.js preset scores 23/25 (15/17 from HTTP headers alone). Using middleware nonces instead of 'unsafe-inline' raises CSP from 3/5 to the full 5/5 (25/25).
Static `next.config.mjs` headers cannot mint per-request nonces; Next.js inline hydration scripts may require `'unsafe-inline'` in static `next.config.mjs` (3/5 CSP points in ReadyCheck) unless you switch to dynamic `middleware.ts` nonces (5/5).
Enter exact origins like https://api.yourdomain.com into the Custom connect-src or script-src fields above. Never add wildcard * or 'unsafe-eval', and verify your live deployment on audit.runglab.dev.