Nginx's streaming response configuration
Nginx's streaming response refers to the process of receiving the response content to the client while gradually sending it to the client when Nginx is a reverse proxy server.
This response method can improve user experience and network transmission efficiency, and is often used to process larger response content.
By default, Nginx handles requests by cached response content.
That is, Nginx will only send it to the client after receiving the full response.
However, in some scenarios, this approach may lead to high latency or request failure.
Therefore, Nginx provides the function of streaming response, allowing the reverse proxy server to gradually send the response content to the client while receiving the response.
This method can reduce the waiting time while reducing the load pressure of network transmission.
Implement Nginx's streaming response
You need to add corresponding instructions to the Nginx configuration file, such as:
That is, typewriter effect,
# No cache, support streaming output proxy_cache off; # Close cache proxy_buffering off; # Turn off proxy buffering chunked_transfer_encoding on; # Enable block transfer encoding tcp_nopush on; # Enable TCP NOPUSH option and disable Nagle algorithm tcp_nodelay on; # Enable TCP NODELAY option and disable delay ACK algorithm keepalive_timeout 300; # set upkeep-aliveThe timeout is65Second
This nginx configuration is to solve the problem of streaming response.
Implemented through the following configurations
proxy_cache off; # Close cache
- Turn off the cache to prevent the proxy server from cacheing streaming response content, resulting in the client not receiving the complete response.
proxy_buffering off; # Turn off proxy buffering
- Turn off the buffering of the proxy server for the response to prevent it from buffering the entire response and then sending it to the client, making it impossible to achieve streaming effect.
chunked_transfer_encoding on; # Turn on block transmission encoding
- Turn on block transmission encoding, allowing the response to be divided into multiple blocks for transmission, realizing streaming.
tcp_nopush on; # OpenTCP NOPUSHOptions,prohibitNaglealgorithm
- Turn on the TCP NOPUSH option, disable the Nagle algorithm, prevent the merger of small pieces of data, and send it to the client in real time.
tcp_nodelay on; # OpenTCP NODELAYOptions,Prohibit delaysACKalgorithm
- Turn on the TCP NODELAY option, disable the delay ACK algorithm, prevent the delay of ACK packets, and send them to the client in time.
keepalive_timeout 300; # set upkeep-aliveThe timeout is65Second
- Increase keepalive timeout to prevent the connection between the proxy and the source server from being closed when the streaming response is not completed.
- Therefore, by turning off cache and proxy buffering, turning on block encoding, disabling Nagle and delay ACK algorithms, and increasing keepalive timeout time, streaming response transmission between the proxy server and the client can be realized.
- Using this configuration, nginx proxy can pass the streaming response of the source server to the client without causing the response content to be truncated or delayed, achieving a true streaming experience.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.