How to merge subrequest header.

agentzh agentzh at gmail.com
Thu Nov 25 13:31:19 MSK 2010


On Thu, Nov 25, 2010 at 6:18 PM, agentzh <agentzh at gmail.com> 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
    P3P: CP=" OTI DSP COR IVA OUR IND COM "

Cheers,
-agentzh



More information about the nginx mailing list