detfalskested

Server-Sent Events from behind nginx

I was having weird issues with the timing of delivery of Server-Sent Events in a production Django application. It was set up to burst out a few events at the same time. For some reason, the last one did not show up with the rest of the burst. Instead, it showed up as the first item in the next burst, which would again not include the last expected item.

I first suspected async to be the culprit, as I am only getting my feet wet in that aspect. But after a bit of debugging (and acquitting async), it turned out to be nginx doing some buffering of responses. Something I, of course, did not experience during local development, where nginx is not part of the setup.

Fortunately, nginx allow you to turn of proxy buffering. Either completely for your site, via proxy_buffering off. Or by adding a custom header to the specific response in the application behind nginx, X-Accel-Buffering: no.

A further explanation can be found in the nginx documentation.