Hello!
On Mon, Apr 28, 2014 at 5:17 AM, George Cao wrote:
> location /uri {
> content_by_lua '
> -- some other stuff
> local response = ngx.location.capture('/uri/as/memcached/key')
> if 200 == response.status
> ngx.say(response.body)
> end
> ';
> }
>
> location /uri/as/memcached/key {
> set $memcached_key $uri
Please note that the nginx builtin variable $uri always evaluates to
the current (sub)request's URI. So when you're using a subrequest
(initiated by ngx.location.capture, for example) to access this
location, $uri will always be "/uri/as/memcached/key" (or with this
prefix).
I think you may want to use $request_uri here, which evaluates to the
main request's raw URL (including querystring though).
BTW, you've missed quite a few semicolons in your nginx configuration
above. Does it mean that you have not tested the configuration you
pasted above yourself? Please always copy&paste your tested
configuration, otherwise I cannot take you seriously.
> The problem is when the key is missed from the memcached servers, the
> subrequest /uri/as/memcached/key issued by ngx.location.capture always
> return status code 404, which means it did not try to get the response from
> the real backend app servers.
>
That's incorrect. When a key does not exist in memcached, ngx_memc
will return the 404 error page as well (that is, when the memcached
server responds "NOT_FOUND").
> But if we issue a request through a browser /uri/as/memcached/key, we got
> the correct status code 200 and also the right response body.
>
That's because when the request accessing location
/uri/as/memcached/key is the main request, $uri is what you want.
> So what's wrong with ngx.location.capture?
>
If you believe there is a bug in ngx.location.capture, please provide
a minimal and complete example that can reproduce the issue you're
seeing on others' boxes with the precise steps you specify. Otherwise
we can easily waste our time here.
It's worth mentioning that for your very use case, the ngx_srcache
module looks like a better fit:
https://github.com/openresty/srcache-nginx-module#readme
Best regards,
-agentzh