diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index 69dcfe7d224c..0a6c2c174442 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -596,7 +596,7 @@ http { proxy_cache $upstream_cache_zone; proxy_cache_valid any {% if proxy_cache.cache_ttl then %} {* proxy_cache.cache_ttl *} {% else %} 10s {% end %}; proxy_cache_min_uses 1; - proxy_cache_methods GET HEAD; + proxy_cache_methods GET HEAD POST; proxy_cache_lock_timeout 5s; proxy_cache_use_stale off; proxy_cache_key $upstream_cache_key; diff --git a/apisix/plugins/proxy-cache.lua b/apisix/plugins/proxy-cache.lua index 326b529fe50f..34f30569d9aa 100644 --- a/apisix/plugins/proxy-cache.lua +++ b/apisix/plugins/proxy-cache.lua @@ -63,7 +63,11 @@ local schema = { cache_method = { type = "array", minItems = 1, - items = core.schema.method_schema, + items = { + description = "supported http method", + type = "string", + enum = {"GET", "POST", "HEAD"}, + }, uniqueItems = true, default = {"GET", "HEAD"}, }, diff --git a/docs/en/latest/plugins/proxy-cache.md b/docs/en/latest/plugins/proxy-cache.md index 361678408737..0fc3d69d5dff 100644 --- a/docs/en/latest/plugins/proxy-cache.md +++ b/docs/en/latest/plugins/proxy-cache.md @@ -35,7 +35,7 @@ The proxy-cache plugin, which provides the ability to cache upstream response da | cache_zone | string | optional | disk_cache_one | | Specify which cache area to use, each cache area can be configured with different paths. In addition, cache areas can be predefined in conf/config.yaml file. When the default value is not used, the specified cache area is inconsistent with the pre-defined cache area in the conf/config.yaml file, and the cache is invalid. | | cache_key | array[string] | optional | ["$host", "$request_uri"] | | key of a cache, can use variables. For example: ["$host", "$uri", "-cache-id"] | | cache_bypass | array[string] | optional | | | Whether to skip cache retrieval. That is, do not look for data in the cache. It can use variables, and note that cache data retrieval will be skipped when the value of this attribute is not empty or not '0'. For example: ["$arg_bypass"] | -| cache_method | array[string] | optional | ["GET", "HEAD"] | ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD","OPTIONS", "CONNECT", "TRACE"] | Decide whether to be cached according to the request method | +| cache_method | array[string] | optional | ["GET", "HEAD"] | ["GET", "POST", "HEAD"] | Decide whether to be cached according to the request method | | cache_http_status | array[integer] | optional | [200, 301, 404] | [200, 599] | Decide whether to be cached according to the upstream response status | | hide_cache_headers | boolean | optional | false | | Whether to return the Expires and Cache-Control response headers to the client, | | no_cache | array[string] | optional | | | Whether to cache data, it can use variables, and note that the data will not be cached when the value of this attribute is not empty or not '0'. | diff --git a/docs/zh/latest/plugins/proxy-cache.md b/docs/zh/latest/plugins/proxy-cache.md index ca51183cac6b..c9ff8cddc5df 100644 --- a/docs/zh/latest/plugins/proxy-cache.md +++ b/docs/zh/latest/plugins/proxy-cache.md @@ -35,7 +35,7 @@ title: proxy-cache | cache_zone | string | 可选 | disk_cache_one | | 指定使用哪个缓存区域,不同的缓存区域可以配置不同的路径,在 conf/config.yaml 文件中可以预定义使用的缓存区域。当不使用默认值时,指定的缓存区域与 conf/config.yaml 文件中预定义的缓存区域不一致,缓存无效。 | | cache_key | array[string] | 可选 | ["$host", "$request_uri"] | | 缓存key,可以使用变量。例如:["$host", "$uri", "-cache-id"] | | cache_bypass | array[string] | 可选 | | | 是否跳过缓存检索,即不在缓存中查找数据,可以使用变量,需要注意当此参数的值不为空或非'0'时将会跳过缓存的检索。例如:["$arg_bypass"] | -| cache_method | array[string] | 可选 | ["GET", "HEAD"] | ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD","OPTIONS", "CONNECT", "TRACE"] | 根据请求method决定是否需要缓存 | +| cache_method | array[string] | 可选 | ["GET", "HEAD"] | ["GET", "POST", "HEAD"] | 根据请求method决定是否需要缓存 | | cache_http_status | array[integer] | 可选 | [200, 301, 404] | [200, 599] | 根据响应码决定是否需要缓存 | | hide_cache_headers | boolean | 可选 | false | | 是否将 Expires 和 Cache-Control 响应头返回给客户端 | | no_cache | array[string] | 可选 | | | 是否缓存数据,可以使用变量,需要注意当此参数的值不为空或非'0'时将不会缓存数据 | diff --git a/t/APISIX.pm b/t/APISIX.pm index 706d29c077e1..5b960cc87f8b 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -665,7 +665,7 @@ _EOC_ proxy_cache \$upstream_cache_zone; proxy_cache_valid any 10s; proxy_cache_min_uses 1; - proxy_cache_methods GET HEAD; + proxy_cache_methods GET HEAD POST; proxy_cache_lock_timeout 5s; proxy_cache_use_stale off; proxy_cache_key \$upstream_cache_key; diff --git a/t/plugin/proxy-cache.t b/t/plugin/proxy-cache.t index c1d6dcd7cfcd..1ea16ce4f732 100644 --- a/t/plugin/proxy-cache.t +++ b/t/plugin/proxy-cache.t @@ -737,3 +737,47 @@ Cache-Control: bar Expires: any --- no_error_log [error] + + + +=== TEST 27: sanity check (invalid method for cache_method) +--- 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": { + "proxy-cache": { + "cache_zone": "disk_cache_one", + "cache_bypass": ["$arg_bypass"], + "cache_method": ["GET", "PUT"], + "cache_http_status": [200], + "hide_cache_headers": true, + "no_cache": ["$arg_no_cache"] + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- error_code: 400 +--- response_body eval +qr/failed to check the configuration of plugin proxy-cache err/ +--- no_error_log +[error]