Client Control
client-control 插件可为路由或服务设置请求体大小限制。它允许单个 API 使用不同于网关全局 NGINX 配置的限制,并在超限请求到达上游服务前拒绝请求。
请求处理与产品兼容性
请求声明了 Content-Length 时,如果声明的大小超过配置的非零限制,插件会立即以 413 Request Entity Too Large 拒绝请求。对于长度未知的请求,网关会在 NGINX 读取请求体时继续执行该限制。
该插件可用于 APISIX 和 API7 Enterprise。当前维护版本对 gRPC 上游的处理存在以下差异:
| 产品 | HTTP 请求 | 发往 gRPC 上游的请求 |
|---|---|---|
| APISIX 3.19.0 | 执行配置的限制。 | 执行初始的声明长度检查,但在内部重定向后不会重新应用运行时限制。在此版本中,不要依赖该插件控制 gRPC 请求体大小。 |
| APISIX 3.20.0 | 执行配置的限制。 | 在内部 gRPC 重定向后重新应用限制,包括请求未声明 Content-Length 的情况。 |
| API7 Enterprise 3.9.23 和 3.10.10 | 执行配置的限制。 | 执行初始的声明长度检查,但在内部重定向后不会重新应用运行时限制。在这些版本中,不要依赖该插件控制 gRPC 请求体大小。 |
示例
在路由上设置 1 KiB 限制,然后发送小于和大于限制的请求,验证网关会拒绝超限请求体。
限制请求体大小
创建启用 client-control 插件的路由:
- 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": "client-control-route",
"uri": "/anything",
"plugins": {
"client-control": {
"max_body_size": 1024
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
adc.yaml
services:
- name: client-control-service
labels:
docs-example: client-control
routes:
- name: client-control-route
uris:
- /anything
plugins:
client-control:
max_body_size: 1024
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更,并确认它们只影响本指南的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=client-control
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=client-control
- Gateway API
- APISIX CRD
client-control-ic.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: client-control-plugin-config
spec:
plugins:
- name: client-control
config:
max_body_size: 1024
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: client-control-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: client-control-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
client-control-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: client-control-route
spec:
ingressClassName: apisix
http:
- name: client-control-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: client-control
enable: true
config:
max_body_size: 1024
将配置应用到集群:
kubectl apply -f client-control-ic.yaml
发送一个未超过限制的小请求:
curl -i "http://127.0.0.1:9080/anything" \
-H "Content-Type: application/json" \
-d '{"message":"ok"}'
请求应到达上游,并返回 HTTP 2xx 响应。
发送 2 KiB 的请求体:
head -c 2048 /dev/zero | \
curl -i "http://127.0.0.1:9080/anything" \
-H "Content-Type: application/octet-stream" \
--data-binary @-
网关应以 413 Request Entity Too Large 拒绝该请求。