Headers not returned from subrequest #68

Closed
Fry-kun opened this Issue Nov 22, 2011 · 3 comments

Projects

None yet

2 participants

@Fry-kun
Fry-kun commented Nov 22, 2011

lua-nginx 5fcb1a7
nginx 1.1.8

I'm trying to have nginx add a custom return header, but lua code doesn't see it. If it matters, my original code sets it in a redis2 internal location, but for simplicity the example below uses echo

nginx config:

location /foo {
content_by_lua 'local u=require("utils");local res=ngx.location.capture("/bar"); ngx.print(u.pformat(res))';
}
location /bar {
echo "baz";
add_header "Foo" "Bar";
}

Results:

$ curl -v 'http://localhost/bar'

  • About to connect() to localhost port 80 (#0)
  • Trying 127.0.0.1... connected
  • Connected to localhost (127.0.0.1) port 80 (#0)

    GET /bar HTTP/1.1
    User-Agent: curl/7.21.3-DEV (x86_64-unknown-linux-gnu) libcurl/7.21.3-DEV OpenSSL/1.0.0 zlib/1.2.5 libssh2/1.2.4
    Host: localhost
    Accept: /

    < HTTP/1.1 200 OK
    < Server:
    < Date: Tue, 22 Nov 2011 22:55:08 GMT
    < Content-Type: application/octet-stream
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < Foo: Bar
    <
    baz
  • Connection #0 to host localhost left intact
  • Closing connection #0

$ curl -v 'http://localhost/foo'

  • About to connect() to localhost port 80 (#0)
  • Trying 127.0.0.1... connected
  • Connected to localhost (127.0.0.1) port 80 (#0)

    GET /foo HTTP/1.1
    User-Agent: curl/7.21.3-DEV (x86_64-unknown-linux-gnu) libcurl/7.21.3-DEV OpenSSL/1.0.0 zlib/1.2.5 libssh2/1.2.4
    Host: localhost
    Accept: /

    < HTTP/1.1 200 OK
    < Server:
    < Date: Tue, 22 Nov 2011 22:55:37 GMT
    < Content-Type: application/octet-stream
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    <
    unnamed = {
    ["status"] = 200;
    ["body"] = "baz
    ";
    ["header"] = {
    ["Content-Type"] = "application/octet-stream";
    };
    };
  • Connection #0 to host localhost left intact
  • Closing connection #0
@agentzh
Member
agentzh commented Nov 23, 2011

On Wed, Nov 23, 2011 at 6:57 AM, Fry-kun
reply@reply.github.com
wrote:

lua-nginx 5fcb1a7
nginx 1.1.8

I'm trying to have nginx add a custom return header, but lua code doesn't see it.

Subrequest response headers will not be automatically forwarded to its
parent request. You have to forward any yourself if you want to, via
the ngx.header API:

http://wiki.nginx.org/HttpLuaModule#ngx.header.HEADER

If it matters, my original code sets it in a redis2 internal location, but for simplicity the example below uses echo
nginx config:

location /foo {
 content_by_lua 'local u=require("utils");local res=ngx.location.capture("/bar"); ngx.print(u.pformat(res))';
}
location /bar {
 echo "baz";
 add_header "Foo" "Bar";
}

You example will not work because

  1. You issued a subrequest to /bar but you do not manually forward its
    response headers to the main request's response.
  2. The standard ngx_headers module works only in main requests, and it
    is a no-op in subrequests. You have to use Lua or ngx_headers_more to
    add the Foo header in location /bar.

Here's an example that should work:

location /foo {
    content_by_lua '
        local u = require "utils"
        local res = ngx.location.capture("/bar")
        if res.status ~= 200 then
            ngx.exit(500)
        end
        ngx.header.Foo = res.header.Foo
        ngx.print(u.pformat(res.body))
    ';
}
location /bar {
    echo "baz";
    more_set_headers "Foo: Bar";
}

Another thing you should take care of the nginx module loading order.
You have to ensure that while running nginx's configure script,
ngx_headers_more appears after ngx_lua such that ngx_headers_more's
output header filter runs before ngx_lua's filter. That is,

./configure --add-module=/path/to/lua-nginx-module \
                 --add-module=/path/to/headers-more-module ...

If you're using our ngx_openresty bundle, then you'll be safe because
ngx_openresty will take care of that:

http://openresty.org/

Regards,
-agentzh

@agentzh
Member
agentzh commented Nov 28, 2011

Consider it resolved.

@agentzh agentzh closed this Nov 28, 2011
@Fry-kun
Fry-kun commented Nov 28, 2011

Thanks for a very thorough response! The issue has been resolved using more_set_headers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment