Your application streams events correctly in development, but after deploying it behind Nginx, the frontend receives several events together instead of receiving them as they are generated. Sometimes, nothing appears until the connection closes.
This is a common problem with Server-Sent Events (SSE). The application may be producing events correctly, but an intermediate proxy is buffering the response before forwarding it to the client.
The most common fix is to disable response buffering for the SSE endpoint in Nginx. However, the server's response format, connection timeouts, compression, and other proxy layers can also affect streaming.
Why SSE Stops Streaming Behind Nginx
Server-Sent Events allow a server to send updates to a client over a long-lived HTTP connection. They are commonly used for AI-generated text, live notifications, progress updates, application logs, and real-time status changes.
Unlike a regular API response, an SSE connection is expected to deliver events incrementally while the connection remains open.
Nginx enables response buffering by default for proxied responses. It can read data from the upstream application into buffers before forwarding it to the client. With buffering enabled, small SSE events may be held temporarily and delivered in batches rather than immediately. Nginx documents this behavior in its proxy module documentation.
The result is misleading: your backend appears to be streaming, but the user sees delayed updates.
1. Disable Buffering for the SSE Endpoint
The first thing to check is the Nginx location that handles your SSE route.
If the application exposes an endpoint such as /api/events, configure that location to disable proxy buffering:
location /api/events {
proxy_pass http://127.0.0.1:8000;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
}
This configuration assumes the endpoint is served by an HTTP upstream on port 8000. Adapt the upstream address and location path to your deployment.
The important directive is proxy_buffering off. It tells Nginx to pass the upstream response to the client as it receives the data, rather than accumulating the response in its proxy buffers.
proxy_cache off is useful when proxy caching is configured and you want the stream to bypass it. The read timeout allows longer periods between successive reads from the upstream, but the correct value depends on your application's heartbeat interval and infrastructure.
After editing the configuration, validate and reload Nginx:
nginx -t
nginx -s reload
Remark: Apply buffering changes to the actual SSE route, not necessarily every endpoint on your application. Ordinary JSON responses generally do not need the same streaming configuration.
2. Set the Correct SSE Response Headers
Your backend must also return a valid SSE response. Disabling Nginx buffering cannot turn an incorrectly formatted response into a working event stream.
At minimum, an SSE endpoint should return:
Content-Type: text/event-streamto identify the response as an event stream.Cache-Control: no-cacheto discourage caching of the stream.X-Accel-Buffering: noto tell Nginx not to buffer the response, provided Nginx has not been configured to ignore this header.
The last header is particularly useful when you cannot modify the Nginx configuration yourself. Nginx normally honors X-Accel-Buffering, although administrators can disable this behavior with proxy_ignore_headers. See the Nginx proxy header documentation.
Your application must also emit valid SSE events. Each event is a block of text terminated by a blank line.
For example:
data: First update
data: Second update
Each blank line terminates an event. If the application generates an event but never completes the event block, the browser may not dispatch it as expected.
Remark: Flushing data from your application and disabling proxy buffering are related but separate concerns. Both the application and the delivery path must allow incremental output.
3. Keep the Connection Alive
An SSE connection can remain open for minutes or hours. If no data arrives for longer than an intermediary's timeout, the connection may close even when response buffering is disabled.
Nginx's proxy_read_timeout controls the time allowed between successive reads from the upstream server, not the total lifetime of the response.
For streams that can remain idle, send periodic heartbeat comments:
: keep-alive
SSE comments are ignored by the browser's event handler, but they still produce traffic that can help keep an otherwise idle connection alive.
Choose a heartbeat interval shorter than the shortest relevant idle timeout across the infrastructure. Also ensure that the application handles client disconnections and releases resources associated with completed or abandoned streams.
Remark: Increasing the timeout alone does not fix buffering. Use heartbeats to maintain idle connections and disable buffering to deliver events incrementally.
4. Check Compression and Other Proxy Layers
If proxy_buffering off is already configured but events still arrive in batches, inspect the rest of the response path.
Compression can introduce additional buffering or delay, depending on the compressor and how data is flushed. Test the SSE route with compression disabled before deciding whether compression is responsible.
Also check whether the request passes through a CDN, ingress controller, load balancer, or another reverse proxy. Disabling buffering at one Nginx instance does not prevent a later proxy from buffering the response.
If Nginx forwards requests using FastCGI or uWSGI rather than proxy_pass, check the corresponding buffering directives, such as fastcgi_buffering or uwsgi_buffering, instead of assuming proxy_buffering controls that path.
A useful debugging approach is to compare the stream at each boundary: first the backend directly, then through Nginx, and finally through the public domain. The first point where events become delayed usually identifies the layer to investigate.
5. Test Whether the Stream Is Actually Incremental
Use curl with the -N option to disable curl's own output buffering:
curl -N -i https://example.com/api/events
Observe whether events appear as they are produced.
If the stream works directly against the backend but stalls through the public URL, investigate Nginx and any intermediate proxies. If it also stalls when connecting directly to the backend, inspect the application's event generation, flushing behavior, and SSE formatting first.
For browser-based clients, confirm that the response has the text/event-stream content type and that the frontend handles incoming events as they arrive.
This simple comparison is more useful than changing several Nginx settings at once.
Common Mistakes to Avoid
Disabling buffering globally. Apply the change to the SSE location unless the broader application genuinely requires streaming behavior.
Assuming the backend is responsible for every delay. A backend can produce events correctly while a proxy holds them.
Increasing timeouts without sending heartbeats. Long timeouts do not guarantee that idle connections will survive every network device.
Ignoring event formatting. SSE events require the correct media type and properly terminated event blocks.
Ignoring compression or additional proxies. The complete network path determines when data reaches the browser.
Conclusion
When Server-Sent Events work in development but stop streaming through Nginx, response buffering is the first thing to investigate.
Start by setting proxy_buffering off on the SSE endpoint, returning the correct event-stream headers, and keeping idle connections alive with heartbeats. If updates still arrive in batches, compare the backend's direct response with the proxied response and inspect compression, ingress controllers, and other intermediaries.
The key principle is simple: producing an event is not enough; every layer between your application and the client must allow that event to be delivered incrementally.
Frequently Asked Questions
Why does SSE work locally but not behind Nginx?
The local development server may forward events directly, while Nginx buffers the proxied response by default. Disabling buffering for the SSE location often resolves the difference.
Does X-Accel-Buffering: no replace proxy_buffering off?
It can serve as an application-level way to disable Nginx response buffering when Nginx honors the header. Explicitly setting proxy_buffering off in the correct location is also a clear operational solution.
Why do SSE events arrive in batches?
Buffering in Nginx or another proxy, compression, or delayed flushing by the application can prevent individual events from reaching the browser immediately. Compare the response at each layer to identify where the delay begins.
Do I need to increase proxy_read_timeout?
Only when your stream may remain idle longer than the current timeout. A longer timeout does not disable buffering, and periodic heartbeats can help keep idle connections active.



