What is proxy buffering
As a common reverse proxy, Nginx provides proxy buffering. It allows Nginx to read the response from the server and cache it in local memory or disk, and then send it to the client at the appropriate speed.
Why have proxy buffering
In the Internet, as the reverse proxy layer, NGINx is often in the same room as the server. The network quality between nginx and the server is so good that Nginx can read responses from the server very quickly.
However, the client is usually the user network, and the network quality to nginx is not controllable. It is definitely much lower than the network speed between Nginx and the back-end server.
This leads to a problem: Because the connection from the client to Nginx is too slow, nginx can only send the response to the client at a slower speed. As a result, the back-end server can only pass the response to Nginx at the same slow speed, resulting in a long connection time per request.
In the case of high concurrency, the back-end server may have a large backlog of connections, eventually dragging down the server side.
Advantages and disadvantages of proxy buffering
Advantages of proxy buffering
With proxy caching enabled, Nginx can read and buffer the response body to local memory or disk as quickly as possible, and then simultaneously deliver the response to the client at the appropriate network speed, depending on the network quality of the client.
This not only solves the problem of too many connections to the server, but also ensures the continuous and stable transfer of responses to the client.
Disadvantages of proxy buffering
-
Enabling proxy caching consumes memory on the Nginx server. If there are too many requests, nginx may consume too much memory. So the buffer size Settings need to be evaluated based on the actual server configuration and volume of requests.
-
In the case of a response that is too large for the buffer to hold the entire response body, nginx writes the rest to a temporary file on disk. In the case of a large number of requests, the DISK I/OS of the Nginx server may be too high. Temporary files are stored in/TMP. The/TMP directory is generally mounted on a system disk. If the I/OS of the system disk are too high, the system load increases.
-
In cases where the client network quality is good, such as when the client is in the same room as nginx, it is more efficient to turn off the proxy buffer and forward the response directly to the client in real time.
Configuration parameters for proxy buffering
Nginx has some common configuration parameters for controlling proxy buffers. These parameters are available in HTTP blocks, server blocks, or Location blocks.
1. proxy_buffering
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
Copy the code
This parameter allows the user to enable or disable the proxy buffer.
When the proxy buffer is enabled, nginx reads the response from the server as soon as possible and caches it in the memory-allocated buffer. The size of the proxy buffer is specified by the proxy_buffer_size and proxy_buffers parameters.
When the response size exceeds the memory buffer, a portion of the response body can be stored in a temporary file on disk. The size of temporary files on disk is specified by the proxy_max_temp_file_size and proxy_temp_file_write_size parameters.
2. proxy_buffer_size
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
Copy the code
This parameter sets the size of the cache used to read the first part of the response from the server. This section usually contains a small Response header.
Typically, this buffer size is set to the size of a memory page, 4k or 8K, depending on the server platform. You can make it smaller, but you don’t have to make it too big, because it’s just used to buffer the initial part of the response.
Note that the proxy_BUFFer_SIZE parameter is not affected by whether proxy_buffering is turned on or off. It’s always in effect.
3. proxy_buffers
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
Copy the code
This parameter sets the number and size of buffers used to read responses from the server. By default, the single buffer size is usually set to the size of one memory page, such as 4K or 8K.
It is important to note that the buffer size is set for each requested connection. This means that for each connection, a “number*size” memory buffer is allocated.
Also note that the buffer configuration size is evaluated based on the server configuration and volume of requests; Avoid nginx running out of memory.
4. proxy_busy_buffers_size
Syntax: proxy_busy_buffers_size size;
Default: proxy_busy_buffers_size 8k|16k;
Context: http, server, location
Copy the code
This parameter is used to set the buffer size that is allowed to start sending a response to the client when the complete response has not been read from the server.
If the buffer of the specified size is busy, it cannot write any more, while the remaining buffer can continue to read the response from the server, or write the response to a temporary file on disk.
This parameter is usually set to the sum of the single buffers in proxy_buffer_size and proxy_buffers, that is, twice the size of a single memory page.
5. proxy_max_temp_file_size
Syntax: proxy_max_temp_file_size size;
Default: proxy_max_temp_file_size 1024m;
Context: http, server, location
Copy the code
This parameter is used to set the maximum size of temporary files on the disk.
When the entire response returned by the server exceeds the memory buffer size set by proxy_buffering and proxy_buffers, the rest of the response body can be stored in a temporary file on disk. Proxy_max_temp_file_size specifies the maximum size of the temporary file.
The size of data that can be written to disk temporarily at one time is specified by the proxy_temp_FILe_write_size parameter.
Note that setting the proxy_MAX_temp_FILe_size parameter to 0 disallows saving responses to temporary files on disk.
6. proxy_temp_file_write_size
Syntax: proxy_temp_file_write_size size;
Default: proxy_temp_file_write_size 8k|16k;
Context: http, server, location
Copy the code
This parameter is used to set the size of temporary files written to disk each time.
This parameter is usually set to the sum of the single buffers in proxy_buffer_size and proxy_buffers, that is, twice the size of a single memory page.
Example of proxy buffer configuration
# open proxy_buffering
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 4 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 0;
Copy the code