Response body missing when using ngx.location.capture() to fetch response from a location that uses proxy_pass, cache & aio
I have a setup where requests are captured by a location that has a content_by_lua_file directive. The lua code here contains some pre and post processing, but the important bit is that it uses ngx.location.capture to make a request to another nginx location. This location is setup with proxy_pass and cache.
I wanted to measure the impact of the dedicated io threads feature that was added to nginx core recently and so recompiled openresty (1.9.3.1) with --with-threads and --with-file-aio flags passed to the configure script and after the rebuild and nginx restart, added the aio threads; directive to the second location that handles proxying and caching.
The result was that the response body was missing when I hit the location handled by lua. The headers are present. Response looks fine when I hit the cached location directly instead of hitting the lua location that uses ngx.localtion.capture
Here is what the example setup looks like:
location /location1 {
content_by_lua_file "location1.lua";
}
location /location2 {
rewrite /location2/(.*) /$1 break;
proxy_pass http://localhost:8081;
proxy_cache my_cache;
proxy_cache_key $uri$is_args$args;
proxy_cache_lock on;
proxy_cache_valid 200 7d;
proxy_ignore_headers "Expires" "Cache-Control" "Set-Cookie";
# add a cache hit status header
more_set_headers "X-Nginx-Cache: $upstream_cache_status";
# newly added
aio threads;
}The contents of location1.lua look something like this:
local url = "/location2" .. ngx.var.uri
sr_response = ngx.location.capture(url)
-- additional logic
ngx.status = sr_response.status
ngx.print(sr_response.body)The goal is to take advantage of nginx's HTTP caching capabilities and to execute some additional logic in lua based on the response.
Removing the aio directive makes it work as expected again. Same results with aio on; instead of aio threads;
Tried it with the error_log level set to debug, didn't see anything except for a couple lines that said route matched location2 and that the connection was closed after that.
Source: openresty/lua-nginx-module