luarocks: Lua 包管理工具

发布时间: 更新时间: 总字数:608 阅读时间:2m 作者: IP上海 分享 网址

LuaRocks 是 Lua 编程语言的程序包管理器,它提供了分发 Lua 模块的标准格式,易于管理 rock 安装的工具以及用于分发它们的服务器

国内代理

  • luarocks.cn 是国内加速网站,使用 --server https://luarocks.cn 指定国内源,示例
$ luarocks install apisix --server https://luarocks.cn
$ luarocks install kong --server https://luarocks.cn

使用

安装 lua-casbin

/usr/local/openresty/luajit/bin/luarocks install lrexlib-pcre PCRE_DIR=/usr/local/openresty/pcre
GIT_SSL_NO_VERIFY=1 /usr/local/openresty/luajit/bin/luarocks install casbin

安装 resty-http

luarocks install lua-resty-http

使用示例(现象类似于 curl --resolve 的效果):

resty-http-get-demo ...
-- 加载 lua-resty-http 库
local http = require "resty.http"
local httpc = http.new()

-- 设置目标 URL
local url = "https://xiexianbin.cn"

-- 解析主机名和端口
local host = "xiexianbin.cn"
local port = 443

-- 建立 TCP 连接
local ok, err = httpc:connect({
    scheme = "https",
    host = host,
    port = port,
    ssl_verify = false  -- 禁用 SSL 验证(仅用于测试,生产环境应启用)
})

if not ok then
    ngx.say("failed to connect: ", err)
    return
end

-- 发送 HTTP 请求
local res, err = httpc:request({
    path = "/",
    headers = {
        ["Host"] = host,
        ["User-Agent"] = "lua-resty-http"
    }
})

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

-- 读取响应体
local body, err = res:read_body()
if not body then
    ngx.say("failed to read body: ", err)
    return
end

-- 关闭连接
local ok, err = httpc:close()
if not ok then
    ngx.say("failed to close: ", err)
    return
end

-- 输出响应状态码和内容
ngx.say("Status: ", res.status)
ngx.say("Body length: ", #body)
-- ngx.say("Body content: ", body)  -- 如果需要输出完整内容可以取消注释
resty-http-post-demo ...
local http = require "resty.http"
local httpc = http.new()

-- 目标 URL
local url = "https://xiexianbin.cn/api"
local host = "xiexianbin.cn"
local port = 443

-- 建立 HTTPS 连接(禁用 SSL 验证,仅测试用)
local ok, err = httpc:connect({
    scheme = "https",
    host = host,
    port = port,
    ssl_verify = false
})

if not ok then
    ngx.say("failed to connect: ", err)
    return
end

-- 构造 POST 请求数据(JSON 格式)
local post_data = {
    name = "John",
    age = 30
}
local json_body = require("cjson").encode(post_data)

-- 发送 POST 请求
local res, err = httpc:request({
    method = "POST",
    path = "/api",
    headers = {
        ["Host"] = host,
        ["Content-Type"] = "application/json",
        ["Content-Length"] = #json_body,
    },
    body = json_body
})

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

-- 读取响应
local body, err = res:read_body()
if not body then
    ngx.say("failed to read body: ", err)
    return
end

-- 关闭连接
local ok, err = httpc:close()
if not ok then
    ngx.say("failed to close: ", err)
    return
end

-- 输出结果
ngx.say("Status: ", res.status)
ngx.say("Response: ", body)

F&Q

module ‘resty.http’ not found

luarocks install lua-resty-http

failed to load module resty.openssl.*, mTLS isn’t supported without lua-resty-openssl

luarocks install lua-resty-openssl

lua ssl certificate verify error: (20: unable to get local issuer certificate)

  • nginx.conf 中配置(参考
lua_ssl_verify_depth 2;
lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.pem;
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数