diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index 7255a10e1c11..b7c1e1735291 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -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; diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index 962a2705fe3f..d87342b93221 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -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 = { diff --git a/apisix/plugins/proxy-cache.lua b/apisix/plugins/proxy-cache.lua index b3acbb06a8dd..78a285802c30 100644 --- a/apisix/plugins/proxy-cache.lua +++ b/apisix/plugins/proxy-cache.lua @@ -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 diff --git a/t/APISIX.pm b/t/APISIX.pm index 1a51d1e35a30..feb82d0c5463 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -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) diff --git a/t/plugin/proxy-cache.t b/t/plugin/proxy-cache.t index 5a1e2442d51a..c1d6dcd7cfcd 100644 --- a/t/plugin/proxy-cache.t +++ b/t/plugin/proxy-cache.t @@ -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]