Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

cakephp - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...