关于"ngx.req.get_uri_args"在lua_code_cache打开之后的诡异行为

133 views
Skip to first unread message

刘宏

unread,
May 9, 2013, 4:19:13 AM5/9/13
to open...@googlegroups.com
hello!
由于需要对所有请求的参数进行合法性检查,所以把参数检查的操作放到一个lua文件中作为公共函数调用,文件内容简要如下
1.public_check.lua
function new(self)
 print(get_uri_args()["app_id"])
 return setmetatable({}, mt) 
end

2.service_1.lua
local public= require "public_check"
local form, err = public:new()
3.nginx.conf
lua_code_cache on;
location ~^/v2/(.+) { 
 default_type 'text/plain'; 
 access_by_lua_fileservice_1; 
          }

以上代码在运行的时候发现第一请求过来的时候 print能打印出值,第二次请求过来的时候就变成nil,但是app_id实际上是有传过来的。
但是把lua_code_cache 改成off以后就正常了,
以上问题是什么原因呢,期待大神解惑啊,急急急!

以上谢谢。



刘宏

unread,
May 9, 2013, 4:31:15 AM5/9/13
to open...@googlegroups.com
 access_by_lua_fileservice_1;  这里应该是
 access_by_lua_file service_1.lua; 
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsub...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
 
 


刘宏

unread,
May 9, 2013, 4:36:42 AM5/9/13
to open...@googlegroups.com
对了,还有这个也加了
local class_mt = {
    -- to prevent use of casual module global variables
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}

setmetatable(_M, class_mt)

lhmwzy

unread,
May 9, 2013, 5:25:49 AM5/9/13
to open...@googlegroups.com
没问题啊
我在lualib/resyt/目录下,放一名为pub_check.lua的文件,内容为:
local setmetatable = setmetatable
local error = error
local ngx = ngx

module(...)
local mt = { __index = _M }
function new(self)
ngx.print(ngx.req.get_uri_args()["app_id"])

return setmetatable({}, mt)
end

local class_mt = {
-- to prevent use of casual module global variables
__newindex = function (table, key, val)
error('attempt to write to undeclared variable "' .. key .. '"')
end
}

在conf/lua/目录下,放一名为service.lua的文件,内容为:
local public= require "resty.pub_check"

local form, err = public:new()

在nginx.conf中通过如下方式调用:

location ~^/v2/(.+) {
    default_type 'text/plain';
    access_by_lua_file 'conf/lua/service.lua';
    echo '';
}
这里没有设置lua_code_cache on;因为默认它就是on



订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

lhmwzy

unread,
May 9, 2013, 5:27:11 AM5/9/13
to open...@googlegroups.com
忘了说明,我用的是openresty-1.2.8.1

agentzh

unread,
May 9, 2013, 3:02:35 PM5/9/13
to openresty
Hello!

2013/5/9 刘宏:
> hello!
> 由于需要对所有请求的参数进行合法性检查,所以把参数检查的操作放到一个lua文件中作为公共函数调用,文件内容简要如下
> 1.public_check.lua
> function new(self)
> print(get_uri_args()["app_id"])
> return setmetatable({}, mt)
> end
>

你这里的 Lua 模块的定义是错误的。还是因为你没有给出完整的代码?

请在报告问题总是尽量给出能复现问题的完整的代码和配置,并提供你使用的相关软件的版本(包括操作系统),谢谢合作 :)

Best regards,
-agentzh

赵旭海

unread,
May 9, 2013, 4:41:50 AM5/9/13
to open...@googlegroups.com
试试把public_check.lua中调用的函数都定义为局部变量。。 然后将这个lua文件定义为module
如:
local sub = string.sub
local len = string.len
local lower = string.lower
local insert = table.insert
local concat = table.concat

module("public_check")


发件人: open...@googlegroups.com [open...@googlegroups.com] 代表 刘宏 [myol...@163.com]
发送时间: 2013年5月9日 16:31
收件人: open...@googlegroups.com
主题: Re:[openresty] 关于"ngx.req.get_uri_args"在lua_code_cache打开之后的诡异行为

订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

刘宏

unread,
May 12, 2013, 9:48:47 PM5/12/13
to open...@googlegroups.com
我目前用的tengine 1.4.5,里面的ngx_lua应该是0.7.19
一下是补齐的文件
public_check.lua

local print = print
local get_uri_args = ngx.req.get_uri_args
module(...)
_VERSION = '0.1'
local mt = { __index = _M }
function new(self)
print(get_uri_args()["app_id"])
return setmetatable({}, mt)
end
local class_mt = {
    -- to prevent use of casual module global variables
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}

setmetatable(_M, class_mt)


经过周末的调试我发现是由于
2.service_1.lua
local public= require "public_check"
local form, err = public:new()
ngx.req.set_header("app_id", app_id)-->这句操作调用导致的,注释掉就不会第二调用的时候出错了。

现在是不知道为什么ngx.req.set_header这个调用会导致第二次以后的请求,在public_check.lua都获取不到ngx.req的值了。
以上谢谢!

> 写道:
>Hello!
>
>2013/5/9 刘宏:
>> hello!
>> 由于需要对所有请求的参数进行合法性检查,所以把参数检查的操作放到一个lua文件中作为公共函数调用,文件内容简要如下
>> 1.public_check.lua
>> function new(self)
>>  print(get_uri_args()["app_id"])
>>  return setmetatable({}, mt)
>> end
>>
>
>你这里的 Lua 模块的定义是错误的。还是因为你没有给出完整的代码?
>
>请在报告问题总是尽量给出能复现问题的完整的代码和配置,并提供你使用的相关软件的版本(包括操作系统),谢谢合作 :)
>
>Best regards,
>-agentzh
>
>-- 
>-- 
>邮件来自列表“openresty”,专用于技术讨论!
>订阅: 请发空白邮件到 openresty+subs...@googlegroups.com

agentzh

unread,
May 13, 2013, 2:45:43 PM5/13/13
to openresty
Hello!

2013/5/12 刘宏:
> 我目前用的tengine 1.4.5,里面的ngx_lua应该是0.7.19

你使用最新的 ngx_openresty 1.2.8.1 能复现此问题么?最新的 ngx_lua 版本已经是 0.8.1 了.

值得一提的是,我不会对 Tengine + ngx_lua 出现的问题提供技术支持。

> 经过周末的调试我发现是由于
> 2.service_1.lua
> local public= require "public_check"
> local form, err = public:new()
> ngx.req.set_header("app_id", app_id)-->这句操作调用导致的,注释掉就不会第二调用的时候出错了。
>
> 现在是不知道为什么ngx.req.set_header这个调用会导致第二次以后的请求,在public_check.lua都获取不到ngx.req的值了。

请提供一个最小化的完整示例以及复现问题所需的操作步骤。你提供的代码片段不足以让我在本地复现你看到的问题 :)

Best regards,
-agentzh

刘宏

unread,
May 13, 2013, 10:59:53 PM5/13/13
to open...@googlegroups.com
Hello!
首先感谢在你的百忙中的回复哦,
已经用ngx_openresty-1.2.8.3.tar.gz建立一个最小化测试用例,附件是lua文件和配置文件 ,lua文件放在nginx/lua目录下。
只要service.lua中有ngx.req.set_header("app_id", "temp")这句调用存在,必然会导致第一次请求以后的所有的请求
print(get_uri_args()["app_id"])打印出来的值都是nil






> 写道:
>Hello!
>
>2013/5/12 刘宏:
>> 我目前用的tengine 1.4.5,里面的ngx_lua应该是0.7.19
>
>你使用最新的 ngx_openresty 1.2.8.1 能复现此问题么?最新的 ngx_lua 版本已经是 0.8.1 了.
>
>值得一提的是,我不会对 Tengine + ngx_lua 出现的问题提供技术支持。
>
>> 经过周末的调试我发现是由于
>> 2.service_1.lua
>> local public= require "public_check"
>> local form, err = public:new()
>> ngx.req.set_header("app_id", app_id)-->这句操作调用导致的,注释掉就不会第二调用的时候出错了。
>>
>> 现在是不知道为什么ngx.req.set_header这个调用会导致第二次以后的请求,在public_check.lua都获取不到ngx.req的值了。
>
>请提供一个最小化的完整示例以及复现问题所需的操作步骤。你提供的代码片段不足以让我在本地复现你看到的问题 :)
>
>Best regards,
>-agentzh
>
>-- 
>-- 
>邮件来自列表“openresty”,专用于技术讨论!
>订阅: 请发空白邮件到 openresty...@googlegroups.com
>发言: 请发邮件到 open...@googlegroups.com
>退订: 请发邮件至 openresty+...@googlegroups.com
nginx.conf
public_check.lua
service.lua

刘宏

unread,
May 14, 2013, 2:09:52 AM5/14/13
to open...@googlegroups.com
貌似发现问题的根源了
是ngx.req.set_header("app_id", "temp")这句里面的app_id的下划线导致的,改成ngx.req.set_header("app-id", "temp")这样写就没问题了,
而且似乎nginx.conf中的underscores_in_headers on;配置对这里无效。






在 2013-05-14 10:59:53,"刘宏" <myol...@163.com> 写道:
Hello!
首先感谢在你的百忙中的回复哦,
已经用ngx_openresty-1.2.8.3.tar.gz建立一个最小化测试用例,附件是lua文件和配置文件 ,lua文件放在nginx/lua目录下。
只要service.lua中有ngx.req.set_header("app_id", "temp")这句调用存在,必然会导致第一次请求以后的所有的请求
print(get_uri_args()["app_id"])打印出来的值都是nil

在 2013-05-14 02:45:43,agentzh <age...@gmail.com
> 写道:
>Hello!
>
>2013/5/12 刘宏:
>> 我目前用的tengine 1.4.5,里面的ngx_lua应该是0.7.19
>
>你使用最新的 ngx_openresty 1.2.8.1 能复现此问题么?最新的 ngx_lua 版本已经是 0.8.1 了.
>
>值得一提的是,我不会对 Tengine + ngx_lua 出现的问题提供技术支持。
>
>> 经过周末的调试我发现是由于
>> 2.service_1.lua
>> local public= require "public_check"
>> local form, err = public:new()
>> ngx.req.set_header("app_id", app_id)-->这句操作调用导致的,注释掉就不会第二调用的时候出错了。
>>
>> 现在是不知道为什么ngx.req.set_header这个调用会导致第二次以后的请求,在public_check.lua都获取不到ngx.req的值了。
>
>请提供一个最小化的完整示例以及复现问题所需的操作步骤。你提供的代码片段不足以让我在本地复现你看到的问题 :)
>
>Best regards,
>-agentzh
>
>-- 
>-- 
>邮件来自列表“openresty”,专用于技术讨论!
>订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
>发言: 请发邮件到 open...@googlegroups.com
>退订: 请发邮件至 openresty+unsub...@googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subs...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsub...@googlegroups.com

agentzh

unread,
May 14, 2013, 8:20:14 PM5/14/13
to openresty
Hello!

2013/5/13 刘宏:
> 貌似发现问题的根源了
> 是ngx.req.set_header("app_id",
> "temp")这句里面的app_id的下划线导致的,改成ngx.req.set_header("app-id", "temp")这样写就没问题了,

ngx_lua 模块的 git 仓库里面最近已经去掉了在 ngx.req.set_header 方法中自动转换请求头的名称中的下载线的逻辑:

https://github.com/chaoslawful/lua-nginx-module/commit/5cd8292

请尝试 ngx_openresty 最新的预发布版 1.2.8.5rc1:

http://openresty.org/download/ngx_openresty-1.2.8.5rc1.tar.gz

> 而且似乎nginx.conf中的underscores_in_headers on;配置对这里无效。
>

lua_transform_underscores_in_response_headers
配置指令从名字上就能看到只作用于“响应头”,而你这里分明设置的是“请求头”,因此没有效果也是在情理之中的了 :)

Best regards,
-agentzh

lhmwzy

unread,
May 14, 2013, 8:47:24 PM5/14/13
to open...@googlegroups.com
ngx.req.set_header的值,也不能通过ngx.req.get_uri_args得到吧?



Best regards,
-agentzh

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

刘宏

unread,
May 15, 2013, 10:44:17 PM5/15/13
to open...@googlegroups.com
感谢春哥回复,应用补丁后问题已经解决,后头琢磨一下问题发生的原因是啥。


>
>> 而且似乎nginx.conf中的underscores_in_headers on;配置对这里无效。
>>
>
>lua_transform_underscores_in_response_headers
>配置指令从名字上就能看到只作用于“响应头”,而你这里分明设置的是“请求头”,因此没有效果也是在情理之中的了 :)
>
>Best regards,
>-agentzh
>
>-- 
>-- 
>邮件来自列表“openresty”,专用于技术讨论!
>订阅: 请发空白邮件到 openresty...@googlegroups.com
>发言: 请发邮件到 open...@googlegroups.com
>退订: 请发邮件至 openresty+...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages