nginx 反向代理后SSE连接无效的问题

增加配置:

nginx 复制代码
proxy_buffering off;

在使用 Nginx 作为反向代理时,如果未配置 proxy_buffering off;,可能会导致 Server-Sent Events (SSE) 连接没有响应。这是因为 Nginx 默认启用了缓冲机制,这会导致 SSE 事件被缓冲,而不是立即传递给客户端。以下是详细解释和解决方案:

问题分析

  1. Nginx 缓冲机制

    • 默认行为:Nginx 默认会缓冲代理请求和响应数据。这意味着 SSE 事件会被缓存,直到缓冲区满或达到一定时间间隔,然后一次性发送给客户端。
    • 影响 SSE:SSE 依赖于实时推送事件,如果事件被缓冲,客户端将无法及时收到这些事件,导致连接看起来没有响应。
  2. proxy_buffering 指令

    • 作用:控制 Nginx 是否对代理请求和响应进行缓冲。
    • 默认值on(开启缓冲)。
nginx 复制代码
    location /api/ {
        proxy_pass http://api.com/;

        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PATCH, PUT' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Origin' '$http_origin' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With' always;
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PATCH, PUT' always;
            add_header 'Access-Control-Max-Age' 1728000;

            add_header 'Content-Type' 'application/json;charset=UTF-8';
            add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With' always;
            add_header 'Content-Length' 0;
            return 204;
        }
        proxy_redirect default;
        proxy_cookie_path ~*^/.* /;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 8192m;

        proxy_buffering off;
        # proxy_cache off;
        # keepalive_timeout 3600s;
        # proxy_connect_timeout 18000;
        # proxy_send_timeout 18000;
        # proxy_read_timeout 18000;
    }

    location /im-ws {
        proxy_pass http://api.com;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 3600s;
    }
相关推荐
英俊潇洒美少年1 天前
Vue 生产环境打包:SourceMap、压缩、混淆、加密全解 + 最佳实践
前端·javascript·vue.js
巴博尔1 天前
UNIAPP中NVUE页面 动画
android·前端·javascript·ios·uni-app
她说人狗殊途1 天前
基于 vue-cli 创建
前端·javascript·vue.js
AZaLEan__1 天前
前端移动端适配与 Bootstrap
前端·bootstrap·html
大家的林语冰1 天前
Deno 2.8 正式发布,再次超越 Bun,史上最大的次版本升级诞生!
前端·javascript·node.js
渣渣xiong1 天前
从零开始:前端转型AI agent直到就业第五十七天-第五十八天
前端·人工智能·python
AI周红伟1 天前
周红伟:长鑫科技(CXMT)财务全景分析
前端·chrome·科技
excel1 天前
JS 正则在多次 test() 时为什么会出现 lastIndex 缓存问题?
前端
IT_陈寒1 天前
为什么 Java 的 Optional 让我调试到深夜?
前端·人工智能·后端
米丘1 天前
React 19.x 的 lazy 与 Suspense
前端·javascript·react.js