欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

nginx an upstream response is buffered to a temporary file 有大用

nginx an upstream response is buffered to a temporary file

       
Darker_坤IP属地: 北京            
字数 427阅读 3,437            


查看Nginx日志,发现有很多报错

2021/02/27 17:00:54 [warn] 28#28: *49939 
an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/6/14/0000000146 while reading upstream, client: xxx.xxx.xxx.x, 
server: airflow.yidianshihui.com, request: "GET /home HTTP/1.1", upstream: "http://xxx.xxx.xxx.xxx:xxxx/home", host: "airflow.yidianshihui.com", 
referrer: "http://airflow.yidianshihui.com/home?tags=%E6%AF%8F%E6%97%A5ic_sku%E5%90%8C%E6%AD%A5"
   

1.错误日志:warn:an upstream response is buffered to a temporary file

解决办法:增加fastcgi_buffers      8 4K;     fastcgi_buffer_size  4K;
   

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

解决办法:增加client_max_body_size 2050m; client_body_buffer_size 1024k;

Nginx 的 buffer 机制:

对于来自 FastCGI Server 的 Response,Nginx 将其缓冲到内存中,然后依次发送到客户端浏览器。缓冲区的大小由 fastcgi_buffers 和 fastcgi_buffer_size 两个值控制。

比如如下配置:

fastcgi_buffers      8 4K;
fastcgi_buffer_size  4K;
   

fastcgi_buffers 控制 nginx 最多创建 8 个大小为 4K 的缓冲区,而 fastcgi_buffer_size 则是处理 Response 时第一个缓冲区的大小,不包含在前者中。所以总计能创建的最大内存缓冲区大小是 84K+4K = 36k。而这些缓冲区是根据实际的 Response 大小动态生成的,并不是一次性创建的。比如一个 8K 的页面,Nginx 会创建 24K 共 2 个 buffers。

当 Response 小于等于 36k 时,所有数据当然全部在内存中处理。如果 Response 大于 36k 呢?fastcgi_temp 的作用就在于此。多出来的数据会被临时写入到文件中,放在这个目录下面。同时你会在 error.log 中看到一条类似 warning:

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"
   

显然,缓冲区设置的太小的话,Nginx 会频繁读写硬盘,对性能有很大的影响,但也不是越大越好,没意义

官方文档:
http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size    

修改Nginx配置:

location /api {
        proxy_set_header  Host  $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass   http://elk_server/api;
        client_body_buffer_size 1024k;
        client_max_body_size 2050m;
        fastcgi_buffers 8 4K;
        fastcgi_buffer_size 4K;
    }
   

参考:
Nginx性能调优之buffer参数设置
nginx常见问题    


来自  https://www.jianshu.com/p/ac8b39b987e1   


               

an upstream response is buffered to a temporary file  


an upstream response is buffered to a temporary file
  • 1

这个错误也不会影响功能,意思是nginx默认的buffer太小,每个请求的缓存太小,请求头header太大时会出现缓存不足,内存放不下上传的文件,就写入到了磁盘中,使nginx的io太多,造成访问中断。

缓解的方法:

nginx配置文件nginx.conf中加入配置:(根据实际情况配置数值)
client_max_body_size 2048m;
  • 1

(设置请求体的大小,用nginx来做webserver的时,上传大文件时需要特别注意这个参数,否则会中断在nginx的请求中是无法记录到访问的)
client_body_buffer_size 1024k;
(Nginx分配给请求数据的Buffer大小,如果请求的数据小于client_body_buffer_size直接将数据先在内存中存储。)

proxy_buffer_size  256k;
  • 1

(proxy_buffer_size所设置的buffer_size的作用是用来存储upstream端response的header)

proxy_buffering  on;
  • 1

(proxy_buffering 是为了开启response buffering of the proxied server,开启后proxy_buffers和proxy_busy_buffers_size参数才会起作用)

proxy_buffers 64 128k;
proxy_busy_buffers_size 512k;
  • 1

  • 2

缓冲区设置的太小的话,Nginx 会频繁读写硬盘,对性能有很大的影响,但也不是越大越好,没意义

若配置完上面后日志中还是频繁出现,则配置:
fastcgi_buffer_size       512k;
fastcgi_buffers        6  512k;
fastcgi_busy_buffers_size  512k;
fastcgi_temp_file_write_size  512k;
fastcgi_intercept_errors    on;
  • 1

  • 2

  • 3

  • 4

  • 5

重启nginx 或者 nginx -s reload

创作者:吴仔汕
欢迎大家参考,还可以提出疑问或者不同看法噢。
原创作品,转载请标明出处!!

文章知识点与官方知识档案匹配,可进一步学习相关知识

CS入门技能树Linux入门初识Linux27216 人正在系统学习中


来自   https://blog.csdn.net/Zisson_no_error/article/details/119796369


如何修复 nginx 上游响应缓冲到临时文件错误

05/08/2017 提示与技巧 发表评论

运行 Nginx 一段时间后。您可以阅读我如何 在 Centos 上使用 Linux、Nginx、PHP-FPM 和 MySQL 安装 LEMP 服务器我从 Centos 上的 nginx 收到大量警告日志,上游响应被缓冲到一个我不喜欢看到的临时文件。问题是我没有为 nginx 设置 fastcgi_buffer 所以 nginx 发出警告上游响应被缓冲到临时文件 /var/cache/nginx/fastcgi_temp/。

查看Nginx的错误日志

# cat /var/log/nginx/error.log

有许多上游响应缓冲到临时文件 /var/cache/nginx/fastcgi_temp/ 来自 forum.namhuy.net 的警告日志。警告消息表示响应已缓冲到磁盘。这不是那么重要的问题,但我不想出现任何错误。您可以在 .conf 文件中使用“fastcgi_max_temp_file 0”禁用磁盘缓冲或设置 fastcgi_buffers 和 fastcgi_buffer_size

2014 / 05 / 10  03 : 56 : 06  [ warn ]  1483 #0: *144520 在读取上游时,上游响应被缓冲到临时文件 /var/cache/nginx/fastcgi_temp/5/78/0000003785,客户端:54.86。 133.62,服务器:forum.namhuy.net,请求:“GET /style.php?id=1&lang=en&sid=69475f2bd936d3e96ef925ce546a79c9 HTTP/1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“论坛。 namhuy.net", referrer: "http://forum.namhuy.net/viewforum.php?f=3&sid=74d333aafeefe7332a418519e8b03277" 
2014 / 05 / 10  03 : 56 :28  [警告]  1483#0: *144518 上游响应缓冲到临时文件 /var/cache/nginx/fastcgi_temp/6/78/0000003786 读取上游时,客户端:54.86.133.62,服务器:forum.namhuy.net,请求:“GET /style.php?id=1&lang=en HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "forum.namhuy.net", referrer: "http://forum.namhuy. net/posting.php? 
mode = post & f =3&sid=69475f2bd936d3e96ef925ce546a79c9 " 2014 / 05 / 10  03:57:40 [警告] 1483  #0: *144673 上游响应缓冲到临时文件 /var/cache/nginx/fastcgi_temp/7/78/0000003787 读取上游时,客户端:198.204.244.53,服务器:forum.namhuy.net,请求:“GET /style.php?id=1&lang=en&sid=b70a193e6808fb2183ac1c0228097227 HTTP/1.1",上游:"fastcgi://127.0.0.1:9000",主机:"forum.namhuy.net",引用者:"http://forum. 
namhuy.net/viewforum.php?f=2&sid=74d333aafeefe7332a418519e8b03277 " 2014 / 05 / 10  03:58:06 [警告] 6276 _ _ _  #0: *37 在读取上游时,上游响应被缓冲到临时文件 /var/cache/nginx/fastcgi_temp/1/00/0000000001,客户端:198.204.244.53,服务器:forum.namhuy.net,请求:“GET /style.php?id=1&lang=en HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "forum.namhuy.net", referrer: "http://forum.namhuy. net/viewtopic.php?f=2&t=2&sid=b70a193e6808fb2183ac1c0228097227"

禁用 fastcgi 磁盘缓冲

# nano /etc/nginx/conf.d/default.conf

并将此行添加到 location ~ \.php$ { fastcgi configurations }

fastcgi_max_temp_file 0;

你应该有这样的东西

     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 0;
        include        fastcgi_params;
    }

To set fastcgi_buffer_size and fastcgi_buffer

# nano /etc/nginx/conf.d/default.conf

and add this two lines to location ~ \.php$ { fastcgi configurations }

fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;

You should have something look like this

    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 0;
        fastcgi_buffer_size 4K;
        fastcgi_buffers 64 4k;
        include        fastcgi_params;
    }

Fastcgi buffer will store responses up to 4K+64*4K=260K in memory.

来自  https://itbeginner.net/fix-nginx-upstream-response-buffered-temporary-file-error/




K48373902:[警告] 错误日志中的消息:上游响应在读取上游时缓冲到临时文件

描述

NGINX Plus 错误日志包含如下警告:

2020/01/01 11:11:54 [warn] 1234#1234: *123 在读取上游时,上游响应被缓冲到临时文件 /var/cache/nginx/proxy_temp/1/00/0000000001,客户端:xxx。 xxx.xxx.xxx,服务器:example.com,请求:“GET /image.png HTTP/1.1”,上游:“ http://127.0.0.1:8080/image.png ”,主机:“ example.com ” , 推荐人:“ http://example.com

环境

  • NGINX Plus 作为反向代理 (proxy_pass, fastcgi_pass)

原因

警告消息表明响应已缓冲到磁盘,因为它不适合配置的内存缓冲区。

建议的操作

根据您的用例,您有三个选项:

    选项 1:增加缓冲区

    您可以调整缓冲区以适应完整的上游响应。如果最大可能的响应大小未知,我们建议将两个参数都增加两倍,直到警告停止出现。

    proxy_buffers 16 16k;

    proxy_buffer_size 16k;

    根据官方文档,默认值为proxy_buffers 4k/8k 取决于平台),proxy_buffers_size它由默认内存页面大小决定(要找出它,只需运行getconf PAGESIZE)。


      选项 2:禁用缓冲

      设置proxy_buffering关闭

      如果响应太大(例如,5 GB 的电影),您可以完全禁用响应缓冲以将响应直接流式传输到客户端。

      proxy_buffering off;
      默认值为on将其设置为off,会将响应直接流式传输到客户端。虽然不推荐!

        选项 3:忽略这些警告

        您至少可以什么都不做,但要留意关键的服务器性能指标:CPU磁盘 I/O内存

        这些警告是无害的,您可以忽略它们,除非您发现磁盘 I/O 使用率很高。 来自  https://my.f5.com/manage/s/article/K48373902


        upstream response is buffered to a temporary file请求帮助:在中找到如何修复日志error.log #3020


        问题描述

        我在访问静态资源(js、css 等)时总是收到以下警告,有什么方法可以设置它来解决问题。

        an upstream response is buffered to a temporary file /usr/local/apisix/proxy_temp/6/85/0000116856 while reading upstream, client
        • apisix 版本 (cmd: apisix version): 2.0

        • 操作系统:Centos 7


        这是因为 Nginx 上游模块默认具有非零临时文件大小配置:

        语法:proxy_max_temp_file_size 大小;
        默认值:
        proxy_max_temp_file_size 1024m;
        上下文:http,服务器,位置
        当启用代理服务器的响应缓冲,并且整个响应不适合由 proxy_buffer_size 和 proxy_buffers 指令设置的缓冲区时
        ,部分响应可以保存到临时文件中。该指令设置临时文件的最大大小。一次写入临时文件的数据大小由
        proxy_temp_file_write_size 指令设置。
        零值禁用对临时文件的响应缓冲。

        所以当请求超出内存缓冲区时,数据将被保存到文件中。

        别担心,这不是问题。


        来自   https://github.com/apache/apisix/issues/3020


        普通分类: