简体   繁体   中英

Module socket not found lua

I am trying to use lua to access redis values from nginx. When i execute lua files on command line there everything is ok i am able to read and write values to redis. But i when try to execute the same files from nginx by accessing a location in which access_by_lua directive is written the following error logged in error log file

no field package.preload['socket'] 
no file '/home/sivag/redis/redis-lua/src/socket.lua'
no file 'src/socket.lua'
no file '/home/sivag/lua/socket.lua'
no file '/opt/openresty/lualib/socket.so'
no file './socket.so'
no file '/usr/local/lib/lua/5.1/socket.so'
no file '/opt/openresty/luajit/lib/lua/5.1/socket.so'
no file '/usr/local/lib/lua/5.1/loadall.so'

What is the reason for this and how can i resolve this?

在我的例子中,我只需要安装lua-socket包,因为 socket 库不像其他语言那样内置在默认的 Lua 安装中。

You get this error because your code executes the command require("socket") This command will search for a file with that name in several directories. If successful the content will be executed as Lua code. If it is not successful you'll end up with your error message.

In order to fix this you have to add the path containing the file either to the system variable LUA_PATH or you have to add it to the global table package.path befor you require the file. Lua will replace ? with the name you give to require()

For example

package.path = package.path .. ";" .. thisPathContainsTheLuaFile .. "?.lua"

Please read:

http://www.lua.org/manual/5.3/manual.html#pdf-require

https://www.lua.org/pil/8.1.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM