您的位置:首页 > 运维架构 > Nginx

nginx fastcgi_buffers设置

2016-09-09 17:15 148 查看
打开nginx的warn级别error_log,看到如下信息:

2011/04/23 17:24:08 [warn] 9639#0: *44 an upstream response is buffered to a temporary file /tmp/fastcgi_temp/8/0/0000000008 while reading upstream, client: 118.118.118.118, server: sealinger.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000",
host: "www.sealinger.com"

参考《分析 fastcgi_temp 错误以及 Nginx 的 Buffer 机制》一文:

先简单的说一下 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 时第一个缓冲区的大小,不包含在前者中。所以总计能创建的最大内存缓冲区大小是 8*4K+4K = 36k。而这些缓冲区是根据实际的 Response 大小动态生成的,并不是一次性创建的。比如一个 8K 的页面,Nginx 会创建 2*4K 共 2 个 buffers。

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

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

一步步调整参数,直到没有这个warn信息:

    #syntax: fastcgi_buffers the_number is_size 

    #default: fastcgi_buffers 8 4k/8k 

    #context: http, server, location 

    #fastcgi_buffers 256 4k; # Sets the buffer size to 4k + 256 * 4k = 1028k

    fastcgi_buffers 32 4k;

-----------------------------------------------------------

参考:http://wiki.nginx.org/HttpFcgiModule

fastcgi_buffer_size

syntax: fastcgi_buffer_size the_size 

default: fastcgi_buffer_size 4k/8k 

context: http, server, location 

This directive sets the buffer size for reading the header of the backend FastCGI process. 

By default, the buffer size is equal to the size of one buffer in

fastcgi_buffers

. This directive allows you to set it to an arbitrary value.

fastcgi_buffers

syntax: fastcgi_buffers the_number is_size 

default: fastcgi_buffers 8 4k/8k 

context: http, server, location 

This directive sets the number and the size of the buffers into which the reply from the FastCGI process in the backend is read. 

By default, the size of each buffer is equal to the OS page size. Depending on the platform and architecture this value is one of 4k, 8k or 16k. 

On Linux you can get the page size issuing:

getconf PAGESIZE

it returns the page size in bytes. 

Example:

fastcgi_buffers 256 4k; # Sets the buffer size to 4k + 256 * 4k = 1028k

This means that any reply by the FastCGI process in the backend greater than 1M goes to disk. Only replies below 1M are handled directly in memory.

转自:https://blog.gesha.net/archives/350/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: