openresty的lua_package_path


  • 文檔

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依次替換到尋找文件,全部尋找完畢還找不到就報錯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM