diff --git a/apisix/plugins/limit-conn.lua b/apisix/plugins/limit-conn.lua index dbffbabb8277..6ca46d5d1df7 100644 --- a/apisix/plugins/limit-conn.lua +++ b/apisix/plugins/limit-conn.lua @@ -30,9 +30,9 @@ local schema = { enum = {"remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"}, }, - rejected_code = {type = "integer", minimum = 200}, + rejected_code = {type = "integer", minimum = 200, default = 503}, }, - required = {"conn", "burst", "default_conn_delay", "key", "rejected_code"} + required = {"conn", "burst", "default_conn_delay", "key"} } diff --git a/apisix/plugins/limit-count.lua b/apisix/plugins/limit-count.lua index 42db2d54784b..3e9d4af28ade 100644 --- a/apisix/plugins/limit-count.lua +++ b/apisix/plugins/limit-count.lua @@ -34,7 +34,8 @@ local schema = { enum = {"remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"}, }, - rejected_code = {type = "integer", minimum = 200, maximum = 600}, + rejected_code = {type = "integer", minimum = 200, maximum = 600, + default = 503}, policy = { type = "string", enum = {"local", "redis"}, @@ -53,7 +54,7 @@ local schema = { }, }, additionalProperties = false, - required = {"count", "time_window", "key", "rejected_code"}, + required = {"count", "time_window", "key"}, } diff --git a/apisix/plugins/limit-req.lua b/apisix/plugins/limit-req.lua index e35c4b328e51..1caadce8b2f1 100644 --- a/apisix/plugins/limit-req.lua +++ b/apisix/plugins/limit-req.lua @@ -29,9 +29,9 @@ local schema = { enum = {"remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"}, }, - rejected_code = {type = "integer", minimum = 200}, + rejected_code = {type = "integer", minimum = 200, default = 503}, }, - required = {"rate", "burst", "key", "rejected_code"} + required = {"rate", "burst", "key"} } diff --git a/doc/plugins/limit-count-cn.md b/doc/plugins/limit-count-cn.md index 6f7425628ef9..401a21bb6c8b 100644 --- a/doc/plugins/limit-count-cn.md +++ b/doc/plugins/limit-count-cn.md @@ -31,7 +31,7 @@ |count |必选 |指定时间窗口内的请求数量阈值| |time_window |必选 |时间窗口的大小(以秒为单位),超过这个时间就会重置| |key |必选 |是用来做请求计数的依据,当前接受的 key 有: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for"。| -|rejected_code |必选 |T当请求超过阈值被拒绝时,返回的 HTTP 状态码。| +|rejected_code |可选 |T当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认 503。| |policy |可选 |用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis 服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速).| |redis_host |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。| |redis_port |可选 |当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口,默认端口 6379。| diff --git a/doc/plugins/limit-count.md b/doc/plugins/limit-count.md index 51fb6de63e58..b27d97d6e428 100644 --- a/doc/plugins/limit-count.md +++ b/doc/plugins/limit-count.md @@ -38,7 +38,7 @@ Limit request rate by a fixed number of requests in a given time window. |count |required|the specified number of requests threshold.| |time_window |required|the time window in seconds before the request count is reset.| |key |required|the user specified key to limit the rate. Here is fully key list: "remote_addr", "server_addr", "http_x_real_ip", "http_x_forwarded_for".| -|rejected_code |required|The HTTP status code returned when the request exceeds the threshold is rejected.| +|rejected_code |optional|The HTTP status code returned when the request exceeds the threshold is rejected, default 503.| |policy |optional|The rate-limiting policies to use for retrieving and incrementing the limits. Available values are `local`(the counters will be stored locally in-memory on the node, default value) and `redis`(counters are stored on a Redis server and will be shared across the nodes, usually used it to do the global speed limit).| |redis_host |optional|When using the `redis` policy, this property specifies the address of the Redis server.| |redis_port |optional|When using the `redis` policy, this property specifies the port of the Redis server. The default port is 6379.| diff --git a/doc/plugins/limit-req-cn.md b/doc/plugins/limit-req-cn.md index b05148ca736a..b122f00ceb11 100644 --- a/doc/plugins/limit-req-cn.md +++ b/doc/plugins/limit-req-cn.md @@ -25,10 +25,12 @@ ## 参数 -* `rate`:指定的请求速率(以秒为单位),请求速率超过 `rate` 但没有超过 (`rate` + `brust`)的请求会被加上延时 -* `burst`:请求速率超过 (`rate` + `brust`)的请求会被直接拒绝 -* `rejected_code`:当请求超过阈值被拒绝时,返回的 HTTP 状态码 -* `key`:是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。 +|名称 |可选项 |描述| +|--------- |--------|-----------| +|rate |必选|指定的请求速率(以秒为单位),请求速率超过 `rate` 但没有超过 (`rate` + `brust`)的请求会被加上延时。| +|burst |必选|请求速率超过 (`rate` + `brust`)的请求会被直接拒绝。| +| key |必选|是用来做请求计数的依据,当前接受的 key 有:"remote_addr"(客户端IP地址), "server_addr"(服务端 IP 地址), 请求头中的"X-Forwarded-For" 或 "X-Real-IP"。| +|rejected_code |可选|当请求超过阈值被拒绝时,返回的 HTTP 状态码,默认 503。| ## 示例 diff --git a/doc/plugins/limit-req.md b/doc/plugins/limit-req.md index f388756189f1..eca43d85a4b4 100644 --- a/doc/plugins/limit-req.md +++ b/doc/plugins/limit-req.md @@ -37,8 +37,8 @@ limit request rate using the "leaky bucket" method. |--------- |--------|-----------| |rate |required|is the specified request rate (number per second) threshold. Requests exceeding this rate (and below `burst`) will get delayed to conform to the rate.| |burst |required|is the number of excessive requests per second allowed to be delayed. Requests exceeding this hard limit will get rejected immediately.| -|rejected_code |required|The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.| | key |required|is the user specified key to limit the rate, now accept those as key: "remote_addr"(client's IP), "server_addr"(server's IP), "X-Forwarded-For/X-Real-IP" in request header.| +|rejected_code |optional|The HTTP status code returned when the request exceeds the threshold is rejected. The default is 503.| ## How To Enable diff --git a/doc/plugins/response-rewrite-cn.md b/doc/plugins/response-rewrite-cn.md index d4c1bc66f04e..275b5f252182 100644 --- a/doc/plugins/response-rewrite-cn.md +++ b/doc/plugins/response-rewrite-cn.md @@ -31,7 +31,7 @@ |名字 |可选|说明| |------- |-----|------| -|status_code |可选| 修改上游返回状态码| +|status_code |可选| 修改上游返回状态码,默认保留原始响应代码。| |body |可选| 修改上游返回的 `body` 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉| |body_base64 |可选| 布尔类型,描述 `body` 字段是否需要 base64 解码之后再返回给客户端,用在某些图片和 Protobuffer 场景| |headers |可选| 返回给客户端的 `headers`,这里可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可| diff --git a/doc/plugins/response-rewrite.md b/doc/plugins/response-rewrite.md index 86dfb2caf777..cd676a53271b 100644 --- a/doc/plugins/response-rewrite.md +++ b/doc/plugins/response-rewrite.md @@ -40,7 +40,7 @@ response rewrite plugin, rewrite the content from upstream. |Name |Requirement|Description| |------- |-----|------| -|status_code |optional| New `status code` to client| +|status_code |optional| New `status code` to client, keep the original response code by default.| |body |optional| New `body` to client, and the content-length will be reset too.| |body_base64 |optional| This is a boolean value,identify if `body` in configuration need base64 decoded before rewrite to client.| |headers |optional| Set the new `headers` for client, can set up multiple. If it exists already from upstream, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. | diff --git a/t/admin/plugins.t b/t/admin/plugins.t index 93b8c3bd5f14..135b971ebd14 100644 --- a/t/admin/plugins.t +++ b/t/admin/plugins.t @@ -51,7 +51,7 @@ GET /apisix/admin/plugins --- request GET /apisix/admin/plugins/limit-req --- response_body -{"properties":{"rate":{"minimum":0,"type":"number"},"burst":{"minimum":0,"type":"number"},"key":{"enum":["remote_addr","server_addr","http_x_real_ip","http_x_forwarded_for"],"type":"string"},"rejected_code":{"minimum":200,"type":"integer"}},"required":["rate","burst","key","rejected_code"],"type":"object"} +{"properties":{"rate":{"minimum":0,"type":"number"},"burst":{"minimum":0,"type":"number"},"key":{"enum":["remote_addr","server_addr","http_x_real_ip","http_x_forwarded_for"],"type":"string"},"rejected_code":{"type":"integer","default":503,"minimum":200}},"required":["rate","burst","key"],"type":"object"} --- no_error_log [error] diff --git a/t/admin/schema.t b/t/admin/schema.t index d266f2afce72..d98b491d79a5 100644 --- a/t/admin/schema.t +++ b/t/admin/schema.t @@ -131,7 +131,7 @@ passed --- request GET /apisix/admin/schema/plugins/limit-count --- response_body eval -qr/"required":\["count","time_window","key","rejected_code"]/ +qr/"required":\["count","time_window","key"\]/ --- no_error_log [error] diff --git a/t/plugin/limit-conn.t b/t/plugin/limit-conn.t index 3cfbc6d8f3db..02f907f6a1ac 100644 --- a/t/plugin/limit-conn.t +++ b/t/plugin/limit-conn.t @@ -797,3 +797,64 @@ GET /test_concurrency 503 --- error_log limit key: 10.10.10.2route + + + +=== TEST 20: default rejected_code +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "limit-conn": { + "conn": 100, + "burst": 50, + "default_conn_delay": 0.1, + "key": "remote_addr" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/limit_conn" + }]], + [[{ + "node": { + "value": { + "plugins": { + "limit-conn": { + "rejected_code": 503 + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/limit_conn" + }, + "key": "/apisix/routes/1" + }, + "action": "set" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] diff --git a/t/plugin/limit-count.t b/t/plugin/limit-count.t index 988a99cf9a9c..0e22937d029a 100644 --- a/t/plugin/limit-count.t +++ b/t/plugin/limit-count.t @@ -585,3 +585,55 @@ passed [200, 200, 503, 503] --- no_error_log [error] + + + +=== TEST 20: default rejected_code +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "key": "remote_addr" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]], + [[{ + "node": { + "value": { + "plugins": { + "limit-count": { + "rejected_code": 503 + } + } + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] diff --git a/t/plugin/limit-req.t b/t/plugin/limit-req.t index a9d4127bb8c7..001d975d684f 100644 --- a/t/plugin/limit-req.t +++ b/t/plugin/limit-req.t @@ -379,3 +379,56 @@ GET /t passed --- no_error_log [error] + + + +=== TEST 11: default rejected_code +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "limit-req": { + "rate": 4, + "burst": 2, + "key": "remote_addr" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "desc": "上游节点", + "uri": "/hello" + }]], + [[{ + "node": { + "value": { + "plugins": { + "limit-req": { + "rejected_code": 503, + "key": "remote_addr" + } + } + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error]