Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't override cache relative headers #3789

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 %};
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
15 changes: 11 additions & 4 deletions apisix/plugins/proxy-cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions t/plugin/proxy-cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]