how to get the headers form the result of ngx.location.capture_multi

800 views
Skip to first unread message

bin al

unread,
May 20, 2014, 7:07:48 AM5/20/14
to openre...@googlegroups.com
nginx config:

server {
    location /t1 {
         rewrite ^/t1/(.*) /$1 break;
         proxy_pass  http://192.168.4.1:9000/;
    }

    location /t2 {
         rewrite ^/t2/(.*) /$1 break;
         proxy_pass  http://192.168.4.2:9001/;
    }

    location / {
        local uri = ngx.var.uri
        local res1, res2 = ngx.location.capture_multi{
               { “/t1"..uri, {method=ngx.HTTP_POST}},
               { “/t2"..uri, {method=ngx.HTTP_GET}},
        }
        for k, v in ipairs(res1.header) do
             ngx.say(k, v)
        end
    }
}

Question:
the res1.header is empty table. I want to know why? and how can i get the headers of both requests. 
And I have tried  ngx.resp.get_headers()  .but the result of ngx.resp.get_headers() is still empty table. 

Yichun Zhang (agentzh)

unread,
May 20, 2014, 8:09:47 PM5/20/14
to openresty-en
Hello!

On Tue, May 20, 2014 at 4:07 AM, bin al wrote:
> Question:
> the res1.header is empty table. I want to know why? and how can i get the
> headers of both requests.
> And I have tried ngx.resp.get_headers() .but the result of
> ngx.resp.get_headers() is still empty table.
>

Please note that, many response headers are generated by the standard
ngx_http_header_filter_module which runs quite late in output filter
chain. So in the capturing filters for ngx.location.capture*, most of
the response headers are just not available yet. Well, we could
duplicate some of the logic in ngx_http_header_filter_module in this
case, but running the full logic involves quite some performance
overhead. So we'd better produce response headers selectively. What
headers are you interested in particular here?

Best regards,
-agentzh

bin al

unread,
May 20, 2014, 10:36:21 PM5/20/14
to openre...@googlegroups.com
Thank you for your answer.
I am interested in the cookies which would be used for the latter requests. Authentication information is required by one of the subrequests. 


在 2014年5月21日星期三UTC+8上午8时09分47秒,agentzh写道:

Yichun Zhang (agentzh)

unread,
May 21, 2014, 3:18:28 PM5/21/14
to openresty-en
Hello!

On Tue, May 20, 2014 at 7:36 PM, bin al wrote:
> Thank you for your answer.
> I am interested in the cookies which would be used for the latter requests.
> Authentication information is required by one of the subrequests.
>

I've tried the following standalone example for the Set-Cookie headers
sent from the backend server, which works fine for me:

location = /fake { # fake backend
return 200 "ok";
add_header Set-Cookie foo;
add_header Set-Cookie bar;
}

location /t {
proxy_pass http://127.0.0.1:$server_port/fake;
}

location = /main {
content_by_lua '
local res1, res2 = ngx.location.capture_multi{
{ "/t1", {method=ngx.HTTP_POST}},
{ "/t2", {method=ngx.HTTP_GET}},
}
local concat = table.concat
for k, v in pairs(res1.header) do
if type(v) == "table" then
ngx.say(k, ": ", concat(v, ", "))
else
ngx.say(k, ": ", v)
end
end
';
}

Accessing /main gives:

$ curl localhost:8080/main
Content-Length: 2
Set-Cookie: foo, bar
Content-Type: text/plain

The major mistake in your original example is that you incorrectly
used ipairs() to traverse a hash table (instead of pairs()). Also,
your original example has so many syntax errors and I hope you will
copy&paste your tested configuration snippet the next time.

Best regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages