Controlling request headers from Lua for subrequests

3,316 views
Skip to first unread message

James Hurst

unread,
Sep 14, 2012, 8:26:58 AM9/14/12
to openre...@googlegroups.com
Hi,

I'm using subrequests to proxy to an upstream origin server, and allowing the main request headers to be sent through. In certain cases, I need to modify the request by adding a validator, like "If-Modified-Since" for example... see "Unspecificied end-to-end revalidation": http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4

I can't see a way to do this though? I noticed there is now a set_header() function, but this doesn't affect sub requests. I tried setting "http_if_modified_since" in "vars" for the call to ngx.location.capture(), but this just complains that I'm setting an undefined variable.

Do we need a "header" table for ngx.location.capture()? Or perhaps an option for set_header(include_subrequests?)?

Any other ideas?

Thanks,

-- 
James.

agentzh

unread,
Sep 14, 2012, 1:57:37 PM9/14/12
to openre...@googlegroups.com
Hello!

On Fri, Sep 14, 2012 at 5:26 AM, James Hurst <ja...@riverratrecords.com> wrote:
> I'm using subrequests to proxy to an upstream origin server, and allowing
> the main request headers to be sent through. In certain cases, I need to
> modify the request by adding a validator, like "If-Modified-Since" for
> example... see "Unspecificied end-to-end revalidation":
> http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4.
>
> I can't see a way to do this though? I noticed there is now a set_header()
> function, but this doesn't affect sub requests. I tried setting
> "http_if_modified_since" in "vars" for the call to ngx.location.capture(),
> but this just complains that I'm setting an undefined variable.
>

For now there's several work-around, see:

http://groups.google.com/group/openresty/browse_thread/thread/59d2c011dd366cc3/52e0055013a5e63a

(This thread is in Chinese, you need to use Google Translate or other things.)

Basically it is just

1. set the parent request's request header before calling
ngx.location.capture so that the subrequest will inherit it,

or 2. use proxy_set_header to set the header directly in your
subrequest location:

http://wiki.nginx.org/HttpProxyModule#proxy_set_header

In the option 2, you may need to pass the header value to the
subrequest by means of nginx variables or something like that.

> Do we need a "header" table for ngx.location.capture()? Or perhaps an option
> for set_header(include_subrequests?)?
>

There's a pending pull request from vadim-pavlov that adds an
"extra_headers" option to ngx.location.capture and its friends:

https://github.com/chaoslawful/lua-nginx-module/pull/139

I'm always looking for the time to tweak and merge it :)

Best regards,
-agentzh

James Hurst

unread,
Sep 20, 2012, 6:35:11 AM9/20/12
to openre...@googlegroups.com
Hi,
 
1. set the parent request's request header before calling
ngx.location.capture so that the subrequest will inherit it,

Oh.. this is basically all I need, and it seems to work! When reading the docs, it says "None of the current request's subrequests will be affected"? Perhaps I'm not understanding something? http://wiki.nginx.org/HttpLuaModule#ngx.req.set_header

I just ran a test, and it looks like I can store the parent request's header value, change it with set_header(), capture the subrequest (which indeed inherits the new value), and then restore the original value afterwards. Which for my particular case reads quite well too.

Am I missing something about the note in the docs?

Cheers,

James.

agentzh

unread,
Sep 20, 2012, 2:37:37 PM9/20/12
to openre...@googlegroups.com
Hello!

On Thu, Sep 20, 2012 at 3:35 AM, James Hurst <ja...@riverratrecords.com> wrote:
>> 1. set the parent request's request header before calling
>> ngx.location.capture so that the subrequest will inherit it,
>
> Oh.. this is basically all I need, and it seems to work! When reading the
> docs, it says "None of the current request's subrequests will be affected"?
> Perhaps I'm not understanding something?
> http://wiki.nginx.org/HttpLuaModule#ngx.req.set_header
>

Oh, the documentation is wrong here. I dunno why it says that. I've
just fixed it:

http://wiki.nginx.org/HttpLuaModule#ngx.req.set_header

Nice catch! Thanks!

> I just ran a test, and it looks like I can store the parent request's header
> value, change it with set_header(), capture the subrequest (which indeed
> inherits the new value), and then restore the original value afterwards.
> Which for my particular case reads quite well too.
>

Yes, you can surely do that :)

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