Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx answers 400 when a request header or cookie is too big

Error400 Bad Request

Short 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.

  • Applies to nginx 1.26, nginx 1.28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1400 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. 2The 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

What does the error look like?

  • 400 Bad Request
  • 414 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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.