Next.js API routes return 401 when Authorization header is stripped behind Cloudflare Access
Environment
Next.js 13 app (app router) running on Vercel, Node 18 runtime; Cloudflare Access + Cloudflare Worker in front, production
Symptoms
Intermittent 401 Unauthorized on POST /api/* endpoints that require Bearer token. Failures affect ~10% of requests, no client-side errors.
Observations
Origin logs show missing Authorization header for failing requests. Requests succeed when sent directly to origin hostname bypassing Cloudflare. Cloudflare Access shows normal allow decisions, Worker modifies headers for other routes.
Already attempted
Disabled a recently added Transform Rule in Cloudflare — no change. Temporarily removed Worker but left Access enabled; still saw some failures. Replayed failing request via curl to Cloudflare edge and to origin.
Question
What Cloudflare configuration or proxy behavior can strip Authorization on some requests, and how to ensure Authorization reaches origin reliably?
Verified community troubleshooting
First check any Cloudflare Worker or Transform Rules — they frequently remove Authorization. Re-enable the Worker with temporary logging (mask tokens) to confirm whether Authorization arrives at the edge and whether the Worker forwards it explicitly. If Cloudflare Access is in use, switch to sending the Access JWT assertion to the origin or enable the option that forwards the original Authorization header. Test by curling the Cloudflare edge (host header set) and verifying the origin receives Authorization. Roll back Worker changes if they cause regression; use the Access assertion header as a safe fallback.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development33% match
Next.js API behind Vercel Edge intermittently returns 403 due to cached edge responses dropping Cookie headers
Open0 answers - Web Development29% match
Next.js serverless API on Vercel returns intermittent 504s for long upstream requests when HTTP keep-alive sockets are exhausted
Open0 answers - Web Development27% match
Cloudflare Worker strips Authorization header when X-Forwarded-For present causing 401s on API POSTs
Solved1 answer - Web Development27% match
NGINX returns 502 only for long-running Node.js API endpoints while short requests succeed
Open0 answers - Web Development27% match
Large JSON POSTs intermittently rejected with 413 from Next.js API on Vercel despite sizeLimit set
Open0 answers