Nginx: [error] upstream sent too big header while reading response header from upstream (502 Bad Gateway)
The backend's response headers (cookies, Set-Cookie, long headers) exceeded Nginx's proxy/fastcgi buffer size.
upstream sent too big header while reading response header from upstream[error] upstream sent too big header while reading response header from upstream502 Bad Gateway upstream sent too big headerFastCGI sent in stderr: "Primary script unknown"upstream sent too big header Meaning
Increase buffers: for proxy — proxy_buffer_size 16k; proxy_buffers 4 16k; proxy_busy_buffers_size 32k;; for PHP-FPM — fastcgi_buffer_size 16k; fastcgi_buffers 16 16k;. Alternatively shrink headers (fewer/huge cookies, giant redirect URLs, debug headers).
Common causes
- Large cookies/session headers
- Many Set-Cookie headers
- Long Location or custom headers
- Framework debug headers (Laravel Debugbar)
- Default buffer 4k/8k too small
⚡ Quick fix
- Raise
proxy_buffer_size/fastcgi_buffer_sizeand related buffers - Reduce cookie/header size
- Remove debug headers in production
Detailed fix by platform
Nginx
proxy_buffer_size 16k; proxy_buffers 4 16k; proxy_busy_buffers_size 32k;thennginx -t && systemctl reload nginx
How to diagnose
- Header — Which is huge?
- Buffers — Sizes?
- Source — Cookies/debug?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 414 Request-URI Too Large / 400 Request Header Or Cookie Too Large Nginx: 400 Bad Request — Request Header Or Cookie Too Large / client sent too long header line
- 502 HTTP 502 Bad Gateway
- FastCGI sent in stderr: PHP message Nginx: [error] FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect()" while reading response header from upstream
Most viewed in Nginx / Apache
- nginx: [emerg] unknown directive Nginx: nginx: [emerg] unknown directive "x" in /etc/nginx/sites-enabled/x:12 — configuration file test failed
- upstream timed out Nginx: upstream timed out (110: Connection timed out) while reading response header from upstream
- AH00558 Apache: AH00558: apache2: Could not reliably determine the server's fully qualified domain name
Other ways to find it
Where this error sits
Origin: Edge & web server. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026