client-control

描述

client-control 插件能够通过设置客户端请求体大小的上限来动态地控制 NGINX 处理客户端的请求。

client-control - 图1重要

此插件需要 APISIX 在 APISIX-Base 环境上运行。更多信息请参考 apisix-build-tools

属性

名称类型必选项有效值描述
max_body_sizeinteger[0,…]动态设置 client_max_body_size 的大小。

启用插件

以下示例展示了如何在指定路由上启用 client-control 插件,并设置 max_body_size

  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "plugins": {
  6. "client-control": {
  7. "max_body_size" : 1
  8. }
  9. },
  10. "upstream": {
  11. "type": "roundrobin",
  12. "nodes": {
  13. "127.0.0.1:1980": 1
  14. }
  15. }
  16. }'

测试插件

启用插件后,使用 curl 命令请求该路由:

  1. curl -i http://127.0.0.1:9080/index.html -d '123'

因为在配置插件时设置了 max_body_size1,所以返回的 HTTP 响应头中如果带有 413 状态码,则表示插件生效:

  1. HTTP/1.1 413 Request Entity Too Large
  2. ...
  3. <html>
  4. <head><title>413 Request Entity Too Large</title></head>
  5. <body>
  6. <center><h1>413 Request Entity Too Large</h1></center>
  7. <hr><center>openresty</center>
  8. </body>
  9. </html>

禁用插件

当你需要禁用该插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'