openresty的lua_package_path

本文介绍如何通过lua_package_path配置OpenResty的Lua文件搜索路径,并举例说明了如何使用require加载指定的Lua模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 文档

lua_package_path可以配置openresty的文件寻址路径。官网文档如下:

 # 设置纯 Lua 扩展库的搜寻路径(';;' 是默认路径):
 lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';

 # 设置 C 编写的 Lua 扩展模块的搜寻路径(也可以用 ';;'):
 lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';

然后require的字符串就会替换对应的问号?,一个文件就会去/foo/bar/下面寻找。

  • example

在代码中require "controller.test",会依次根据package.path匹配对应的lua文件。即替换掉对应的问号。(在lapis框架中,在框架的根目录中创建一个文件夹名字叫controllers,写一个文件test.lua,可以正常输出,改为controller,找不到对应的文件夹,打开日志,查看openresty的寻找方式)

首先输出package.path:

/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1.0-beta2/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua

在log中查看:

    no field package.preload['controller.test']
    no file '/usr/local/openresty/site/lualib/controller/test.lua'
    no file '/usr/local/openresty/site/lualib/controller/test/init.lua'
    no file '/usr/local/openresty/lualib/controller/test.lua'
    no file '/usr/local/openresty/lualib/controller/test/init.lua'
    no file './controller/test.lua'
    no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta2/controller/test.lua'
    no file '/usr/local/share/lua/5.1/controller/test.lua'
    no file '/usr/local/share/lua/5.1/controller/test/init.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/controller/test.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/controller/test/init.lua'
    no file '/usr/local/openresty/site/lualib/controller/test.so'
    no file '/usr/local/openresty/lualib/controller/test.so'
    no file './controller/test.so'
    no file '/usr/local/lib/lua/5.1/controller/test.so'
    no file '/usr/local/openresty/luajit/lib/lua/5.1/controller/test.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
    no file '/usr/local/openresty/site/lualib/controller.so'
    no file '/usr/local/openresty/lualib/controller.so'
    no file './controller.so'
    no file '/usr/local/lib/lua/5.1/controller.so'
    no file '/usr/local/openresty/luajit/lib/lua/5.1/controller.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'

openresty根据package.path依次替换到寻找文件,全部寻找完毕还找不到就报错。

转载于:https://www.cnblogs.com/mentalidade/p/6958326.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值