Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: 开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunk一起响应 #11977

Open
GreatLazyMan opened this issue Feb 18, 2025 · 4 comments
Labels
bug Something isn't working plugin

Comments

@GreatLazyMan
Copy link

GreatLazyMan commented Feb 18, 2025

Current Behavior

开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunk一起响应
已经尝试在route和nginx关闭cache,没有效果。示例代码

Expected Behavior

期望在修改响应体后,可以流式的返回响应体

Error Logs

No response

Steps to Reproduce

1.启动apisix和etcd
2.配置文件中配置自定义插件
3.启动测试服务器,可以返回流式响应
4.配置route和upstream
插件代码示例

local plugin_name = "extract-reasoning"
local core = require("apisix.core")
local cjson = require("cjson.safe")
local ngx = ngx

local schema = {
    type = "object",
    properties = {},
}

local _M = {
    version = 1.0,
    priority = 1000,
    name = plugin_name,
    schema = schema,
}


function _M.header_filter(conf, ctx)
    -- 设置SSE响应头
    ngx.header["Content-Type"] = "text/event-stream"
    -- ngx.header["Cache-Control"] = "no-cache"
    ngx.header["Connection"] = "keep-alive"
    ngx.header["Cache-Control"] = "no-cache, no-store, must-revalidate"
    ngx.header["Pragma"] = "no-cache"
    ngx.header["Expires"] = "0"
end

function _M.body_filter(conf, ctx)
    local chunk = ngx.arg[1]
    local is_last = ngx.arg[2]

    ngx.arg[1] = chunk
end

return _M

Environment

  • APISIX version (run apisix version):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):
@dosubot dosubot bot added bug Something isn't working plugin labels Feb 18, 2025
@GreatLazyMan GreatLazyMan changed the title bug: 开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunc一起响应 bug: 开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunk一起响应 Feb 18, 2025
@bzp2010
Copy link
Contributor

bzp2010 commented Feb 22, 2025

  1. 检查网关前的任何潜在L4/L7负载均衡器,确认它们是不是具有并开启了响应体缓冲。

  2. 如果网关前没有任何负载均衡器,则尝试使用自定义NGINX配置以关闭 proxy_buffering, https://apisix.apache.org/docs/apisix/customize-nginx-configuration/


  1. Check any potential L4/L7 load balancers in front of the gateway to confirm that they do not have response body buffering enabled.

  2. If there aren't any load balancers in front of the gateway, try using a custom NGINX configuration to turn off proxy_buffering, https://apisix.apache.org/docs/apisix/customize-nginx-configuration/

@GreatLazyMan
Copy link
Author

GreatLazyMan commented Feb 23, 2025

@bzp2010 参考其他回答,增加了如下配置

nginx_config:
  http_configuration_snippet: |
    proxy_buffering off;

可以解决问题,但是这个配置是全局的,有什么其他灵活的配置的方法么?比如nginx可以在location的位置配置。我的场景希望可以在对于处理header中含有"stream": true 请求,设置配置 proxy_buffering off;,可以帮忙看下apisix有办法配置吗?非常感谢

@ymichaelson
Copy link

ymichaelson commented Feb 26, 2025

Current Behavior

开发了一个插件,修改了server返回的流式响应的请求体,apisix一次响应不是一个chunk,而是累积几个chunk一起响应 已经尝试在route和nginx关闭cache,没有效果。示例代码

Expected Behavior

期望在修改响应体后,可以流式的返回响应体

Error Logs

No response  无响应

Steps to Reproduce

1.启动apisix和etcd 2.配置文件中配置自定义插件 3.启动测试服务器,可以返回流式响应 4.配置route和upstream 插件代码示例

local plugin_name = "extract-reasoning"
local core = require("apisix.core")
local cjson = require("cjson.safe")
local ngx = ngx

local schema = {
type = "object",
properties = {},
}

local _M = {
version = 1.0,
priority = 1000,
name = plugin_name,
schema = schema,
}

function _M.header_filter(conf, ctx)
-- 设置SSE响应头
ngx.header["Content-Type"] = "text/event-stream"
-- ngx.header["Cache-Control"] = "no-cache"
ngx.header["Connection"] = "keep-alive"
ngx.header["Cache-Control"] = "no-cache, no-store, must-revalidate"
ngx.header["Pragma"] = "no-cache"
ngx.header["Expires"] = "0"
end

function _M.body_filter(conf, ctx)
local chunk = ngx.arg[1]
local is_last = ngx.arg[2]

ngx.arg[1] = chunk

end

return _M

Environment

  • APISIX version (run apisix version):APISIX 版本(运行 apisix 版本):
  • Operating system (run uname -a):作系统(运行 uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):OpenResty / Nginx 版本(运行 openresty -Vnginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):etcd 版本(如果相关)(运行 curl http://127.0.0.1:9090/v1/server_info ):
  • APISIX Dashboard version, if relevant:APISIX Dashboard 版本(如果相关):
  • Plugin runner version, for issues related to plugin runners:插件运行程序版本,用于与插件运行程序相关的问题:
  • LuaRocks version, for installation issues (run luarocks --version):LuaRocks 版本,针对安装问题(运行 luarocks --version):

@GreatLazyMan
hello,
按照这个插件测试,没有达到预期的效果,还是累积多个chunk再返回,是还需要有什么配置吗?路由上已经配置了这个plugin

@qizhendong1
Copy link
Contributor

about this issue, it maybe nginx side, look this nginx/nginx#316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin
Projects
Status: 📋 Backlog
Development

No branches or pull requests

4 participants