I use ngx_http_subrequest to send a subrequest with the parent’s
response
body,after the sub request processed, I got the subrequest’s body, but
It
seems has no way to merge the subrequest header to parent request.
Anybody else has met this same problem, and how to do this?
I use ngx_http_subrequest to send a subrequest with the parent’s response
body,after the sub request processed, I got the subrequest’s body, but It
seems has no way to merge the subrequest header to parent request.
You can register a post_subrequest callback function to your
ngx_http_subrequest call and do subrequest header processing there.
Anybody else has met this same problem, and how to do this?
You can check out the corresponding code in our ngx_echo or ngx_lua
module. There’s a live example for merging subrequest headers and body
into the main request in ngx_lua (untested though, but should work):
location /proxy {
proxy_pass http://foo.bar.com/$query_string;
}
location /main {
content_by_lua '
var res = ngx.location.capture("/proxy",
{ args = "/foo/bar/baz" }
)
if res.status == ngx.HTTP_OK then
for k,v in pairs(res.header) do
ngx.header[k] = v
end
ngx.print(res.body)
else
ngx.exit(res.status)
end
';
}
You can register a post_subrequest callback function to your
ngx_http_subrequest call and do subrequest header processing there.
It seems post_subrequest callback function can’t change the parent’s
header,because the parent’s header filter has been process finished
before
the function call, and the output buf has created.
And I have fixed this problem, at the header filter function. Just like
following steps,maybe that’s not a good way, agentzh pls give me some
more
advanced suggestion.
static ngx_int_t
part = &sr->headers_out.headers.part;
part = part->next;
You can check out the corresponding code in our ngx_echo or ngx_lua
module. There’s a live example for merging subrequest headers and body
into the main request in ngx_lua (untested though, but should work):
Sorry, the previous sample has bugs. The following example is tested
and working:
It seems post_subrequest callback function can’t change the parent’s
header,because the parent’s header filter has been process finished before
the function call, and the output buf has created.
The key point is that not to send out the parent headers when you need
to change it later (according to its subrequests or other things).
And I have fixed this problem, at the header filter function. Just like
following steps,maybe that’s not a good way, agentzh pls give me some more
advanced suggestion.
static ngx_int_t
ngx_http_social_header_filter(ngx_http_request_t *r)
{
… //some other works
You can surely buffer the parent requests’ headers and any outputs
before your subrequest actually finishes in your output filter
functions