How to make a subrequest from content handler?

Yichun Zhang (agentzh) agentzh at gmail.com
Thu Feb 26 20:07:52 UTC 2015


Hello!

On Thu, Feb 26, 2015 at 4:41 AM, kabirova wrote:
> I have a problem when using subrequest in content handler.
> The content handler (my_content_handler) calls
> ngx_http_read_client_request_body with callback handler (my_callback).
> my_callback() makes a subrequest:
>

Just check out how my ngx_echo module implements its
echo_read_request_body [1] and echo_subrequest [2]. Similarly, our
ngx_lua module implements the ngx.req.read_body() and
ngx.location.capture in the content phase (via content_by_lua).

Maybe you can directly use ngx_echo or ngx_lua for your purposes to
save all the pain on the nginx C land.

> I have 2 questions:
> 1. how to return back to my_content_handler, if it invokes a subrequest?
>

This depends on whether you're doing sync or async subrequests (that
is, whether your content handler needs to wait for the subrequest's
completion). It's hard to explain well in just a few sentences and
your best bet is the existing proven working code out there (see
above).

> 2. how to proxy subrequest's response to client?
>

Both ngx_echo and ngx_lua can do this and the latter has more control
over the subrequest response.

In addition, my ngx_srcache module also uses subrequest extensively
though in an earlier phase (access phase):

    https://github.com/openresty/srcache-nginx-module

Still worth a look if you still insist wrestling on the nginx C land :)

Well, good luck!

Best regards,
-agentzh

[1] http://wiki.nginx.org/HttpEchoModule#echo_read_request_body
[2] http://wiki.nginx.org/HttpEchoModule#echo_subrequest



More information about the nginx-devel mailing list