Understanding HTTP 431 Request Header Fields Too Large: Cookie Bloat, Header Limits, and Why You Often Get a 400 Instead
431 Request Header Fields Too Large is the error you get for something you didn't knowingly send. Nobody types a 20 KB Cookie header. It builds up one analytics script, one A/B test and one session token at a time, until a server somewhere in the chain refuses to read any more. The awkward part is that the server that refuses often doesn't say 431 at all. NGINX, Apache and AWS load balancers answer oversized headers with a 400, and Cloudflare documents a 413. This post covers what 431 means, where oversized headers come from, the real limits on common platforms, how to fix it from both sides, and how it compares to 413, 414 and 400.
What Is a 431?
A 431 means the server won't process the request because its headers are too big, either all of them together or one in particular. It was defined in RFC 6585, alongside 428, 429 and 511:
The 431 status code indicates that the server is unwilling to process the request because its header fields are too large. The request MAY be resubmitted after reducing the size of the request header fields.
It can be used both when the set of request header fields in total is too large, and when a single header field is at fault. In the latter case, the response representation SHOULD specify which header field was too large. โ RFC 6585, Section 5
In plain English: "I stopped reading your headers. Send less and try again." Unlike most errors, retrying the identical request will never work. Something has to get smaller first, and the RFC asks the server to say which header was the problem when it knows.
The same section adds one rule that's easy to miss:
Responses with the 431 status code MUST NOT be stored by a cache. โ RFC 6585, Section 5
A 431 is about one client's headers, not the resource, so a cached copy would wrongly block every other visitor.
HTTP Has No Header Limit, but Every Server Does
The core HTTP spec sets no maximum. It leaves the choice to servers, and tells them what to do when they pick one:
A server that receives a request header field line, field value, or set of fields larger than it wishes to process MUST respond with an appropriate 4xx (Client Error) status code. Ignoring such header fields would increase the server's vulnerability to request smuggling attacks โ RFC 9110, Section 5.4
Note the wording: "an appropriate 4xx", not "a 431". Rejecting oversized headers is mandatory, but the code isn't. Every server, proxy and load balancer picks its own limit and its own status, and the smallest limit in your request path wins.
What Each Platform Actually Does
These are the documented defaults, checked against each vendor's own docs or source:
| Layer | Default limit | What the client gets |
|---|---|---|
| NGINX | large_client_header_buffers 4 8k: each header field must fit in one 8 KB buffer | 400 "Request Header Or Cookie Too Large" |
| Apache httpd | LimitRequestFieldSize 8190 bytes per header field | 400 |
| Node.js (Express, Next.js, Vite) | 16 KiB of headers in total | 431 |
| AWS Application Load Balancer | 16 K request line, 16 K single header, 64 K entire request header (not adjustable) | Listed under 400 and 413 |
| Cloudflare | 128 KB of request headers in total | 413, per Cloudflare's changelog |
Some details behind those rows:
- NGINX documents it plainly: "A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client" (
large_client_header_buffers). A request line that's too long gets a 414 instead. Internally, NGINX's source tracks this case as a non-standard 494 and rewrites it to 400 before sending. The same limits apply over HTTP/2 and HTTP/3. - Apache caps each field with
LimitRequestFieldSize, which defaults to 8190 bytes. The docs point out that "SPNEGO authentication headers can be up to 12392 bytes," so Kerberos single sign-on can go over the default on its own. In the httpd source, an oversized field sets the status to 400 with the note "Size of a request header field exceeds server limit." - Node.js is the one that sends a real 431. Its
--max-http-header-sizeflag defaults to 16 KiB (raised from 8 KiB in v13.13.0). Going over it fails the parser withHPE_HEADER_OVERFLOW, and since v12 the defaultclientErrorhandling answers with a 431. Express,next startand Vite's dev server all inherit this (Vite's troubleshooting guide covers it). - AWS ALB lists its header quotas as not adjustable. Its troubleshooting page gives "the request header exceeded 16 K per request line, 16 K per single header, or 64 K for the entire request header" as a possible cause of both HTTP 400 and HTTP 413. It doesn't mention 431 at all.
- Cloudflare now allows 128 KB of request headers in total (connection limits). Until an October 2025 increase, the limit was 32 KB in total and 16 KB per header. The changelog says requests over the limit failed with "
HTTP 413(Request Header Fields Too Large)": a 413 status labeled with 431's name.
So "431" is often a description, not a status code. If your logs show a 400 for "header too large", check which layer answered before digging into the app.
Common Causes
Oversized headers almost always come from one client's accumulated state, which is why one user is broken while everyone else is fine.
- Cookie bloat. Every cookie whose domain and path match is sent on every request. RFC 6265 asks browsers to support at least 4096 bytes per cookie and 50 cookies per domain (Section 6.1), so a browser that stays within the spec can still send far more than an 8 KB server limit. The same section tells servers to "use as few and as small cookies as possible."
- Large tokens in headers. A JWT carrying every role, permission and group claim in its payload can run to several kilobytes, and it travels in
Authorizationon every API call. Store the same token in a cookie as well and you pay for it twice. Kerberos/SPNEGOAuthorizationheaders are large by design, as the Apache docs note. - Many cookies on a shared parent domain. A cookie set with
Domain=example.comgoes to every subdomain (RFC 6265, Section 8.6). Put marketing tags, a CMS and a help desk widget on one parent domain, andapi.example.comreceives all their cookies while using none of them. - Localhost cookies from other dev projects. "Cookies do not provide isolation by port" (RFC 6265, Section 8.5). The session cookie from yesterday's project on
localhost:8080gets sent to today's project onlocalhost:3000, along with every other cookie you've collected onlocalhost. Local development rarely has a proxy in front, so Node's 16 KiB limit is the one you hit, which is why this shows up as a 431 fromnext devor Vite on a project that hasn't set a single cookie yet. - Too many custom headers, or one that keeps growing. Tracing and debugging headers added by several layers of proxies, or a cookie whose value grows on every request. AWS has a dedicated 463 for an
X-Forwarded-Forthat keeps growing.
Fixing It on the Client
For a single user stuck on a 431 (or a "Request Header Or Cookie Too Large" 400):
- Clear cookies for that site only. In Chrome DevTools, it's under Application โ Storage โ Cookies.
- On localhost, clear the
localhostcookies, since they're shared across every port. Switching to127.0.0.1also works for now, because cookies match on the host name and the two have separate cookie jars. - Check what you're sending. Open the failing request in the Network tab, or reproduce it with
curl -vand the sameCookievalue. One header much bigger than the rest is your culprit. - For API clients, slim the token. Ask the identity provider for fewer claims or a reference token, rather than sending a full permissions list on every call.
Fixing It on the Server
Clearing cookies helps one user once. These changes stop it from happening again:
- Keep session state on the server. The cookie should hold a session ID, not the session. Moving a cart, preferences or a whole user profile out of cookies and into a database or cache usually solves the problem completely.
- Scope cookies tightly. Leave out the
Domainattribute unless subdomains really need the cookie. Without it, RFC 6265 makes the cookie host-only. UsePathfor cookies only one part of the site needs, and keep marketing tools off the parent domain your API shares. - Expire cookies you no longer use. Old A/B test cookies stay until they expire. Send
Max-Age=0for retired names. - Raise the limit only as a last resort, and at every layer. Raising Node's limit doesn't help if NGINX or the load balancer in front still rejects the request first.
Node.js and Express
Node's limit applies before Express sees the request, so app.listen() can't change it. Create the server yourself and pass maxHeaderSize, or start the process with the flag:
import http from "node:http";
import express from "express";
const app = express();
// Raise Node's 16 KiB default for this server only
const server = http.createServer({ maxHeaderSize: 32 * 1024 }, app);
server.listen(3000);node --max-http-header-size=32768 server.js
# Next.js passes NODE_OPTIONS through to Node
NODE_OPTIONS='--max-http-header-size=32768' next startIf you want the 431 to follow the RFC (naming the problem and telling caches not to store it), replace the default clientError response. There's no req or res at this stage, so you write the raw response to the socket:
server.on("clientError", (err: NodeJS.ErrnoException, socket) => {
if (err.code === "ECONNRESET" || !socket.writable) {
socket.destroy();
return;
}
if (err.code === "HPE_HEADER_OVERFLOW") {
const body = JSON.stringify({
error: "request_header_fields_too_large",
message: "Request headers are too large. Clear this site's cookies and try again.",
});
socket.end(
"HTTP/1.1 431 Request Header Fields Too Large\r\n" +
"Content-Type: application/json\r\n" +
"Cache-Control: no-store\r\n" +
`Content-Length: ${Buffer.byteLength(body)}\r\n` +
"Connection: close\r\n\r\n" +
body,
);
return;
}
socket.end("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
});At this point Node doesn't know which header overflowed, only that the total was too large. To name the header, as the RFC suggests, enforce your own smaller limit on the header you care about:
const MAX_AUTHORIZATION = 8 * 1024;
app.use((req, res, next) => {
const auth = req.get("Authorization");
if (auth && auth.length > MAX_AUTHORIZATION) {
return res.status(431).set("Cache-Control", "no-store").json({
error: "request_header_fields_too_large",
header: "Authorization",
message: "The access token is too large. Request a token with fewer claims.",
});
}
next();
});Next.js App Router
A useful pattern is a cookie budget well under the hard limit, enforced in proxy.ts (the Next.js 16 name for middleware). The request still gets through Node, so you can still respond, and the same response can expire your own cookies that are safe to drop, so the retry the RFC allows actually works:
// proxy.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const COOKIE_BUDGET = 8 * 1024; // well under Node's 16 KiB default
const DISPOSABLE = ["recently_viewed", "ab_homepage", "ab_checkout"];
export function proxy(request: NextRequest) {
const cookieHeader = request.headers.get("cookie") ?? "";
if (cookieHeader.length <= COOKIE_BUDGET) {
return NextResponse.next();
}
const response = NextResponse.json(
{
error: "request_header_fields_too_large",
header: "Cookie",
message: "Too many cookies for this site. Some have been cleared; please retry.",
},
{ status: 431, headers: { "Cache-Control": "no-store" } },
);
for (const name of DISPOSABLE) {
if (request.cookies.has(name)) {
response.cookies.set(name, "", { maxAge: 0, path: "/" });
}
}
return response;
}Only expire cookies your app set itself, with the same Path (and Domain, if any) used when you set them. Otherwise the browser treats the expiry as a different cookie and keeps the original.
NGINX
If you've decided the headers really are legitimate, raise the buffer size. The size of one buffer is the per-field limit:
http {
# Default is 4 8k; each header field must fit in one buffer
large_client_header_buffers 4 16k;
}Keep the layers consistent. With NGINX in front of Node, a header NGINX rejects comes back as a 400 and never reaches your app, while one that passes can still hit Node's 16 KiB total and come back as a 431. Same problem, two different errors.
431 vs 413 vs 414 vs 400
| Code | Meaning | Use when |
|---|---|---|
| 431 Request Header Fields Too Large | The headers are too big | The total header block, or one field such as Cookie or Authorization, is over your limit |
| 413 Payload Too Large | The body is too big | The request content (an upload, a JSON payload) is over your limit (RFC 9110 renamed it Content Too Large) |
| 414 URI Too Long | The URL is too long | The request target is longer than the server will interpret, often a huge query string |
| 400 Bad Request | The request is malformed | The syntax or framing is broken, or the server reports oversized headers this way (NGINX, Apache, AWS ALB) |
The short version: 431 is headers, 413 is the body, 414 is the URL, and 400 is the catch-all most servers use for oversized headers anyway. When you control the response, prefer 431. It tells the client exactly what to shrink, and a 400 doesn't.
431 and SEO
431 rarely affects search, because it comes from state a particular client has built up, and a crawler doesn't carry a returning visitor's cookie jar. If a crawler does get one, Google treats it like any other client error: "All 4xx errors, except 429, are treated the same," and those URLs drop out of the index (Google Search Central). The bigger risk is a CDN caching a 431, which would serve it to everyone, crawlers included.
Common Pitfalls
- Looking for 431 when the proxy sent 400. Search the logs for "Request Header Or Cookie Too Large" and check which layer answered.
- Raising one layer's limit. Raising Node's
maxHeaderSizechanges nothing if NGINX still rejects anything over 8 KB per field. Line up every hop, from CDN to app. - Retrying without changing anything. The identical request fails again. Something has to shrink first, and servers can help by expiring disposable cookies in the 431 response.
- Not naming the header. The RFC says the response SHOULD name the header when one field is at fault. "Your Cookie header is 14 KB" is much more useful than a blank error page.
- Letting a 431 be cached. Send
Cache-Control: no-store, and check your CDN isn't caching 4xx responses.
Wrapping Up
The fix for a 431 is almost always to make the headers smaller, not to raise the limit. The rules of thumb:
- 431 means the request's headers, in total or one field, are over the server's limit, and the request can be retried once they're smaller
- RFC 9110 requires a 4xx for oversized headers, not specifically 431
- Node.js sends a real 431 at 16 KiB. NGINX and Apache send 400 at about 8 KB per field, AWS ALB documents 400 and 413, and Cloudflare documents 413
- The usual causes are cookie bloat, large JWTs, parent-domain cookies and localhost cookies shared across ports
- Fix it at the source with server-side sessions, host-only cookies and fewer token claims, and never let a 431 be cached
For more, see our pages on 431 Request Header Fields Too Large, 413 Payload Too Large, 414 URI Too Long and 400 Bad Request. The body-size version of this problem is in our understanding 413 Payload Too Large post, NGINX's "Request Header Or Cookie Too Large" 400 shows up in understanding 400 Bad Request, and you can compare 413 vs 431 or compare 400 vs 431 side by side.
Related Status Codes
- 400Bad Request
- 401Unauthorized
- 402Payment Required
- 403Forbidden
- 404Not Found
- 405Method Not Allowed
- 406Not Acceptable
- 407Proxy Authentication Required
- 408Request Timeout
- 409Conflict
- 410Gone
- 411Length Required
- 412Precondition Failed
- 413Payload Too Large
- 414URI Too Long
- 415Unsupported Media Type
- 416Range Not Satisfiable
- 417Expectation Failed
- 418I'm a Teapot
- 421Misdirected Request
- 422Unprocessable Entity
- 423Locked
- 424Failed Dependency
- 425Too Early
- 426Upgrade Required
- 428Precondition Required
- 429Too Many Requests
- 431Request Header Fields Too Large
- 451Unavailable For Legal Reasons