proxy-buffering
proxy-buffering 插件动态禁用 NGINX proxy_buffering 指令。
在配置 API7 网关使用 Server-Sent Events (SSE) 上游服务,或响应分块数据(如 etcd 监视事件)的服务时,应禁用缓冲。
示例
配置 SSE 上游
以下示例演示了如何在具有 SSE 上游服务的路由上禁用 proxy_buffering。
为 SSE 启动一个示例上游服务:
- Docker
- Kubernetes
docker run -d -p 8080:8080 jmalloc/echo-server
为 SSE 服务器部署创建 Kubernetes 清单文件:
sse-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: sse-server
spec:
replicas: 1
selector:
matchLabels:
app: sse-server
template:
metadata:
labels:
app: sse-server
spec:
containers:
- name: echo-server
image: jmalloc/echo-server
ports:
- containerPort: 8080
为 SSE 服务再创建一个 Kubernetes 清单文件:
sse-service.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: sse-service
spec:
selector:
app: sse-server
ports:
- protocol: TCP
port: 8080
targetPort: 8080
type: ClusterIP
应用清单:
kubectl apply -f sse-deployment.yaml -f sse-service.yaml
创建一个指向上游的路由并配置 proxy-buffering:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "proxy-buffering-route",
"uri": "/.sse",
"plugins": {
"proxy-buffering": {
"disable_proxy_buffering": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}'
adc.yaml
services:
- name: sse-service
routes:
- uris:
- /.sse
name: proxy-buffering-route
plugins:
proxy-buffering:
disable_proxy_buffering: true
upstream:
type: roundrobin
nodes:
- host: 127.0.0.1
port: 8080
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
proxy-buffering-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: proxy-buffering-plugin-config
spec:
plugins:
- name: proxy-buffering
config:
disable_proxy_buffering: true
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: proxy-buffering-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /.sse
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: proxy-buffering-plugin-config
backendRefs:
- name: sse-service
port: 8080
proxy-buffering-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: proxy-buffering-route
spec:
ingressClassName: apisix
http:
- name: proxy-buffering-route
match:
paths:
- /.sse
upstreams:
- serviceName: sse-service
servicePort: 8080
plugins:
- name: proxy-buffering
config:
disable_proxy_buffering: true
应用配置:
kubectl apply -f proxy-buffering-ic.yaml
向路由发送请求:
curl "http://127.0.0.1:9080/.sse" -H "Accept: text/event-stream"
你应该会收到 HTTP/1.1 200 OK 响应,并看到类似于以下的连续事件流:
event: server
data: 162291b28f55
id: 1
event: request
data: GET /.sse HTTP/1.1
data:
data: Host: 127.0.0.1:9080
data: Accept: text/event-stream
data: User-Agent: curl/7.74.0
data: X-Forwarded-For: 172.19.0.1
data: X-Forwarded-Host: 127.0.0.1
data: X-Forwarded-Port: 9080
data: X-Forwarded-Proto: http
data: X-Real-Ip: 172.19.0.1
data:
id: 2
event: time
data: 2023-10-19T02:13:53Z
id: 3
event: time
data: 2023-10-19T02:13:54Z
id: 4
event: time
data: 2023-10-19T02:13:55Z
id: 5
...