Hello!
2015-03-05 22:52 GMT-08:00 宋绪双:
> 我在nginx lua 中获取用户ip,我前端有CDN->核心haproxy->nginx lua,在nginx lua中想截取用户真实ip,这时候我抓包发现header里面X-Forward-For有一个或多个节点的情况,如果把他截取出来了,我用的方法如下,x-forward-for一个的时候是正常的,当经过多个节点就异常
>
[...]
> IP = ngx.req.get_headers()["X-Forward-For"]
你需要考虑你这里的 IP 变量是 Lua table 的情况(对于多个同名响应头的情形,会返回一个数组类型的 Lua table)。引用一下对应的官方文档:
https://github.com/openresty/lua-nginx-module#ngxreqget_headers
"For multiple instances of request headers such as:
Foo: foo
Foo: bar
Foo: baz
the value of ngx.req.get_headers()["Foo"] will be a Lua (array) table such as:
{"foo", "bar", "baz"}
"
> nginx lua中如何时直接调用nginx log 的 $http_x_forwarded_for方法生成上面格式的内容了,请指点,非常感谢
可以使用 ngx.var.http_x_forwarded_for,更多细节见对应的官方文档:
https://github.com/openresty/lua-nginx-module#ngxvarvariable
值得一提的是,请尽量使用 openresty 中文邮件列表来讨论这样的问题,谢谢合作!更多细节见 http://openresty.org/#Community
Regards,
-agentzh
P.S. 同时抄送给该列表。