How many nginx buffers is too many?

2020-06-03 07:14发布

问题:

Reading the nginx documentation, the proxy_buffer command has this explanatory message:

This directive sets the number and the size of buffers, into which will be read the answer, obtained from the proxied server. By default, the size of one buffer is equal to the size of page. Depending on platform this is either 4K or 8K.

The default is eight 4k or 8k buffers. Why did the authors of nginx choose eight, and not a higher number? What could go wrong if I add more buffers, or a bigger buffer size?

回答1:

nginx is built to be efficient with memory and its default configurations are also light on memory usage. Nothing will go wrong if you add more buffers, but nginx will consume more RAM.

Eight buffers was probably chosen as the smallest effective count that was a square of two. Four would be too few, and 16 would be greater than the default needs of nginx.

The “too many buffers” answer depends on your performance needs, memory availability, and request concurrency. The “good” threshold to stay under is the point at which your server has to swap memory to disk. The “best” answer is: as few buffers as are necessary to ensure nginx never writes to disk (check your error logs to find out if it is).

Here are nginx configurations I use for a large PHP-FPM application on web hosts with 32 GB of RAM:

 client_body_buffer_size      2m;
 client_header_buffer_size    16k;
 large_client_header_buffers  8 8k;
 fastcgi_buffers              512 16k;
 fastcgi_buffer_size          512k;
 fastcgi_busy_buffers_size    512k;

These configurations were determined through some trial and error and by increasing values from nginx configuration guides around the web. The header buffers remain small because HTTP headers tend to be lightweight. The client and fastcgi buffers have been increased to deal with complex HTML pages and an XML API.