upstream sent too big header while reading response header Nginx / Apache Origin: Edge & web server OSI layer 7 · Application Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: Nginx Linux

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

  1. Raise proxy_buffer_size/fastcgi_buffer_size and related buffers
  2. Reduce cookie/header size
  3. Remove debug headers in production

Detailed fix by platform

Nginx

  1. proxy_buffer_size 16k; proxy_buffers 4 16k; proxy_busy_buffers_size 32k; then nginx -t && systemctl reload nginx

How to diagnose

  1. Header — Which is huge?
  2. Buffers — Sizes?
  3. 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:

Where this error sits

Origin: Edge & web server. Errors that are often confused with or caused by this one (click to open):

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 (Nginx / Apache)414 Request-URI Too L…502 — HTTP 502 Bad Gateway (HTTP)502FastCGI 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 (Nginx / Apache)FastCGI sent in stder…no resolver defined to resolve example.com — Nginx: [error] no resolver defined to resolve api.example.com, client: 203.0.113.5 (502 when proxy_pass uses a variable) (Nginx / Apache)no resolver defined t…proxy_pass with a URI changes the path (404 from upstream) — Nginx: reverse proxy returns 404 — location /api/ { proxy_pass http://app:3000/; } strips the prefix (or keeps it) (Nginx / Apache)proxy_pass with a URI…recv() failed (104: Connection reset by peer) while reading response header — Nginx: [error] recv() failed (104: Connection reset by peer) while reading response header from upstream (Nginx / Apache)recv() failed (104: C…SSL_do_handshake() failed while SSL handshaking to upstream — Nginx: [error] SSL_do_handshake() failed (SSL: error:0A000410:SSL routines::sslv3 alert handshake failure) while SSL handshaking to upstream (Nginx / Apache)SSL_do_handshake() fa…WebSocket connection failed through Nginx (missing Upgrade headers) — Browser: WebSocket connection to 'wss://example.com/socket' failed: Error during WebSocket handshake: Unexpected response code: 400 (nginx proxy) (Nginx / Apache)WebSocket connection …upstream …

Explore the map of this error Full error map →

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