What Causes SSE Stream Buffering Behind Nginx
By default, Nginx enables response buffering (proxy_buffering on) so it can collect upstream data into memory or temporary disk buffers and release it in larger blocks. That improves throughput for standard HTTP responses, but it breaks real-time Server-Sent Events (SSE) by holding small data: ...\n\n chunks until the buffer fills or the stream closes:
- Default
proxy_buffering on: Nginx reads chunks from your Node.js/Python upstream and holds them instead of flushing each event immediately to the client. - Gzip / response compression: If gzip compression is enabled on
text/event-stream(in Nginx or app middleware), the compressor waits to accumulate enough bytes before emitting a compressed block. - HTTP/1.1 keep-alive & proxy settings: Without
proxy_http_version 1.1andConnection "", or ifproxy_cacheis active, long-lived streams can stall or behave unpredictably. - Idle read timeouts: If an LLM pauses longer than
proxy_read_timeout(60s by default), Nginx can time out and close the stream.
Headers and Directives That Disable Buffering
X-Accel-Buffering: no(response header from your app): Tells Nginx to disable response buffering for that specific response without turning offproxy_bufferingglobally across your entire server.Cache-Control: no-cache(orno-cache, no-transform): Prevents browsers and intermediate caches/proxies from caching or transforming the event stream.Content-Type: text/event-stream: Identifies the response as an SSE stream.- Nginx
proxy_buffering off;(andproxy_cache off;): Disables buffering and caching at the Nginx location level as a server-side guarantee.
Nginx + Node.js SSE Example
location /api/stream {
proxy_pass [http://127.0.0.1:3000](http://127.0.0.1:3000);
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host \$host;
# Disable buffering and caching for streaming responses
proxy_buffering off;
proxy_cache off;
# Keep long LLM streams alive (adjust to match max stream duration)
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
import http from "node:http";
const server = http.createServer((req, res) => {
if (req.url !== "/api/stream") {
res.writeHead(404);
res.end("Not found");
return;
}
// Key headers to prevent proxy/browser buffering and caching
res.writeHead(200, {
"Content-Type": "text/event-stream; charset=utf-8",
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
});
let count = 0;
const interval = setInterval(() => {
count += 1;
res.write(`data: token-\${count}\n\n`);
if (count >= 5) {
clearInterval(interval);
res.write("event: done\ndata: [DONE]\n\n");
res.end();
}
}, 400);
req.on("close", () => {
clearInterval(interval);
res.end();
});
});
server.listen(3000);
Helpful Tips
- Check
proxy_ignore_headers: IfX-Accel-Buffering: noseems ignored, verify your Nginx config does not haveproxy_ignore_headers X-Accel-Buffering;. - Test with
curl -N: Runcurl -N https://your-domain/api/streamfrom a terminal—-N(--no-buffer) lets you see whether chunks arrive incrementally or all at once at the end. - Send periodic SSE comments: Emitting
: keepalive\n\nevery 15–30 seconds prevents upstream/proxy idle timeouts during slow model generation.
You must log in or register to comment.
