DEV Community

Daniele Michelin
Daniele Michelin

Posted on Originally published at daniele-michelin.com

nginx: upstream sent too big header – fix

The backend's response headers (usually cookies) are larger than the buffer nginx reserves for them; nginx gives up and returns 502 Bad Gateway.

Common causes

  • Large or many Set-Cookie headers, typical after SSO logins (SAML, OIDC) with long tokens.
  • Long Content-Security-Policy or Link headers.
  • Default 4 KB / 8 KB buffers (fastcgi_buffer_size, proxy_buffer_size) are too small.
  • Application debug output ends up in headers.

How to check it

Find the error in the nginx log

grep 'too big header' /var/log/nginx/error.log | tail -n 5
Enter fullscreen mode Exit fullscreen mode

Measure the response header size

curl -s -o /dev/null -w '%{size_header}\n' https://cloud.example.com/
Enter fullscreen mode Exit fullscreen mode

Look at the full headers

curl -sI https://cloud.example.com/login
Enter fullscreen mode Exit fullscreen mode

What actually helps

  1. For PHP-FPM: fastcgi_buffer_size 32k; and fastcgi_buffers 16 16k; in the matching location block.
  2. For a reverse proxy: proxy_buffer_size 16k;, proxy_buffers 8 16k;, proxy_busy_buffers_size 32k;.
  3. Apply with nginx -t && systemctl reload nginx.

FAQ

Why does it only happen after the SSO login?

On the redirect back from the identity provider the application sets several session and SAML cookies at once. That one response overflows the buffer.

Can I make the buffers as large as I want?

The values apply per connection. 32–64 KB is harmless; much more points to a problem in the application.


Full page with every command and the German/Italian version: daniele-michelin.com/en/errors/nginx-upstream-sent-too-big-header/

First published at daniele-michelin.com. The error lexicon covers HTTP, DNS, TLS, mail, database, PHP, nginx, systemd and Nextcloud, in German, English and Italian.

Top comments (0)