# How to merge subrequest header

**URL:** <https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579>\
**Category:** NGINX\
**Created:** [November 24, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579 "2010-11-24T14:01:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 24, 2010, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/1 "2010-11-24T14:01:32Z")

</div>

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.

Anybody else has met this same problem, and how to do this?

Thanks.

* * *

The time you enjoy wasting is not wasted time!

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [November 25, 2010, 1:55am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/2 "2010-11-25T01:55:49Z")

</div>

Anybody else has met this same problem, and how to do this?

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [November 25, 2010, 11:20am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/3 "2010-11-25T11:20:14Z")

</div>

On Wed, Nov 24, 2010 at 9:00 PM, Roast [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [November 25, 2010, 11:37am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/4 "2010-11-25T11:37:58Z")

</div>

Thanks agentzh.

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;
> 
> ```
> 
> 1. 
> 
> ```
> hh->lowcase_key = ngx_pnalloc(r->pool, h[i].key.len);
> 
> ```

```
return ngx_http_next_header_filter(r);

```

> }

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):

That’s two great module,but we need more work within our module.

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [November 25, 2010, 11:32am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/5 "2010-11-25T11:32:16Z")

</div>

On Thu, Nov 25, 2010 at 6:18 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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:

location /proxy {  
proxy\_pass [百度安全验证](http://www.baidu.com/s);  
}

location /main {  
content\_by\_lua ’  
local res = ngx.location.capture(“/proxy”,  
{ args = { wd = “Perl” } }  
)

```
       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
   ';

```

}

I used the following command line to test the /main location to check  
the response body:

curl localhost/main|grep Perl|iconv -f gbk -t utf8

and the following command to check the response headers:

```
$ curl -I localhost/main
HTTP/1.1 200 OK
Server: nginx/0.8.41
Date: Thu, 25 Nov 2010 10:30:12 GMT
Content-Type: text/html;charset=gbk
Connection: keep-alive
Content-Length: 32274
Cache-Control: private
Set-Cookie: BAIDUID=F8A74DD5F378F2EFFEA0EC6B8BD10E1B:FG=1;

```

expires=Thu, 25-Nov-40 10:30:11 GMT; path=/; [domain=.baidu.com](http://domain=.baidu.com)  
P3P: CP=" OTI DSP COR IVA OUR IND COM "

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [November 25, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/6 "2010-11-25T12:23:48Z")

</div>

On Thu, Nov 25, 2010 at 6:36 PM, Roast [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 😉

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [February 27, 2012, 10:24am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/7 "2012-02-27T10:24:38Z")

</div>

On Sun, Feb 26, 2012 at 8:02 AM, edo888 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you please share some piece of code. I want to make a post  
> subrequest to send the response body to fastcgi and filter it there.

See the detailed discussions here:  
[https://github.com/agentzh/echo-nginx-module/issues/8](https://github.com/agentzh/echo-nginx-module/issues/8)

Best regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Roast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Roast](https://www.ruby-forum.com/u/Roast)\
**Post date:** [February 26, 2012, 1:03am UTC](https://www.ruby-forum.com/t/how-to-merge-subrequest-header/199579/8 "2012-02-26T01:03:04Z")

</div>

Hi,

Can you please share some piece of code. I want to make a post  
subrequest to send the response body to fastcgi and filter it there.

Posted at Nginx Forum:

> **[Re: How to merge subrequest header.](https://forum.nginx.org/read.php?2,152939,222982#msg-222982)**
>
> 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. Anybody else has met this...
