Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 如何从nginx中的lua访问套接字?_Sockets_Nginx_Lua - Fatal编程技术网

Sockets 如何从nginx中的lua访问套接字?

Sockets 如何从nginx中的lua访问套接字?,sockets,nginx,lua,Sockets,Nginx,Lua,我已经从支持lua的源代码构建了nginx,并且能够运行如下服务器端脚本: http { lua_package_path '/usr/local/share/lua/5.1/?.lua;;'; server { listen 80; location /hi { content_by_lua ' ngx.header["Content-Type"] = "text/plain;charset=utf

我已经从支持lua的源代码构建了nginx,并且能够运行如下服务器端脚本:

http {
  lua_package_path '/usr/local/share/lua/5.1/?.lua;;';
  server {
    listen 80;    
    location /hi {
        content_by_lua '            
            ngx.header["Content-Type"] = "text/plain;charset=utf-8"
            ngx.say("Hello world!")
            --local s = require("socket")
            ngx.say(_VERSION);
        ';
    }
  }
}
因此,当我访问时,我得到以下输出:

Hello world!
Lua 5.1
如果我取消注释linelocal s=require(“socket”),那么我的浏览器中会出现以下错误:

Unable to load page
Problem occurred while loading the URL http://localhost/hi
Connection terminated unexpectedly
soucket.lua在此文件夹中显示:

root@debian:/usr/local/share/lua/5.1# ls
ltn12.lua  mime.lua  socket  socket.lua
更新:添加cpath无助于:

lua_package_cpath '/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/socket/?.so;;';

# ls /usr/local/lib/lua/5.1/socket/*.so
/usr/local/lib/lua/5.1/socket/core.so  /usr/local/lib/lua/5.1/socket/serial.so  /usr/local/lib/lua/5.1/socket/unix.so

# ls /usr/local/share/lua/5.1/
ltn12.lua  mime.lua  socket  socket.lua
如何修复/诊断此问题? Thanx

如果socket.lua是, 那么它需要socket/core.so

您需要指定lua\u包的路径

cpath用于已编译的共享库模块(.so),路径用于文本模块(.lua)

/usr/local/lib/lua/5.1/?.so-如果socket.lua是, 那么它需要socket/core.so

您需要指定lua\u包的路径

cpath用于已编译的共享库模块(.so),路径用于文本模块(.lua)


/usr/local/lib/lua/5.1/?。因此-cpath的最常见值

不要尝试在nginx中使用LuaSocket。LuaSocket是一个阻塞库,nginx是非阻塞库,因此您将遇到问题。退房吧。它的API与socket.tcp兼容,但它是非阻塞的。

不要尝试在nginx中使用LuaSocket。LuaSocket是一个阻塞库,nginx是非阻塞库,因此您将遇到问题。退房吧。它的API与socket.tcp兼容,但它是非阻塞的。

liar=)它在连接之前会进入非阻塞模式,并且它有设置O_非阻塞的方法。检查src/usocket.c:371和src/inet.c:421@marsgpl当然,您可以通过一些努力使用LuaSocket编写非阻塞代码,我应该说“大多数API在默认情况下是阻塞的”。cosocket API的设计使代码看起来像“普通”(阻塞)LuaSocket代码,但实际上是非阻塞的。此外,它不需要额外的模块,因为它是Lua nginx模块的本机。@catwell在许多情况下,实际上您无法通过_Lua*访问像init_worker_中那样的LuaSocket,在这些情况下,您必须独立于ngx.socket.liar=)它在连接之前进入非块模式,并且它有设置O_非块的方法。检查src/usocket.c:371和src/inet.c:421@marsgpl当然,您可以通过一些努力使用LuaSocket编写非阻塞代码,我应该说“大多数API在默认情况下是阻塞的”。cosocket API的设计使代码看起来像“普通”(阻塞)LuaSocket代码,但实际上是非阻塞的。此外,它不需要额外的模块,因为它是Lua nginx模块的本机。@catwell在许多情况下,实际上您无法通过_Lua*访问init_worker_中的LuaSocket,在这些情况下,您必须独立于ngx.socket。