-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: add proxy-buffering plugin #13348
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
Open
AlinsRan
wants to merge
12
commits into
apache:master
Choose a base branch
from
AlinsRan:feat/proxy-buffering-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
490ae6c
feat: add proxy-buffering plugin
AlinsRan df48130
docs: add proxy-buffering to config.json sidebar
AlinsRan e2a5331
test: fix proxy-buffering CI failures
copilot-cli a772573
test(proxy-buffering): use streaming endpoint and fix doc format
0e1c5e6
test(proxy-buffering): use EE integration test scripts
62b70c2
chore: add bre to codespell ignore words for cert key content
84d62c0
Potential fix for pull request finding
AlinsRan 79f9fb8
fix: address review comments and CI failures for proxy-buffering plugin
AlinsRan 4e9b968
revert: remove additionalProperties = false from proxy-buffering schema
AlinsRan 1380d22
test(proxy-buffering): move test scripts to ci directory
59264d2
test(proxy-buffering): remove meaningless schema-only .t test file
f9d2a64
test(proxy-buffering): move test scripts to t/cli directory
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ nulll | |
| smove | ||
| aks | ||
| nin | ||
| bre | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| -- | ||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||
| -- contributor license agreements. See the NOTICE file distributed with | ||
| -- this work for additional information regarding copyright ownership. | ||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| -- (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- | ||
| local require = require | ||
| local ngx = ngx | ||
| local core = require("apisix.core") | ||
|
|
||
|
|
||
| local schema = { | ||
| type = "object", | ||
| properties = { | ||
| disable_proxy_buffering = { | ||
| type = "boolean", | ||
| default = false, | ||
| }, | ||
| }, | ||
| } | ||
|
AlinsRan marked this conversation as resolved.
|
||
|
|
||
|
|
||
| local plugin_name = "proxy-buffering" | ||
| local _M = { | ||
| version = 0.1, | ||
| priority = 21991, | ||
| name = plugin_name, | ||
| schema = schema, | ||
| } | ||
|
|
||
|
|
||
| function _M.check_schema(conf) | ||
| return core.schema.check(schema, conf) | ||
| end | ||
|
|
||
|
|
||
| -- we want to control proxy behavior before auth, so put the code under rewrite method | ||
| function _M.rewrite(conf, ctx) | ||
| ngx.ctx.disable_proxy_buffering = conf.disable_proxy_buffering | ||
| end | ||
|
|
||
|
|
||
| return _M | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| title: proxy-buffering | ||
| keywords: | ||
| - Apache APISIX | ||
| - API Gateway | ||
| - Proxy Buffering | ||
| description: The proxy-buffering Plugin disables nginx proxy buffering per route to enable streaming responses such as Server-Sent Events (SSE). | ||
| --- | ||
|
|
||
| <!-- | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| --> | ||
|
|
||
| <head> | ||
| <link rel="canonical" href="https://docs.api7.ai/hub/proxy-buffering" /> | ||
| </head> | ||
|
|
||
| ## Description | ||
|
|
||
| The `proxy-buffering` Plugin disables nginx proxy buffering for the configured route. When proxy buffering is disabled, nginx streams the upstream response directly to the client without accumulating it in memory or on disk first. | ||
|
|
||
| This is particularly useful for: | ||
|
|
||
| - **Server-Sent Events (SSE)**: Clients must receive events in real time; buffering would delay or break the stream. | ||
| - **Streaming APIs**: Large or indefinite response bodies must flow continuously without waiting for the full body. | ||
| - **Real-time data delivery**: Any use case requiring low-latency delivery of partial responses. | ||
|
|
||
| ## Attributes | ||
|
|
||
| | Name | Type | Required | Default | Description | | ||
| | ------------------------- | ------- | -------- | ------- | --------------------------------------------------------------------------------------------- | | ||
| | disable_proxy_buffering | boolean | False | false | When set to `true`, disables [`proxy_buffering`](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering) for this route, enabling streaming responses. | | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Examples | ||
|
|
||
| The examples below demonstrate how you can configure the `proxy-buffering` Plugin for different scenarios. | ||
|
|
||
| :::note | ||
| You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: | ||
|
|
||
| ```bash | ||
| admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g') | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
| ### Disable Proxy Buffering for Streaming Responses | ||
|
|
||
| The following example disables proxy buffering for a route that serves Server-Sent Events (SSE): | ||
|
|
||
| ```shell | ||
| curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \ | ||
| -H "X-API-KEY: $admin_key" -X PUT -d ' | ||
| { | ||
| "uri": "/sse", | ||
| "plugins": { | ||
| "proxy-buffering": { | ||
| "disable_proxy_buffering": true | ||
| } | ||
| }, | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| } | ||
| } | ||
| }' | ||
| ``` | ||
|
|
||
| Send a request to the route: | ||
|
|
||
| ```shell | ||
| curl -i -N -H "Accept: text/event-stream" http://127.0.0.1:9080/sse | ||
| ``` | ||
|
|
||
| Because `disable_proxy_buffering` is `true`, nginx streams each SSE event from the upstream to the client as it arrives, without buffering. | ||
|
|
||
| ## Delete Plugin | ||
|
|
||
| To remove the `proxy-buffering` Plugin, delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect. | ||
|
|
||
| ```shell | ||
| curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \ | ||
| -H "X-API-KEY: $admin_key" -X PUT -d ' | ||
| { | ||
| "uri": "/sse", | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| } | ||
| } | ||
| }' | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| --- | ||
| title: proxy-buffering | ||
| keywords: | ||
| - APISIX | ||
| - API 网关 | ||
| - Proxy Buffering | ||
| description: 本文介绍了 Apache APISIX proxy-buffering 插件的相关操作,你可以使用此插件按路由禁用 nginx 代理缓冲,这对于流式响应(如 Server-Sent Events)至关重要。 | ||
| --- | ||
|
|
||
| <!-- | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| --> | ||
|
|
||
| <head> | ||
| <link rel="canonical" href="https://docs.api7.ai/hub/proxy-buffering" /> | ||
| </head> | ||
|
|
||
| ## 描述 | ||
|
|
||
| `proxy-buffering` 插件用于按路由控制 nginx 代理缓冲行为。禁用代理缓冲后,nginx 会将上游响应直接流式传输给客户端,而不会在内存或磁盘中积累完整响应体。该功能对以下场景至关重要: | ||
|
|
||
| - **Server-Sent Events(SSE)**:客户端需要实时接收事件,缓冲会延迟或中断数据流。 | ||
| - **流式 API**:响应体较大或无限长,需要持续流式传输而无需等待完整响应。 | ||
| - **实时数据推送**:任何需要低延迟传输部分响应的场景。 | ||
|
|
||
| 该插件工作在 `rewrite` 阶段,优先级为 **21991**,早于鉴权插件执行,可以影响 APISIX 流水线中代理 location 的选择。 | ||
|
|
||
| ## 属性 | ||
|
|
||
| | 名称 | 类型 | 必选项 | 默认值 | 描述 | | ||
| | ----------------------- | ------- | ------ | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | disable_proxy_buffering | boolean | 否 | false | 设置为 `true` 时,将为该路由禁用 [`proxy_buffering`](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering),从而支持流式响应。 | | ||
|
|
||
| ## 启用插件 | ||
|
|
||
| 以下示例展示了如何在指定路由上启用 `proxy-buffering` 插件以支持流式响应: | ||
|
|
||
| :::note | ||
|
|
||
| 您可以这样从 `config.yaml` 中获取 `admin_key` 并存入环境变量: | ||
|
|
||
| ```bash | ||
| admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g') | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
| ```shell | ||
| curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \ | ||
| -H "X-API-KEY: $admin_key" -X PUT -d ' | ||
| { | ||
| "uri": "/sse", | ||
| "plugins": { | ||
| "proxy-buffering": { | ||
| "disable_proxy_buffering": true | ||
| } | ||
| }, | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| } | ||
| } | ||
| }' | ||
| ``` | ||
|
|
||
| ## 测试插件 | ||
|
|
||
| 启用插件后,向该路由发送请求: | ||
|
|
||
| ```shell | ||
| curl -i http://127.0.0.1:9080/sse | ||
| ``` | ||
|
|
||
| 由于 `disable_proxy_buffering` 设置为 `true`,nginx 会将响应直接流式传输给客户端,对调用方透明,但消除了 nginx 引入的缓冲延迟。 | ||
|
|
||
| 要验证配置是否已正确保存: | ||
|
|
||
| ```shell | ||
| curl http://127.0.0.1:9180/apisix/admin/routes/1 \ | ||
| -H "X-API-KEY: $admin_key" | ||
| ``` | ||
|
|
||
| 响应中将包含路由对象中的 `proxy-buffering` 插件配置。 | ||
|
|
||
| ## 删除插件 | ||
|
|
||
| 当你需要删除该插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务: | ||
|
|
||
| ```shell | ||
| curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \ | ||
| -H "X-API-KEY: $admin_key" -X PUT -d ' | ||
| { | ||
| "uri": "/sse", | ||
| "upstream": { | ||
| "type": "roundrobin", | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| } | ||
| } | ||
| }' | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.