From aaf8cacf75fb578764be85d5b13b1040a05980e3 Mon Sep 17 00:00:00 2001 From: vakabunga Date: Sat, 14 Mar 2026 17:30:52 +0300 Subject: [PATCH] fix: eliminate SSE buffering through Nginx proxy Add 2 KB padding comment after headers to push past proxy buffer threshold, enable TCP_NODELAY on the socket, and remove erroneous chunked_transfer_encoding off from Nginx that caused full response buffering. --- backend/src/routes/import.ts | 11 +++++++++++ frontend/nginx.conf | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/import.ts b/backend/src/routes/import.ts index 0823f07..ac46011 100644 --- a/backend/src/routes/import.ts +++ b/backend/src/routes/import.ts @@ -32,6 +32,15 @@ function sseWrite(res: import('express').Response, data: Record res.write(`data: ${JSON.stringify(data)}\n\n`); } +/** + * Send a 2 KB comment block to push past any proxy buffering threshold. + * Nginx and other reverse proxies often buffer the first few KB before + * starting to stream — this padding forces the initial flush. + */ +function ssePadding(res: import('express').Response) { + res.write(`: ${' '.repeat(2048)}\n\n`); +} + const router = Router(); router.post( @@ -60,7 +69,9 @@ router.post( res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); res.setHeader('X-Accel-Buffering', 'no'); + res.socket?.setNoDelay(true); res.flushHeaders(); + ssePadding(res); try { const converted = await convertPdfToStatementStreaming( diff --git a/frontend/nginx.conf b/frontend/nginx.conf index 6e4284a..eaae9de 100644 --- a/frontend/nginx.conf +++ b/frontend/nginx.conf @@ -15,7 +15,6 @@ server { proxy_connect_timeout 5s; proxy_read_timeout 600s; proxy_buffering off; - chunked_transfer_encoding off; client_max_body_size 15m; }