ngx.var.proxy_host and ngx.var.upstream_addr show niil (Even though $proxy_host and $upstream_addr show valid information)

909 views
Skip to first unread message

rvsw

unread,
Dec 4, 2014, 7:15:23 AM12/4/14
to openre...@googlegroups.com
Hello agentzh
I added the following in header_filter_by_lua
ngx.log(ngx.ERR, "ngx.var.proxy_host ", ngx.var.proxy_host)
ngx.log(ngx.ERR, "ngx.var.upstream_addr ", ngx.var.upstream_addr)


I see nil for both
However, if I log the variables in access log, I get the  expected value.

I tried in other phases also with the same result. I am running lua-nginx-module version 0.9.11 (i.e. after July 11 2014). I do see a bug fixed in Oct 2013 as detailed at http://mailman.nginx.org/pipermail/nginx-devel/2013-October/004440.html,  but this does not seem to exist any longer.
I am using nginx version 1.7.0.

Please advise on how to troubleshoot this.
Thanks

Yichun Zhang (agentzh)

unread,
Dec 4, 2014, 3:17:49 PM12/4/14
to openresty-en
Hello!

On Thu, Dec 4, 2014 at 4:15 AM, rvsw wrote:
> I added the following in header_filter_by_lua
> ngx.log(ngx.ERR, "ngx.var.proxy_host ", ngx.var.proxy_host)
> ngx.log(ngx.ERR, "ngx.var.upstream_addr ", ngx.var.upstream_addr)
> I see nil for both
> However, if I log the variables in access log, I get the expected value.
>

The $proxy_host and $proxy_port variables in the official nginx core
do not allow dynamic lookup in scripting languages like Lua and Perl.
And your use case requires the following patch for the official nginx
core or just a recent version of OpenResty (which includes the patch
for its bundled nginx core by default):

http://mailman.nginx.org/pipermail/nginx-devel/2013-October/004440.html

The $upstream_addr does *not* have this limitation and should work out
of the box though. Are you 100% sure you have no typos in your Lua
code?

> I tried in other phases also with the same result. I am running
> lua-nginx-module version 0.9.11 (i.e. after July 11 2014). I do see a bug
> fixed in Oct 2013 as detailed at
> http://mailman.nginx.org/pipermail/nginx-devel/2013-October/004440.html,
> but this does not seem to exist any longer.

Well, this patch never gets merged into the mainline nginx because
Maxim Dounin thought it was useless (you can see his response in the
followup emails in that very thread).

I've tried the following minimal example on my side with the latest
OpenResty 1.7.7.1 RC2 release and it works as expected:

location = /t {
proxy_pass http://agentzh.org/;
header_filter_by_lua '
ngx.log(ngx.ERR, "proxy host: ", ngx.var.proxy_host)
ngx.log(ngx.ERR, "upstream addr: ", ngx.var.upstream_addr)
';
}

Accessing /t produces the following 2 nginx error log messages on my side:

[error] 22381#0: *1 [lua] header_filter_by_lua:2: proxy host:
agentzh.org ...
[error] 22381#0: *1 [lua] header_filter_by_lua:3: upstream addr:
106.187.41.147:80 ...

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages