brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx proxy_buffering off passes a streaming response straight through

Nginx
~/tips/tip-nginx-proxy-buffering-off

proxy_buffering off;

Stops nginx from buffering a proxied response, so streamed data reaches the client as it arrives.

Live logs, event streams and long-running responses can look frozen behind nginx. The cause is often buffering, and it can be switched off for just one location.

By default proxy_buffering is on. nginx then receives the response from the backend as fast as it can and stores it in buffers, sized by proxy_buffer_size and proxy_buffers. If the whole response does not fit in memory, part of it can go to a temporary file on disk. The client gets the data from those buffers, which is fine for ordinary pages and bad for something that should arrive piece by piece.

With proxy_buffering off the response is passed to the client synchronously, immediately as it is received, and nginx does not try to read the whole response first. The amount it can take from the backend at a time is then proxy_buffer_size.

location /events/ {
    proxy_pass http://127.0.0.1:8080;
    proxy_buffering off;
}

The directive works in http, server and location, so I would put it on the one location that needs it and leave the rest of the site buffered.

The docs add a second route that I like for apps I write myself: the backend can switch buffering per response by sending an X-Accel-Buffering header with the value yes or no. That means the application decides which of its responses are streams. The behaviour can be disabled on the nginx side with proxy_ignore_headers X-Accel-Buffering, which is useful to know when a header seems to be ignored.

The caveat is the cost. Buffering exists so the backend can finish quickly and be free for the next request, even when the client is slow. Without it, I would expect a slow client to keep the backend busy for as long as it takes to receive the response, so do not turn it off globally. And if a stream still stalls, look at the read timeout as well: proxy_read_timeout defaults to 60 seconds between two reads, and nginx closes the connection when the backend sends nothing within that time.

Further reading: ngx_http_proxy_module

Going deeper: Guide: fix a 502 Bad Gateway in Nginx Proxy Manager, with a checklist that finds the cause

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link