OpenResty 1.11.2.3里的cjson调用有问题

1,434 views
Skip to first unread message

chro...@gmail.com

unread,
Apr 24, 2017, 10:17:31 PM4/24/17
to openresty
Hi,

刚下载了OpenResty 1.11.2.3,在Ubuntu 14.04上编译安装,均使用默认配置。然后跑个测试脚本,发生了错误,日志如下:

2017/04/24 15:04:41 [error] 32261#32261: *1 lua entry thread aborted: runtime error: error loading module 'cjson.safe' from file '/usr/local/openresty/lualib/cjson.so':
    /usr/local/openresty/lualib/cjson.so: undefined symbol: luaL_setfuncs
stack traceback:
coroutine 0:
    [C]: in function 'require'
    /home/chrono/openresty/service/http/cjson.lua:4: in function </home/chrono/openresty/service/http/cjson.lua:1>, client: 127.0.0.1, server: localhost, request: "GET /test/cjson HTTP/1.1", host: "127.1"

lua程序很简单:
  1 -- Copyright (C)
  2
  3 -- safe decode
  4 local cjson = require "cjson.safe"
  5
  6 ngx.say("test cjson")

但同样的代码,用1.11.2.2就没有问题,各位能否帮忙解答一下?

Yuansheng Wang

unread,
Apr 26, 2017, 2:54:38 AM4/26/17
to open...@googlegroups.com
parallels@ubuntu:~$ resty -e 'local cjson = require "cjson.safe"
> ngx.say("test cjson")'
test cjson

parallels@ubuntu:~$ openresty -V
nginx version: openresty/1.11.2.3
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 

我这里没有复现你的问题,建议你输出一下 openresty 的版本信息,确定版本用对了。注意看下 make install 是否把文件正确释放了。


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



--

membphis

chro...@gmail.com

unread,
Apr 26, 2017, 10:27:09 PM4/26/17
to openresty
版本没错。
用resty命令行工具执行lua脚本也是正常的,但用curl执行就发生这个错误。

curl 127.1/test/cjson

配置:
location ~ ^/test/(\w+) {
    content_by_lua_file service/http/$1.lua;
}



版本信息:

/usr/local/openresty/bin/openresty -V
nginx version: openresty/1.11.2.3
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
built with OpenSSL 1.0.1f 6 Jan 2014

TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2

在 2017年4月26日星期三 UTC+8下午2:54:38,Yuansheng写道:
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com



--

membphis

武雅超

unread,
Aug 10, 2017, 4:16:34 AM8/10/17
to openresty
我出现了一模一样的问题,很简单的文件,单独的使用resty执行就没问题,但是客户端请求就出现了这个问题。查了一下午查不出来。


在 2017年4月26日星期三 UTC+8下午2:54:38,Yuansheng写道:
parallels@ubuntu:~$ resty -e 'local cjson = require "cjson.safe"
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com



--

membphis

rongfeng liang

unread,
Aug 10, 2017, 9:19:00 PM8/10/17
to openresty
你可以拷贝cjson.so 到lualib 文件,有可能是类库找不到,以前自己安装碰到多。

在 2017年8月10日星期四 UTC+8下午4:16:34,武雅超写道:

武雅超

unread,
Aug 10, 2017, 9:42:20 PM8/10/17
to openresty
现在正常了,1.11.2.3中的cjson.so是37k,版本1.11.2.2中的cjson.so是122k大小,不一样的。但是现在看可以找到,重现不了了

在 2017年8月11日星期五 UTC+8上午9:19:00,rongfeng liang写道:

Zexuan Luo

unread,
Aug 11, 2017, 8:36:22 AM8/11/17
to openresty
会不会是两者用的 LuaJIT 版本不一样?较新版本的 LuaJIT 有 luaL_setfuncs 符号,但旧的版本里没有。

在 2017年8月11日星期五 UTC+8上午9:42:20,武雅超写道:

葛美洁

unread,
Aug 21, 2017, 5:10:58 AM8/21/17
to openresty
openresty/1.11.2.4有相同的问题

在 2017年4月25日星期二 UTC+8上午10:17:31,chro...@gmail.com写道:

Yichun Zhang (agentzh)

unread,
Aug 21, 2017, 2:33:52 PM8/21/17
to openresty
Hello!

2017-08-10 18:18 GMT-07:00 rongfeng liang:
> 你可以拷贝cjson.so 到lualib 文件,有可能是类库找不到,以前自己安装碰到多。
>

OpenResty 已经自带了 lua-cjson 库,而且必须使用 OpenResty 自带的 lua-cjson 库,因为标准的
lua-cjson 使用了 Lua 5.1 中已经废弃了的 Lua 5.0 的 C API,不再兼容最新版本的 LuaJIT,而
OpenResty 自带的 lua-cjson 是没有问题的。

我刚本地试了最新的 OpenResty 1.11.2.5 发布,使用 cjson.safe 模块,一切正常:

agentzh@imac5k ~ $ resty -V
resty 0.19
nginx version: openresty/1.11.2.5
built by clang 8.0.0 (clang-800.0.42.1)
built with OpenSSL 1.0.2k 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx
--with-cc-opt='-O2 -I/usr/local/opt/openssl/include/
-I/usr/local/opt/pcre/include/' --add-module=../ngx_devel_kit-0.3.0
--add-module=../iconv-nginx-module-0.14
--add-module=../echo-nginx-module-0.61
--add-module=../xss-nginx-module-0.05
--add-module=../ngx_coolkit-0.2rc3
--add-module=../set-misc-nginx-module-0.31
--add-module=../form-input-nginx-module-0.12
--add-module=../encrypted-session-nginx-module-0.06
--add-module=../srcache-nginx-module-0.31
--add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07
--add-module=../headers-more-nginx-module-0.32
--add-module=../array-var-nginx-module-0.05
--add-module=../memc-nginx-module-0.18
--add-module=../redis2-nginx-module-0.14
--add-module=../redis-nginx-module-0.3.7
--add-module=../rds-json-nginx-module-0.14
--add-module=../rds-csv-nginx-module-0.07
--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib
-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/'
--with-http_ssl_module

agentzh@imac5k ~ $ resty -e 'ngx.say(require "cjson.safe".encode{ dogs = 32 })'
{"dogs":32}

Regards,
Yichun

Yichun Zhang (agentzh)

unread,
Aug 21, 2017, 2:35:13 PM8/21/17
to openresty
Hello!

2017-08-21 2:10 GMT-07:00 葛美洁:
> openresty/1.11.2.4有相同的问题
>

请尝试升级到最新的 OpenResty 1.11.2.5。

同时确保你使用的是 OpenResty 自带的 lua-cjson,否则会和最新的 LuaJIT 有兼容性问题(见我上一封邮件里的说明)。

Regards,
Yichun

chro...@gmail.com

unread,
Aug 22, 2017, 1:22:56 AM8/22/17
to openresty
刚编译了最新的1.11.2.5,一切均是默认,无任何定制参数,版本信息如下:

/usr/local/openresty/bin/resty -V

resty 0.19
nginx version: openresty/1.11.2.5
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-http_ssl_module

用resty命令行工具确实没有问题,但跑在Nginx里就出错,错还是那样:

 lua entry thread aborted: runtime error: error loading module 'cjson.safe' from file '/usr/local/openresty/lualib/cjson.so':
    /usr/local/openresty/lualib/cjson.so: undefined symbol: luaL_setfuncs
stack traceback:
coroutine 0:
    [C]: in function 'require'
    /home/chrono/openresty/service/http/cjson.lua:4: in function </home/chrono/openresty/service/http/cjson.lua:1>, client: 127.0.0.1, server: localhost, request: "GET /test/cjson HTTP/1.1", host: "127.1"

按理来说resty命令行和自己配置的lua脚本都是一个Nginx,是否是有什么其他的环境变量设置导致的?

在 2017年8月22日星期二 UTC+8上午2:35:13,agentzh写道:

chro...@gmail.com

unread,
Aug 22, 2017, 1:33:51 AM8/22/17
to openresty
看了一下OpenResty里的luajit,可能是这个原因:
因为测试,我有另外一个Nginx单独编译了luajit+ngx_lua,这个luajit比较老,一直没有更新,grep luaL_setfuncs是没有的,而新的OpenResty的luajit则有这个api。

现在的问题是为什么OpenResty找到了老的luajit,而没有使用/usr/local/openresty/luajit/里的。

我机器上的luajit默认安装到了/usr/local目录里,没有定制路径。

在 2017年8月22日星期二 UTC+8下午1:22:56,chro...@gmail.com写道:

chro...@gmail.com

unread,
Aug 22, 2017, 1:38:59 AM8/22/17
to openresty
哪位有空可以这么测试一下,用个较老版本的luajit+ngx_lua编译并安装,然后再编译OpenResty1.11.2.3+,看看能否出现这个错误。

在 2017年8月22日星期二 UTC+8下午1:33:51,chro...@gmail.com写道:

chro...@gmail.com

unread,
Aug 22, 2017, 2:00:29 AM8/22/17
to openresty
尝试重新安装了最新版的luajit(在OpenResty之外),错误不再出现了。
但感觉这个也算是个bug,希望agentzh能够解决。

在 2017年8月22日星期二 UTC+8下午1:38:59,chro...@gmail.com写道:

Yichun Zhang (agentzh)

unread,
Aug 22, 2017, 2:11:13 PM8/22/17
to openresty
Hello!

2017-08-21 23:00 GMT-07:00 <chro...@gmail.com>:
> 尝试重新安装了最新版的luajit(在OpenResty之外),错误不再出现了。
> 但感觉这个也算是个bug,希望agentzh能够解决。
>

这不是 bug。标准的 lua-cjson 使用了 Lua 5.0 的 C API,最新的 LuaJIT 移除了 Lua 5.0 的老 C
API。我上面已经解释得很清楚了。你安装的“最新版的 luajit”应该不是最新的 git 仓库里的 v2.1
分支吧?所以应该并不是真正意义上的最新版。

Regards,
Yichun

chro...@gmail.com

unread,
Aug 22, 2017, 10:06:57 PM8/22/17
to openresty
搞清楚原因了,是我的nginx(用的旧版luajit)调用了OpenResty的cjson.so,导致无法找到lua_sefunc接口,不是OpenResty的问题。
归根结底还是环境的原因,系统里安装了太多版本的nginx,比较混乱,如果只有一个OpenResty就不会有这个问题。

感谢agentzh的认真解答。

在 2017年8月23日星期三 UTC+8上午2:11:13,agentzh写道:

Zoom.Quiet

unread,
Aug 22, 2017, 11:25:19 PM8/22/17
to openresty
2017-08-23 10:06 GMT+08:00 <chro...@gmail.com>:
> 搞清楚原因了,是我的nginx(用的旧版luajit)调用了OpenResty的cjson.so,导致无法找到lua_sefunc接口,不是OpenResty的问题。
> 归根结底还是环境的原因,系统里安装了太多版本的nginx,比较混乱,如果只有一个OpenResty就不会有这个问题。
>

是时候来一发 ngxvm 了
和 nvm/pyenv/rvm 等等类似, 多版本工作环境管理工具了

> 感谢agentzh的认真解答。
>
> 在 2017年8月23日星期三 UTC+8上午2:11:13,agentzh写道:
>>
>> Hello!
>>
>> 2017-08-21 23:00 GMT-07:00 <chro...@gmail.com>:
>> > 尝试重新安装了最新版的luajit(在OpenResty之外),错误不再出现了。
>> > 但感觉这个也算是个bug,希望agentzh能够解决。
>> >
>>
>> 这不是 bug。标准的 lua-cjson 使用了 Lua 5.0 的 C API,最新的 LuaJIT 移除了 Lua 5.0 的老 C
>> API。我上面已经解释得很清楚了。你安装的“最新版的 luajit”应该不是最新的 git 仓库里的 v2.1
>> 分支吧?所以应该并不是真正意义上的最新版。
>>
>> Regards,
>> Yichun
>
> --
> --
> 邮件来自列表“openresty”,专用于技术讨论!
> 订阅: 请发空白邮件到 openresty...@googlegroups.com
> 发言: 请发邮件到 open...@googlegroups.com
> 退订: 请发邮件至 openresty+...@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



--
life is pathetic, go Pythonic! 人生苦短, Python当歌!
俺: http://zoomquiet.io
授: http://creativecommons.org/licenses/by-sa/2.5/cn/
怒: 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization learning!

Yichun Zhang (agentzh)

unread,
Aug 23, 2017, 3:11:06 PM8/23/17
to openresty
Hello!

2017-08-22 20:24 GMT-07:00 Zoom.Quiet:
>
> 是时候来一发 ngxvm 了
> 和 nvm/pyenv/rvm 等等类似, 多版本工作环境管理工具了
>

这是更多更诡异问题的开始……珍爱生命,远离同一系统内的多版本软件管理,哈哈!

Regards,
Yichun

碧城林

unread,
Dec 4, 2018, 3:45:04 AM12/4/18
to openresty
大家注意一下,which nginx ,是不是启动的时候没有用到openresty里面的nginx


在 2017年4月25日星期二 UTC+8上午10:17:31,chro...@gmail.com写道:
Hi,
Reply all
Reply to author
Forward
0 new messages