lua_package_path 疑问

1,436 views
Skip to first unread message

徐恒

unread,
Mar 4, 2014, 1:14:23 AM3/4/14
to open...@googlegroups.com
我在我的一个虚拟主机文件里面调用了
/usr/local/nginx/lua/common.lua  这个lua脚本,内容为
local mdaxue = require "resty.mdaxue"
mdaxue.common()
结果报错
2014/03/04 13:49:39 [error] 12537#0: *53 lua entry thread aborted: runtime error: /usr/local/nginx/lua/common.lua:1: module 'resty.mdaxue' not found:
no field package.preload['resty.mdaxue']
no file '/usr/local/nginx/conf/waf/resty/mdaxue.lua'
no file '/usr/local/openresty/lualib/resty/mdaxue.so'
no file './resty/mdaxue.so'
no file '/usr/local/lib/lua/5.1/resty/mdaxue.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/mdaxue.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/usr/local/openresty/lualib/resty.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
coroutine 0:
[C]: in function 'require'
/usr/local/nginx/lua/common.lua:1: in function </usr/local/nginx/lua/common.lua:1>, client: 127.0.0.1,
我这个mdaxue模块是放在/usr/local/openresty/lualib/resty/mdaxue.lua这里,
然后如果我在nginx的http块里面加上lua_package_path "/usr/local/openresty/lualib/resty/?.lua;;"; 就可以了。
但我在另外一个机器上面同样的方式装的openresty 却不需要加这句也可以找到这个模块。我不知道到底是哪里配置出了错误,
/usr/local/openresty/lualib/resty/ 这里面的所有lua脚本不是可以直接require的吗?

Yichun Zhang (agentzh)

unread,
Mar 4, 2014, 5:05:18 PM3/4/14
to openresty
Hello!

2014-03-03 22:14 GMT-08:00 徐恒:
> 2014/03/04 13:49:39 [error] 12537#0: *53 lua entry thread aborted: runtime
> error: /usr/local/nginx/lua/common.lua:1: module 'resty.mdaxue' not found:
> no field package.preload['resty.mdaxue']
> no file '/usr/local/nginx/conf/waf/resty/mdaxue.lua'
> no file '/usr/local/openresty/lualib/resty/mdaxue.so'
> no file './resty/mdaxue.so'

从错误消息上看,应该是你先前自己定义的 lua_package_path 指令没有包含默认的搜索路径。我猜你原先是这么写的:

lua_package_path "/usr/local/nginx/conf/waf/?.lua";

此时不会包含默认的搜索路径。而你应当这么写:

lua_package_path "/usr/local/nginx/conf/waf/?.lua;;";

注意字符串最后的“;;”

> 然后如果我在nginx的http块里面加上lua_package_path
> "/usr/local/openresty/lualib/resty/?.lua;;"; 就可以了。

你这里的 /usr/local/openresty/lualib/resty/?.lua 这一部分其实是错的,因为 require
'resty.foo' 的时候会尝试路径
/usr/local/openresty/lualib/resty/resty/foo.lua,注意 resty
出现了两次。你新的配置真正起作用的应当是字符串值末尾的那两个分号。

Regards,
-agentzh

Gavin Shang

unread,
Mar 12, 2014, 11:25:48 PM3/12/14
to open...@googlegroups.com
我也遇到这个问题,错误提示也差不多:
 
2014/03/13 11:09:59 [error] 9770#0: *1 lua entry thread aborted: runtime error: [string "content_by_lua"]:2: module 'resty.ssdb' not found:
no field package.preload['resty.ssdb']
no file '/opt/nginx/conf.d/lib/resty/ssdb.lua'
no file './resty/ssdb.lua'
no file '/usr/local/share/luajit-2.0.2/resty/ssdb.lua'
no file '/usr/local/share/lua/5.1/resty/ssdb.lua'
no file '/usr/local/share/lua/5.1/resty/ssdb/init.lua'
no file './resty/ssdb.so'
no file '/usr/local/lib/lua/5.1/resty/ssdb.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
coroutine 0:
[C]: in function 'require'
[string "content_by_lua"]:2: in function <[string "content_by_lua"]:1>, client: 10.15.62.158, server: spdy.gw.com.cn, request: "GET /smsreq?mobile=13818463800&test=yes HTTP/1.1", host: "10.15.107.157"


但是  文件明明存在,权限有意设置成 777:
#  ll /opt/nginx/conf.d/lib/resty/ssdb.lua
-rwxrwxrwx 1 root root 6305 Mar 12 16:58 /opt/nginx/conf.d/lib/resty/ssdb.lua

配置是:
 lua_package_path "/opt/nginx/conf.d/lib/?.lua;;";

nginx 本版和参数是:
# sbin/nginx -V
nginx version: nginx/1.4.6
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
TLS SNI support enabled
configure arguments: --with-debug --prefix=/opt/nginx --user=nobody --group=nobody --http-client-body-temp-path=temp/client_body/ --http-proxy-temp-path=temp/proxy/ --http-fastcgi-temp-path=temp/fastcgi/ --http-scgi-temp-path=temp/scgi/ --http-uwsgi-temp-path=temp/uwsgi --with-http_image_filter_module --with-google_perftools_module --with-http_ssl_module --with-http_spdy_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-pcre=../pcre-8.33 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1e --add-module=../nginx-push-stream-module-0.4.0 --add-module=../echo-nginx-module-0.49 --add-module=../lua-nginx-module-0.9.2 --add-module=../ngx_cache_purge-2.1 --add-module=../ngx_devel_kit-0.2.19 --add-module=../set-misc-nginx-module-0.22 --add-module=../headers-more-nginx-module-0.23 --add-module=../ngx_txid-master


始终不知道哪里出了问题,求帮助。。。

Yichun Zhang (agentzh)

unread,
Mar 12, 2014, 11:33:16 PM3/12/14
to openresty
Hello!

On Wed, Mar 12, 2014 at 8:25 PM, Gavin Shang wrote:
> 我也遇到这个问题,错误提示也差不多:
[...]
>> no file '/opt/nginx/conf.d/lib/resty/ssdb.lua'
>
> 但是 文件明明存在,权限有意设置成 777:
> # ll /opt/nginx/conf.d/lib/resty/ssdb.lua
> -rwxrwxrwx 1 root root 6305 Mar 12 16:58
> /opt/nginx/conf.d/lib/resty/ssdb.lua
>

这里光检查文件本身的权限是不够的,因为在打开一个文件时还需要访问其路径上的各级目录。你需要确保这些目录(比如
/opt/nginx/conf.d/lib/resty/、/opt/nginx/conf.d/lib/、/opt/nginx/conf.d/
等等)对于 nginx worker 所使用的系统帐户同时都有可执行权限 :)

Regards,
-agentzh

Lynn Chen

unread,
Jan 25, 2016, 9:52:42 PM1/25/16
to openresty
nginx 安装目录:/usr/local/nginx
nginx.conf 配置了: lua_package_path 'lua/lib/?.lua;;';
模块位置: /usr/local/nginx/lua/lib/test.lua

local test = require "test"

同样遇到了类似的问题,把  lua_package_path 'lua/lib/?.lua;;'; 改成了  lua_package_path '/usr/local/nginx/lua/lib/?.lua;;'; 后问题解决。

疑问是:写成相对路径时,到底是在哪个目录下查找模块的呢?

Yichun Zhang (agentzh)

unread,
Jan 27, 2016, 3:14:07 PM1/27/16
to openresty
Hello!

2016-01-25 18:52 GMT-08:00 Lynn Chen:
> 同样遇到了类似的问题,把 lua_package_path 'lua/lib/?.lua;;'; 改成了 lua_package_path
> '/usr/local/nginx/lua/lib/?.lua;;'; 后问题解决。
>
> 疑问是:写成相对路径时,到底是在哪个目录下查找模块的呢?
>

默认是使用的是 nginx 启动时的当前工作目录。当然,也可以通过 working_directory 指令在 nginx.conf 里面覆盖:

http://nginx.org/en/docs/ngx_core_module.html#working_directory

推荐在 lua_package_path 和 lua_package_cpath 指令里面总是使用 $prefix
这个特殊变量,这样可以相对于 nginx 的 server prefix 路径(可通过 nginx 命令行的 -p 选项显式指定值)。

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