openresty / srcache-nginx-module

Transparent subrequest-based caching layout for arbitrary nginx locations.
http://wiki.nginx.org/NginxHttpSRCacheModule
476 stars 105 forks source link

srcache_fetch: $key is not set correctly after access_by_lua subrequest #66

Closed edo888 closed 7 years ago

edo888 commented 7 years ago

Hi,

I have the configuration below:

set $mykey test;
access_by_lua 'local res = ngx.location.capture("/my_test") if res.status == 200 then ngx.var.mykey = res.body end';

set $key $mykey;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;

Where /my_test sub-request returns test2 with status code 200.

I expect the $key passed to memc to be set to test2, but it is always set to test.

I have read that access_by_lua runs in the end of access, while srcache_fetch runs post-access.

Could you explain this behavior?

P.S. Compliments for your amazing modules!!!

Thanks!

agentzh commented 7 years ago

@edo888 You confused with the nginx directive running order. The set directive runs in the rewrite phase, which means that they always run before the access_by_lua directive which runs in the access phase, a later phase, no matter how you arrange them in the nginx.conf file. Please read my nginx tutorials on the directive running order and nginx's running phases first to avoid such common mistakes for nginx freshmen:

https://openresty.org/download/agentzh-nginx-tutorials-en.html

edo888 commented 7 years ago

Thanks for the clarification! It makes sense now. I have solved it by passing $mykey to srcache directly and doing the set on /memc location.

I have tried also by using rewrite_by_lua with rewrite_by_lua_no_postpone on, but as I've read further the order of the execution is undefined and I was not getting the desired result.

Thanks!