nginx answers 400 when a request header or cookie is too big
400 Bad RequestShort answer
nginx rejects a request with 400 when a single header field, usually a cookie, does not fit in one header buffer. The default is 4 buffers of 8k. Raise large_client_header_buffers in the http or server block, or shrink the cookie. HTTP/2 uses the same limit since nginx 1.19.7.
Fix it fast
1Find out which cause you have
Measure the Cookie header the browser sends. Compare its size with the 8k default buffer.
curl -s -o /dev/null -w "%{http_code}\n" -H "Cookie: $(head -c 9000 /dev/zero | tr '\0' 'a')" http://localhost/ sudo nginx -T | grep -n -E "large_client_header_buffers|client_header_buffer_size"2Match what you see, then jump to its fix
- 1
400 appears only for requests that carry a very long header, such as a Cookie header with many cookiesOne header field is bigger than large_client_header_buffersFix - 2
The config sets http2_max_field_size or http2_max_header_size and raising them changes nothingOld HTTP/2 header limits still sit in the configFix
- 1
What does the error look like?
400 Bad Request414 Request-URI Too Large (when the request line, not a header, is too long)
Cause 1 of 2
One header field is bigger than large_client_header_buffers
1Check
A request header field cannot exceed the size of one buffer. The default is 4 buffers of 8k, so a single field over 8k is rejected even though the total limit looks higher. A request line over one buffer gives 414 instead.
sudo nginx -T | grep -n -E "large_client_header_buffers|client_header_buffer_size"2Fix
Raise the buffer size so the largest field fits. Smaller requests are still read into the small client_header_buffer_size buffer first, and larger buffers are allocated only when needed.
http {
large_client_header_buffers 4 16k;
}3Verify
Reload nginx and repeat the request that failed.
sudo nginx -t && sudo nginx -s reload
curl -s -o /dev/null -w "%{http_code}\n" -H "Cookie: $(head -c 9000 /dev/zero | tr '\0' 'a')" http://localhost/Confirmed by nginx.org
Cause 2 of 2
Old HTTP/2 header limits still sit in the config
1Check
Both directives are obsolete since nginx 1.19.7, and the docs name large_client_header_buffers as their replacement.
grep -R -n -E "http2_max_field_size|http2_max_header_size" /etc/nginx/2Fix
Remove the old directives and size large_client_header_buffers for HTTP/2 traffic too.
http {
large_client_header_buffers 4 16k;
}3Verify
Test the config, reload, and request over HTTP/2.
sudo nginx -t && sudo nginx -s reload
curl --http2 -I https://example.com/Confirmed by nginx.org
How do I stop it coming back?
- Keep cookies small: store a session id in the cookie and the data on the server.
- Clear old cookies for the domain when you change cookie names.
- Change buffer sizes at the http level so every site on the server behaves the same.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fix