output_buffers
The output_buffers directive configures the number and size of buffers used for reading the response body from the upstream server.
Description
The output_buffers directive sets the number and size of buffers that NGINX uses to hold the response sent to the client. When a response is generated, NGINX needs to manage output efficiently, especially for larger responses. This directive helps optimize that management by allowing the admin to specify how many buffers, and how large each buffer should be, based on the expected size of the responses. It is particularly useful for performance tuning, as having fewer larger buffers may reduce overhead but might also lead to higher memory consumption.
The syntax for the directive requires two parameters: the first is the number of buffers, and the second is the size of each buffer. For example, output_buffers 2 16k; configures NGINX to use 2 buffers, each with a size of 16 kilobytes. This allocation means that up to 32 kilobytes can be buffered at once before starting to send data to the client. This directive can be declared within the http, server, or location contexts, providing flexibility for different parts of a server configuration.
Care should be taken to choose appropriate buffer sizes and counts based on traffic patterns and response sizes. A too-small buffer may lead NGINX to frequently write to the client, reducing performance, while excessively large buffers can result in wasteful memory usage. It’s advisable to monitor response sizes and performance continually when fine-tuning these values.
Config Example
http {
output_buffers 4 32k;
}Using improper sizes can lead to inefficient memory usage and poor performance.
While increasing the buffer size can enhance performance for large responses, it may waste memory for smaller responses.