I am doing some calculation in a location that generates content using 'content_by_lua'. I would like this calculation to be cached for a period of time. I have tried couple things but none of those work. I would appreciate any insight you have to make this work.
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:8m max_size=30m inactive=2m;
server {
listen 9001;
location /test {
proxy_cache my_cache;
proxy_cache_valid 200 2m;
proxy_cache_min_uses 1;
proxy_cache_key "fooo";
proxy_ignore_headers Expires Cache-Control;
content_by_lua '
local res = ngx.location.capture("/main")
ngx.status = res.status
ngx.print(res.body)
';
more_set_headers "X-Cache-Status: $upstream_cache_status";
}
location /main {
proxy_cache my_cache; # this line is crucial or the cache will be disabled!
proxy_cache_valid 200 2m;
proxy_cache_min_uses 1;
proxy_cache_key "roooo";
proxy_ignore_headers Expires Cache-Control;
content_by_lua '
local res = ngx.location.capture("/index.html")
ngx.status = res.status
for key, value in pairs(res.header) do
ngx.log (ngx.NOTICE, "copying header key:"..key.." value="..value)
ngx.header[key] = value
end
ngx.say("time :", ngx.now())
ngx.say("cache-status: ", res.header["X-Cache-Status"])
ngx.print(res.body)
';
expires 2m;
}
location /index.html {
return 200 "hoooofooookoooo";
}
}
Both /main and /test are not doing any caching and for every request I can see the updated time. I am not sure what I am doing wrong.