Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx upstream sent too big header while reading response header from upstream

I get error like this:

[error] 27544#0: *47335682 upstream sent too big header while reading response 
 header from upstream, client: 88.88.88.88, server: example..com,
 request: "POST /tool/ HTTP/1.1", upstream: "http://88.88.88.88:7080/tool/",
 host: "example.com"

Regarding to this question, it is possible to increase buffer size from nginx conf file like this: upstream sent too big header while reading response header from upstream

http {
  proxy_buffer_size   128k;
  proxy_buffers   4 256k;
  proxy_busy_buffers_size   256k;
}

location
      fastcgi_buffers 16 16k; 
      fastcgi_buffer_size 32k;

(For future reference, default size for fastcgi_buffer_size and fastcgi_buffers is 4k or 8k, regarding to platform)

This text appears in user's browser: Nginx 502 Bad Gateway

I'm planning to temporarily increase buffer size. Then I can log when the buffers are too big. Is it possible to find out headers which is too big for upstream ? apache_response_headers() and headers_list() didn't give me all response headers. It only gave me expires, cache-control and pragma headers.

Does changing proxy_buffer_size makes a performance problem ?

(nginx version: nginx/1.6.0, php 5.4.42, xcache 3.2)

like image 822
trante Avatar asked Aug 04 '15 11:08

trante


1 Answers

You can easily find an answer on SO, but what really makes a difference is the single configuration option:

http {
  fastcgi_buffer_size 32k;
}

Nevertheless this recommendation is probably not what you want. Let's see through details why that helps to solve problem:

fastcgi_buffer:

Syntax: fastcgi_buffers number size;

Default: fastcgi_buffers 8 4k|8k;

Context: http, server, location

Sets the number and size of the buffers used for reading a response from the FastCGI server, for a single connection. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.

fastcgi_buffer_size:

Syntax: fastcgi_buffer_size size;

Default: fastcgi_buffer_size 4k|8k;

Context: http, server, location

Sets the size of the buffer used for reading the first part of the response received from the FastCGI server. This part usually contains a small response header. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. It can be made smaller, however.

So there is only fastcgi_buffer_size makes a difference because response header doesn't fit into the 4KB buffer. Most of time it happens due to large cookie size. So it's strongly recommended to leave settings as is but reduce cookie size instead and have only minimal amount of data stored there like user_id, session_id, so the general idea cookie storage is for non-sensitive set of IDs. Some browsers don't treat large cookies well.

So the solution would be:

1. Reduce cookie size

2. Get back to original settings

http {
  fastcgi_buffers 8 4k;
  fastcgi_buffer_size 4k;
}

In case of difficulties with reduce cookie size turn off buffering for certain location:

location /request {
  fastcgi_buffering off;
}
like image 66
Anatoly Avatar answered Oct 30 '22 18:10

Anatoly