Tag Archives: Nginx 502 Error

Nginx an upstream response is buffered to a temporary file,nginx502 Error

1.Error: warn:an upstream response is buffered to a temporary file

Solution: Add fastcgi_buffers 8 4K; fastcgi_buffer_size 4K;

2. a client request body is buffered to a temporary file

Solution: Add client_max_body_size 2050m; client_body_buffer_size 1024k;

Buffer mechanism of nginx:

For the response from the fastcgi server, nginx buffers it into memory and sends it to the client browser in turn. The size of the buffer is determined by fastcgi_ Buffers and fastcgi_ buffer_ Size is controlled by two values

For example, the configuration is as follows:

fastcgi_buffers      8 4K; fastcgi_buffer_size 4K;

fastcgi_ Buffers control nginx to create up to eight 4K buffers, while fastcgi controls nginx to create up to eight 4K buffers_ buffer_ Size is the size of the first buffer in response processing, which is not included in the former. So the total maximum memory buffer size that can be created is 8 * 4K + 4K = 36K. These buffers are generated dynamically according to the actual response size and are not created at one time. For example, for an 8K page, nginx will create 2 * 4K buffers

When the response is less than or equal to 36K, all data will be processed in memory. What if the response is greater than 36K?fastcgi_ That’s what temps do. The extra data will be temporarily written to the file and placed under this directory. At the same time, you will see a warning like this in error. Log

2010/03/13 03:42:22 [warn] 3994#0: *1 an upstream response is buffered to a temporary file /usr/local/nginx/fastcgi_temp/1/00/0000000001 while reading upstream, client: 192.168.1.111, server: www.xxx.cn, request: "POST /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx.cn", referrer: "http://xxx.cn/test.php"

Obviously, if the buffer is set too small, nginx will read and write to the hard disk frequently, which has a great impact on the performance, but it is not that the larger the better, which is meaningless

Fix Nginx 502 Error:upstream sent too big header while reading response header from upstream

The value of cookies is out of range I mean

Looked at the logs

Error 502 upstream sent too big header while reading response header from upstream

 

sudo gedit /var/log/nginx/error.log

check the error log

 

 

upstream sent too big header while reading response header from upstream

If you search for this error, the explanation on the Internet is similar, but it’s just that the cookie carries too much header and makes you set.

fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;

Try it step by step. The phrase fastcgi_buffers 8 128k, fastcgi_buffers 32 32k is better, the memory is allocated and released as a whole, reduce the number of units k can be used as much as possible.

In addition, if you use nginx for load balancing, it is useless to change the above parameters, to be in the configuration of forwarding, such as the following settings.

 

location @to_other {

                proxy_buffer_size  128k;

                proxy_buffers   32 32k;

                proxy_busy_buffers_size 128k;

add_header X-Static transfer;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP  $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://backend;

}

The three bolded lines are the only ones that will work.

fastcgi_* can be interpreted as the response used by nginx when accepting client requests. proxy is used by nginx when forwarding as a client, if the header is too big and exceeds the default 1k, it will trigger the above upstream sent too big header.

 

location ~ \.php$ {

fastcgi_buffer_size 128k;

fastcgi_buffers 32 32k;

include /etc/nginx/fastcgi_params;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /host/web/$fastcgi_script_name;

}