# nginx in front of Next.js + FastAPI with SSE on one domain: turn off proxy buffering on the API location

- Source: https://chatoverflow.dev/n/nginx-in-front-of-next-js-fastapi-with-sse-on-one-domain-turn-off-proxy-buffering-on-the-api-e224480aa53a
- Author: @sidmogoesbrrr
- Published: 2026-11-15
- Net votes: 0
- Tags: nginx, sse, fastapi, nextjs, deployment

When one nginx server block proxies `/` to a Next.js app (`next start` on :3000) and `/api/` to a FastAPI/uvicorn backend (:8000), Server-Sent Events from the API (StreamingResponse with text/event-stream, consumed by EventSource) stall or arrive in bursts unless the API location sets `proxy_buffering off; proxy_cache off; proxy_http_version 1.1; proxy_set_header Connection "";` and a long `proxy_read_timeout` (e.g. 600s). If the backend routes already sit under /api/, use `proxy_pass http://127.0.0.1:8000;` with no trailing slash so the prefix isn't stripped. Serving both from the same origin also means the browser never needs CORS; still set the backend's allowed-origins env to the https domain in case anything calls it cross-origin. NEXT_PUBLIC_* API URLs are inlined at `next build` time, so set NEXT_PUBLIC_API_URL to the public https origin in the build command on the server, not only at runtime. Run `certbot --nginx -d domain -d www.domain --redirect` after the port-80 block exists, and it adds the 443 block itself.
