Unavailable *.lua from init_by_lua*

474 views
Skip to first unread message

Veniamin Gvozdikov

unread,
Oct 11, 2013, 11:10:58 AM10/11/13
to openre...@googlegroups.com
I have troubles with libs from init_by_lua*. If I load a lib from *.lua file and try use that from content_by_lua* object will nil. Also I tried save object to shared It doesn't work too but It only works with *.so files. How can I use init_by_lua* with *.lua?

Yichun Zhang (agentzh)

unread,
Oct 11, 2013, 2:30:22 PM10/11/13
to openresty-en
Hello!
It should work.

Consider the following minimal example tested successfully on my side:

http {

lua_package_path "$prefix/html/?.lua;;";
init_by_lua '
foo = require "foo"
';

server {
listen 8080;

location = /t {
content_by_lua '
foo.go()
';
}
}

} # http

And we put the following foo.lua file under html/ below the nginx server prefix:

-- foo.lua
local say = ngx.say
local _M = {}

function _M.go()
say("hello from foo")
end

return _M

And then reload nginx and access the /t location:

$ curl localhost:8080/t
hello from foo

Best regards,
-agentzh

Veniamin Gvozdikov

unread,
Oct 11, 2013, 3:08:05 PM10/11/13
to openre...@googlegroups.com
Hi, 

I tried your example and I had:

$ nginx -t
nginx: [alert] lua_code_cache is off; this will hurt performance in /Users/me/opt/openresty/nginx/conf/nginx.conf:35
nginx: the configuration file /Users/me/opt/openresty/nginx/conf/nginx.conf syntax is ok
nginx: [error] failed to run init_by_lua*: /Users/me/Devel/lib/foo.lua:6: no request object found
stack traceback:
[C]: in function 'say'
/Users/me/Deve/llib/foo.lua:6: in function 'go'
/Users/me/Devel/init.lua:6: in main chunk
nginx: configuration file /Users/me/opt/openresty/nginx/conf/nginx.conf test failed

$ cat ../lib/foo.lua
local say = ngx.say

local _M = {}

function _M.go()
    say("hello from foo")
end

return _M

    lua_code_cache off;
    lua_package_path '/Users/me/Devel/lib/?.lua;/Users/me/Devel/lib/?.luac;;';
    init_by_lua_file /Users/me/Devel/init.lua;

dmt:app $ head ../init.lua
foo = require "foo"
foo.go()

nginx version: ngx_openresty/1.4.2.9
built by clang 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
TLS SNI support enabled
configure arguments: --prefix=/Users/me/opt/openresty/nginx --with-cc-opt=-I/usr/local/include --add-module=../ngx_devel_kit-0.2.19 --add-module=../echo-nginx-module-0.48 --add-module=../xss-nginx-module-0.03rc9 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.22 --add-module=../form-input-nginx-module-0.07 --add-module=../encrypted-session-nginx-module-0.03 --add-module=../srcache-nginx-module-0.22 --add-module=../ngx_lua-0.9.0 --add-module=../headers-more-nginx-module-0.22 --add-module=../array-var-nginx-module-0.03rc1 --add-module=../memc-nginx-module-0.13 --add-module=../redis-nginx-module-0.3.6 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.12rc10 --add-module=../rds-csv-nginx-module-0.05rc2 --with-ld-opt='-Wl,-rpath,/Users/me/opt/openresty/luajit/lib -L/usr/local/lib' --with-pcre=/Users/me/Devel/ngx_openresty-1.4.2.9/pcre-8.33 --with-dtrace-probes --with-http_ssl_module

Yichun Zhang (agentzh)

unread,
Oct 11, 2013, 4:19:23 PM10/11/13
to openresty-en
Hello!

On Fri, Oct 11, 2013 at 12:08 PM, Veniamin Gvozdikov wrote:
> nginx: [error] failed to run init_by_lua*: /Users/me/Devel/lib/foo.lua:6: no
> request object found
> stack traceback:
> [C]: in function 'say'
> /Users/me/Deve/llib/foo.lua:6: in function 'go'
> /Users/me/Devel/init.lua:6: in main chunk

No, you're not trying my example. You're trying your example with
obvious programming mistakes.

You call foo.go() directly in the context of init_by_lua, which does
not make sense at all. Because foo.go() is generating a response via
ngx.say(). The error message above clearly indicates that there is no
request object associated with the current (init_by_lua) context
(init_by_lua runs in the nginx master process during nginx startup or
reload).

Note that I call foo.go() in the context of content_by_lua in my example.

Regards,
-agentzh

Veniamin Gvozdikov

unread,
Oct 11, 2013, 4:24:40 PM10/11/13
to openre...@googlegroups.com
Hello,


Thank you. I found my mistake! This is the best answer with details.
Reply all
Reply to author
Forward
0 new messages