fix: don't override cache relative headers (#3789)

This commit is contained in:
罗泽轩 2021-03-10 09:02:28 +08:00 committed by GitHub
parent 3db716dd1b
commit d538105b1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 21 deletions

View File

@ -492,8 +492,6 @@ http {
set $upstream_cache_key '';
set $upstream_cache_bypass '';
set $upstream_no_cache '';
set $upstream_hdr_expires '';
set $upstream_hdr_cache_control '';
proxy_cache $upstream_cache_zone;
proxy_cache_valid any {% if proxy_cache.cache_ttl then %} {* proxy_cache.cache_ttl *} {% else %} 10s {% end %};
@ -505,11 +503,6 @@ http {
proxy_no_cache $upstream_no_cache;
proxy_cache_bypass $upstream_cache_bypass;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
add_header Cache-Control $upstream_hdr_cache_control;
add_header Expires $upstream_hdr_expires;
add_header Apisix-Cache-Status $upstream_cache_status always;
{% end %}
proxy_pass $upstream_scheme://apisix_backend$upstream_uri;

View File

@ -129,8 +129,6 @@ do
upstream_no_cache = true,
upstream_cache_key = true,
upstream_cache_bypass = true,
upstream_hdr_expires = true,
upstream_hdr_cache_control = true,
}
local mt = {

View File

@ -266,14 +266,21 @@ function _M.header_filter(conf, ctx)
end
end
local upstream_hdr_cache_control
local upstream_hdr_expires
if conf.hide_cache_headers == true then
ctx.var.upstream_hdr_cache_control = ""
ctx.var.upstream_hdr_expires = ""
upstream_hdr_cache_control = ""
upstream_hdr_expires = ""
else
ctx.var.upstream_hdr_cache_control = ctx.var.upstream_http_cache_control
ctx.var.upstream_hdr_expires = ctx.var.upstream_http_expires
upstream_hdr_cache_control = ctx.var.upstream_http_cache_control
upstream_hdr_expires = ctx.var.upstream_http_expires
end
core.response.set_header("Cache-Control", upstream_hdr_cache_control,
"Expires", upstream_hdr_expires,
"Apisix-Cache-Status", ctx.var.upstream_cache_status)
ctx.var.upstream_no_cache = no_cache
core.log.info("proxy-cache no cache:", no_cache)
end

8
t/APISIX.pm vendored
View File

@ -539,8 +539,6 @@ _EOC_
set \$upstream_cache_key '';
set \$upstream_cache_bypass '';
set \$upstream_no_cache '';
set \$upstream_hdr_expires '';
set \$upstream_hdr_cache_control '';
proxy_cache \$upstream_cache_zone;
proxy_cache_valid any 10s;
@ -552,12 +550,6 @@ _EOC_
proxy_no_cache \$upstream_no_cache;
proxy_cache_bypass \$upstream_cache_bypass;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
add_header Cache-Control \$upstream_hdr_cache_control;
add_header Expires \$upstream_hdr_expires;
add_header Apisix-Cache-Status \$upstream_cache_status always;
access_by_lua_block {
-- wait for etcd sync
ngx.sleep($wait_etcd_sync)

View File

@ -688,3 +688,52 @@ GET /t
qr/failed to check the configuration of plugin proxy-cache err: cache_key variable \$request_method unsupported/
--- no_error_log
[error]
=== TEST 25: don't override cache relative headers
--- 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,
[[{
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/echo"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 26: hit route
--- request
GET /echo
--- more_headers
Apisix-Cache-Status: Foo
Cache-Control: bar
Expires: any
--- response_headers
Apisix-Cache-Status: Foo
Cache-Control: bar
Expires: any
--- no_error_log
[error]