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;
    }
相关推荐
烂不烂问厨房34 分钟前
前端自适应布局之等比例缩放
开发语言·前端·javascript
kong790692844 分钟前
环境搭建-运行前端工程
前端
CodeLinghu1 小时前
提示词链模式:一种利用LLM大语言模型处理复杂任务的强大范式
前端·人工智能·语言模型
J2虾虾1 小时前
关于Ant Design Vue
前端·javascript·vue.js
程序员笨鸟1 小时前
[特殊字符] React 高频 useEffect 导致页面崩溃的真实案例:从根因排查到彻底优化
前端·javascript·学习·react.js·面试·前端框架
Highcharts.js1 小时前
从旧版到新版:Highcharts for React 迁移全攻略 + 开发者必知的 5 大坑
前端·react.js·前端框架·编辑器·highcharts
独角鲸网络安全实验室1 小时前
高危预警!React核心组件曝CVSS 9.8漏洞,数百万开发者面临远程代码执行风险
运维·前端·react.js·网络安全·企业安全·漏洞·cve-2025-11953
西瓜凉了半个夏~1 小时前
React专题:react,redux以及react-redux常见一些面试题
前端·javascript·react.js
大模型教程.1 小时前
收藏级教程:ReAct模式详解,让大模型从回答问题到解决问题
前端·人工智能·机器学习·前端框架·大模型·产品经理·react
独角鲸网络安全实验室1 小时前
高危预警!React CVE-2025-55182 突破 RSC 防护,未授权 RCE 威胁 39% 云应用
前端·react.js·网络安全·前端框架·漏洞·rce·cve-2025-55182