找不到模块“resty.http”:安装了resty

问题描述

我在使用 resty cli 测试示例代码时遇到问题。 请查找相关命令的输出

$ which resty
/usr/local/bin/resty
$ which openresty
/usr/local/bin/openresty
$ resty -e 'ngx.say("hello world")'               
hello world

文件:test.lua

local http = require "resty.http"
local httpc = http.new()
local res,err = httpc:request_uri("http://example.com/helloworld",{
  method = "POST",body = "a=1&b=2",headers = {
    ["Content-Type"] = "application/x-www-form-urlencoded",},keepalive_timeout = 60000,keepalive_pool = 10
})

if not res then
  ngx.say("Failed to request: ",err)
  return
end

在使用 resty cli 运行此 test.lua 文件时,我遇到了找不到模块“resty.http”的问题:

$ resty test.lua                                    
ERROR: test.lua:1: module 'resty.http' not found:
        no field package.preload['resty.http']
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http/init.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http/init.ljbc'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http/init.lua'
        no file './resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
        no file '/usr/local/share/lua/5.1/resty/http.lua'
        no file '/usr/local/share/lua/5.1/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/lua/5.1/resty/http.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/share/lua/5.1/resty/http/init.lua'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty/http.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.so'
        no file './resty/http.so'
        no file '/usr/local/lib/lua/5.1/resty/http.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/lib/lua/5.1/resty/http.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/site/lualib/resty.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/usr/local/Cellar/openresty/1.19.3.1_1/luajit/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        test.lua:1: in function 'file_gen'
        init_worker_by_lua:45: in function <init_worker_by_lua:43>
        [C]: in function 'xpcall'
        init_worker_by_lua:52: in function <init_worker_by_lua:50>

我也试过 Install resty.http with already installed openresty

解决方法

我使用 Install resty.http with already installed openresty 中提到的相同解决方案解决了这个问题。

第一次,由于我的休息路径,它对我不起作用。 但是当我查看错误日志时,我才知道它在 http.lua 处寻找 /usr/local/Cellar/openresty/1.19.3.1_1/lualib/resty/http.lua 所以复制 https://github.com/ledgetech/lua-resty-http/blob/master/lib/resty/http.lua 对我有用。

感谢Alexander Altshuler提供original answer

,

该库默认与 Kong 一起提供。使用 LUA_PATH 环境变量调整 Lua 路径设置,而不是复制文件。这可以使用 LuaRocks 来完成(Kong 也默认包含)

要使用 LuaRocks 设置路径,请尝试:

eval `luarocks path`