TL;DR — Quick Summary

Fix and troubleshoot Upstream sent too big header while reading response header from upstream when.... Step-by-step solution for Nginx.

Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current NginX documentation for the latest information.

Resolve: Upstream sent too big header while reading response header from upstream when using Nginx PHP-FPM

When using Nginx as a web server, it often acts as a reverse proxy passing requests to an upstream server like PHP-FPM.

By default, Nginx allocates a specific buffer size to read the response headers coming back from the upstream application. If your PHP application sends back an unusually large response header (often due to massive cookies, complex caching headers, or lots of authorization tokens), the buffer fills up immediately and Nginx drops the connection, throwing an error in your error log:

2013/12/21 22:58:04 [error] 46171#0: *1670 upstream sent too big header while reading response header from upstream, client: 123.123.123.123, server: example.com, request: "GET /wp-admin/admin.php...", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "www.example.com"

The Solution: Increase Buffer Sizes

To resolve this issue, you need to increase the default buffer sizes in your nginx.conf (or within the specific server block for your site). There are two different configurations depending on whether you are proxing to FastCGI (PHP-FPM) or a standard HTTP upstream.

1. If using FastCGI (PHP-FPM)

Add or modify the following fastcgi buffer directives inside your http, server, or location block:

http {
    ...
    fastcgi_buffers 16 32k;
    fastcgi_buffer_size 32k;
    ...
}

2. If using a Standard Proxy (e.g., Node.js, Python, or external cache)

If the error occurs on a standard proxy pass rather than FastCGI, you need to adjust the proxy buffers instead:

http {
    ...
    proxy_buffer_size 64k;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 128k;
    ...
}

Warning: If you are also using the proxy_temp_file_write_size directive, remember that its value must be equal to or greater than the maximum value of proxy_buffer_size and one of the proxy_buffers.

Restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Your server should now have enough buffer space to parse large headers from the upstream application.