nginx,

How to resolve Nginx 'an upstream response is buffered to a temporary file' error

Mar 08, 2022 · 1 min read · Post a comment

After connecting Nginx with ELK stack to handle requests I spot a lot of an upstream response is buffered to a temporary file /var/cache/nginx/fastcgi_temp/9/66/00000034785 while reading upstream. The thing is that the fastcgi buffering is disabled and all requests are stored on disk in temporary files. These warnings can pile up the disk space and cause the server to operate weirdly.

Here I’m going to show you how can you easily fix it.

Prerequisites

  • Linux bash environment
  • Nginx

Solution

I assume that you should have location ~ \.php$ in your Nginx app configuration file, and it should look like:

location ~ \.php$ {
    root    /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

To disable FastCGI disk buffering and get rid of all the an upstream response is buffered to a temporary file errors add this line:

fastcgi_max_temp_file_size 0;

in location ~ \.php$.

If you want to buffer the requests into the server or container memory you can use the following config:

fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;

The FastCGI buffer will store 4K + 64*4K = 260K in memory.

The final location ~ \.php$ block should look like:

location ~ \.php$ {
    root           /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_max_temp_file_size 0;
    fastcgi_buffer_size 4K;
    fastcgi_buffers 64 4k;
    include        fastcgi_params;
}

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.