Skip to content

Commit

Permalink
fix(proxy-cache): keep cache_method same with nginx's proxy_cache_met…
Browse files Browse the repository at this point in the history
…hods (#4814)
  • Loading branch information
agile6v authored Aug 15, 2021
1 parent 1cfbaa8 commit ca5ea1f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion apisix/plugins/proxy-cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/proxy-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'. |
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/proxy-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'时将不会缓存数据 |
Expand Down
2 changes: 1 addition & 1 deletion t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions t/plugin/proxy-cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit ca5ea1f

Please sign in to comment.