Where 502 comes from, even with proxy_buffering off
When nginx contacts a backend, it reads the first part of the response — the status line and all HTTP
headers — into a single buffer sized by proxy_buffer_size. By default its size is one system memory page
(getconf PAGESIZE): 4k on Linux x86_64 and ARM64 (8k on platforms with an 8 KB page size).
Once the backend’s response headers exceed 4 KB in total, nginx aborts the response, returns 502 Bad Gateway
to the client, and logs this in /var/log/nginx/error.log:
[error] upstream sent too big header while reading response header from upstream,
client: 203.0.113.10, server: app.example.org,
request: "GET /oauth/callback HTTP/2.0",
upstream: "http://127.0.0.1:8080/oauth/callback", host: "app.example.org"
Most often the 4k ceiling is hit by:
- OIDC and SSO (Keycloak, Authentik, Authelia, oauth2-proxy): on the login callback, the provider returns
a long
Locationheader or severalSet-Cookieheaders carrying an encrypted JWT and role list. - Large security and debug headers: a verbose
Content-Security-Policygenerated by the app, or Symfony/Laravel debug headers (X-Debug-Token, profilers). - PHP apps with plugins: WordPress with SSO, WooCommerce, or cache-tag headers (
X-Cache-Tags) served overfastcgi_pass.
A common mistake is trying to bypass the limit with proxy_buffering off;. Disabling buffering stops
response body buffering; nginx still reads response headers into proxy_buffer_size, so the 502 stays.
Why nginx rejects a standalone proxy_buffer_size 16k;
Adding proxy_buffer_size 16k; (or 32k) on its own makes nginx -t fail:
nginx: [emerg] "proxy_busy_buffers_size" must be less than the size of all "proxy_buffers" minus one buffer
nginx checks two constraints at startup:
proxy_busy_buffers_sizemust be at least as large as the larger ofproxy_buffer_sizeand oneproxy_buffersbuffer.proxy_busy_buffers_sizemust be strictly less than the total size ofproxy_buffersminus one buffer.
By default proxy_buffers is 8 4k (32 KB, or 28 KB minus one buffer), and proxy_busy_buffers_size
defaults to twice proxy_buffer_size. At 8k and 12k that still fits under 28 KB, but at 16k doubling
it gives 32k — breaking the second rule. Set all three buffers as a matching trio:
location / {
proxy_buffer_size 16k; # backend response headers (instead of 4k)
proxy_buffers 4 16k; # 4 buffers of 16 KB = 64 KB per connection
proxy_busy_buffers_size 32k; # 32k >= 16k and 32k < (64k - 16k)
proxy_pass http://127.0.0.1:8080;
}
If a JWT or Set-Cookie chain exceeds 16 KB, step up to 32k, 4 32k, and 64k. Raise these buffers in
the server or location block of the SSO service rather than globally in http. nginx allocates
proxy_buffer_size per active proxied connection, and proxy_buffers grows up to N × size while
buffering responses (see Tuning & compression for body buffering).
PHP-FPM, gRPC, and the auth_request trap
proxy_buffer_size applies to proxy_pass. Other backend modules in nginx use their own buffer directives —
error.log still prints the exact same upstream sent too big header message, with only the upstream:
scheme changing (fastcgi://, uwsgi://, grpc://).
| Backend module | Directives | Default (4 KB page) |
|---|---|---|
proxy_pass (HTTP) |
proxy_buffer_size, proxy_buffers, proxy_busy_buffers_size |
4k, 8 4k, 8k |
fastcgi_pass (PHP-FPM) |
fastcgi_buffer_size, fastcgi_buffers, fastcgi_busy_buffers_size |
4k, 8 4k, 8k |
uwsgi_pass / scgi_pass |
uwsgi_buffer_size, uwsgi_buffers, uwsgi_busy_buffers_size (and scgi_*) |
4k, 8 4k, 8k |
grpc_pass (gRPC) |
grpc_buffer_size (no separate *_buffers) |
4k |
For PHP-FPM (PHP-FPM), add the trio inside the .php location block:
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_busy_buffers_size 32k;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Another trap shows up when protecting services with auth_request (Authelia, Authentik, oauth2-proxy —
Limits & access). If the auth subrequest itself returns oversized headers, it fails with 502,
and the client gets 500 Internal Server Error — with two consecutive lines in error.log:
[error] upstream sent too big header ..., request: "GET / HTTP/2.0", subrequest: "/_auth", upstream: "http://127.0.0.1:9091/api/verify"
[error] auth request unexpected status: 502 while sending to client
Raising the buffers in location / won’t help here: put them in the subrequest’s location = /_auth block
(or at the server level so both locations inherit them):
location = /_auth {
internal;
proxy_pass http://127.0.0.1:9091/api/verify;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_buffer_size 16k;
proxy_buffers 4 16k;
proxy_busy_buffers_size 32k;
}
Don’t mix up backend 502 and client 400
Headers in a proxy flow both ways, and each direction has its own error and directive:
| Direction | What the client sees | What appears in error.log |
Directive to raise |
|---|---|---|---|
| Backend to nginx | 502 Bad Gateway (or 500 with auth_request) |
[error] upstream sent too big header |
proxy_buffer_size + proxy_buffers + proxy_busy_buffers_size (or fastcgi_*) |
| Client to nginx | 400 Bad Request (Request Header Or Cookie Too Large) |
Nothing at error level; at info level: client sent too long header line |
large_client_header_buffers 4 16k; in http or server (default 4 8k, Hardening) |
OIDC and SSO often push both directions over the default: the provider sets large Set-Cookie headers in
the response (requiring proxy_buffer_size), and on the next request the browser sends them back in Cookie
or Authorization: Bearer … — if any single header line exceeds 8k, you also need
large_client_header_buffers 4 16k; in the server block.
How to check
- Measure the backend’s response headers directly (
-D -prints theGETresponse headers while-o /dev/nulldiscards the body) to pick16kor32kbased on the real size:curl -sSD - -o /dev/null http://127.0.0.1:8080/oauth/callback | wc -c - Validate buffer constraints and reload without dropping connections (reload instead of restart):
sudo nginx -t && sudo systemctl reload nginx - Repeat the request through nginx and confirm no new
upstream sent too big headerlines appear inerror.log:sudo tail -n 20 /var/log/nginx/error.log
If it doesn’t work
Added proxy_buffer_size 16k; and nginx -t failed with "proxy_busy_buffers_size" must be less than the size of all "proxy_buffers" minus one buffer
- Cause
- With the default
proxy_buffers 8 4k(32 KB total),proxy_busy_buffers_sizedefaults to twiceproxy_buffer_size(32k), exceeding the32k - 4k = 28klimit. - Fix
- Set all three directives together:
proxy_buffer_size 16k; proxy_buffers 4 16k; proxy_busy_buffers_size 32k;.
Raised proxy_buffer_size in location /, but logging in through auth_request (Authelia, Authentik, oauth2-proxy) returns 500 Internal Server Error
- Cause
- The auth subrequest (
/_auth) returned the large headers and failed with502, whichauth_requestturned into500(auth request unexpected status: 502). - Fix
- Set
proxy_buffer_size,proxy_buffers, andproxy_busy_buffers_sizeinside theauth_requestsubrequestlocation, or higher up in the sharedserverblock (Limits & access).
Set proxy_buffer_size 32k; on a WordPress or Laravel site, but upstream sent too big header still appears in error.log
- Cause
- The log line says
upstream: "fastcgi://..."— requests go to PHP-FPM overfastcgi_pass, whereproxy_*directives don’t apply. - Fix
- Set the FastCGI trio instead:
fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_busy_buffers_size 32k;(PHP-FPM).
Turned buffering off with proxy_buffering off;, yet 502 with upstream sent too big header still happens
- Cause
- Even with
proxy_buffering off, nginx still reads the backend status line and response headers intoproxy_buffer_size. - Fix
- Raise
proxy_buffer_size(along withproxy_buffersandproxy_busy_buffers_size) in the relevantlocationorserverblock.
After raising proxy_buffer_size, SSO login works, but the next page returns 400 Bad Request with Request Header Or Cookie Too Large
- Cause
- The backend set large cookies, and the browser now sends them back in a
Cookieheader over8k— hitting the client request header limitlarge_client_header_buffers(default4 8k). - Fix
- Raise
large_client_header_buffers 4 16k;in that site’sserverblock (Hardening).