nginx cache in front of a location served by 'content_by_lua'

899 views
Skip to first unread message

Gaurav Saxena

unread,
Jun 6, 2017, 9:19:27 PM6/6/17
to openresty-en
Hi,

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. 

Robert Paprocki

unread,
Jun 6, 2017, 9:20:53 PM6/6/17
to openre...@googlegroups.com
Hi,

proxy_cache implies that the location is being served by proxy_pass. Content generated by a handler other than proxy_pass (e.g. content_by_lua) is not cached by proxy pass, and therefore needs to be cached manually via some other mechanism.
 

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gaurav Saxena

unread,
Jun 7, 2017, 12:23:46 AM6/7/17
to openresty-en, rob...@cryptobells.com
Thanks a lot for the quick reply.
Do you know of any examples for custom caching in lua?
Hi,

To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

Robert Paprocki

unread,
Jun 7, 2017, 2:33:57 PM6/7/17
to Gaurav Saxena, openresty-en
Hey Gaurav,



On Tue, Jun 6, 2017 at 9:23 PM, Gaurav Saxena <grvsax...@gmail.com> wrote:
Thanks a lot for the quick reply.
Do you know of any examples for custom caching in lua?


I don't think there are any boilerplate examples of caching manually generated content, but it shouldn't be too hard to build your own, as long as you know how to key your data. Have a look at a few community libs as reference examples:


https://github.com/hamishforbes/lua-resty-tlc
https://github.com/thibaultcha/lua-resty-mlcache

Basically you just need to figure out how you want to cache that data (e.g. the cache key), then store that data in the appropriate medium (per-worker LRU, shared dict, remote network server, etc), and generate your content when the cache lookup misses. Good luck with it!

Gaurav Saxena

unread,
Jun 8, 2017, 12:09:49 AM6/8/17
to rob...@cryptobells.com, openresty-en
Thanks a lot, Robert. Appreciate the detailed pointers :).

Thanks and Regards ,
Gaurav

tokers

unread,
Jun 8, 2017, 2:11:14 AM6/8/17
to openresty-en
Hi

Let's suppose that you have two location A and B, in the location A, you can set some directives about proxy_cache and proxy_pass to location B, the location B is where you can use content_by_lua generating contents.
Frankly, i don't think this is a good solution. :(

Gaurav Saxena

unread,
Jun 13, 2017, 2:32:25 AM6/13/17
to openresty-en
Thanks. Is it possible to do a proxy_pass to a different location? I tried proxy_pass to a different location and that didn't work. 
Reply all
Reply to author
Forward
0 new messages