In a high-performance web service architecture, Nginx plays a crucial role as a reverse proxy server. Among them, Nginx's streaming response function is a very practical feature, which allows Nginx to gradually send content to the client while receiving the response of the backend server. This "receive and send" method not only improves the user experience, but also optimizes network transmission efficiency, which is especially suitable for processing larger response content.
Default behavior and problem
By default, Nginx will first receive and cache the complete response content before sending it to the client at once. Although this approach is simple, it can cause problems in some scenarios:
- High delay: For large files or long-term generated content, the client needs to wait a long time before starting to receive data.
- Request failure risk: If the response content is too large, the request may fail due to insufficient memory or other reasons.
To solve these problems, Nginx provides configuration options for streaming response.
Streaming response configuration
To implement streaming response of Nginx, you need to set it accordingly in the Nginx configuration file. Here is a typical configuration example:
location /streaming { proxy_pass http://backend_server; proxy_cache off; # Close cache proxy_buffering off; # Turn off proxy buffering chunked_transfer_encoding on; # Enable block transmission encoding tcp_nopush on; # Enable TCP NOPUSH option and disable Nagle algorithm tcp_nodelay on; # Enable TCP NODELAY option and disable the delay ACK algorithm keepalive_timeout 300; # Set keep-alive timeout to 300 seconds}
Configuration details
-
proxy_cache off;
:- Turn off the cache function to prevent the proxy server from cacheing streaming response content, and ensure that the client can receive real-time and complete responses.
-
proxy_buffering off;
:- Turn off the proxy server's response buffering to prevent it from buffering the entire response and then sending it to the client, thus achieving a true streaming effect.
-
chunked_transfer_encoding on;
:- Turn on block transfer encoding, allowing the response to be divided into multiple blocks for transmission. This is the key to streaming.
-
tcp_nopush on;
:- Turn on the TCP NOPUSH option and disable the Nagle algorithm. This can prevent the merger of small pieces of data and ensure that the data can be sent to the client in real time.
-
tcp_nodelay on;
:- Turn on the TCP NODELAY option and disable the delay ACK algorithm. This helps reduce the latency of ACK packets and ensures that data is sent in time.
-
keepalive_timeout 300;
:- Increase keepalive timeout to prevent the connection between the proxy and the source server from being closed when the streaming response is not completed. This is set to 300 seconds and can be adjusted according to actual needs.
Configuration effect verification
In order to verify the effect of the above configuration, we can set up a simple backend service that gradually generates and returns data to simulate the scenario of streaming response.
Assuming the backend service is a simple Python Flask application:
from flask import Flask, Response import time app = Flask(__name__) @('/stream') def stream(): def generate(): for i in range(10): yield f"Part {i}\n" (1) # Simulation time-consuming operation return Response(generate(), mimetype='text/plain') if __name__ == '__main__': (host='0.0.0.0', port=5000)
Deploy the above Flask application on the backend server and ensure that Nginx can proxy to the service.
Then, access the URL of the Nginx proxy on the client (such as a browser or using the curl command):
curl http://nginx_server/streaming/stream
Running results:
Part 0
Part 1
(Output part per second, total 10 parts)
...
Part 9
It can be seen that the client can gradually receive the data generated by the backend server, rather than waiting for all the data to be generated and received again. This proves that Nginx's streaming response configuration has taken effect.
Summarize
By turning off cache and proxy buffering, turning on block encoding, disabling Nagle and delay ACK algorithms, and increasing keepalive timeout, we can realize streaming response transmission between Nginx proxy server and client. This configuration not only improves the response efficiency, but also optimizes the user experience, especially suitable for scenarios where large files need to be processed or content generated for a long time.
This is the end of this article about Nginx's streaming response configuration summary. For more information about Nginx streaming response configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!