How to merge subrequest header.

agentzh agentzh at gmail.com
Thu Nov 25 13:18:52 MSK 2010


On Wed, Nov 24, 2010 at 9:00 PM, Roast <zhang.libing at gmail.com> wrote:
> Hi,all.
>
> 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
        ';
    }

Cheers,
-agentzh



More information about the nginx mailing list