已安装OpenResty-1.13.6.1,如何集成resty.http模块?
嗨,我来帮你搞定把resty.http模块加到你那台OpenResty 1.13.6.1里的事儿~下面给你两种靠谱的方法,按需选择就行:
给OpenResty 1.13.6.1集成
resty.http模块的方法 方法一:用OpenResty自带的opm包管理器(最省心)
OpenResty自带的opm工具可以一键安装官方维护的模块,步骤超简单:
- 先切换到root权限(或者命令前加
sudo),确保有足够权限操作:sudo su - 直接执行安装命令,
resty.http对应的官方包是openresty/lua-resty-http:opm get openresty/lua-resty-http - 安装完成后,opm会自动把模块文件放到OpenResty的Lua库路径(
/usr/local/openresty/lualib/resty/),不用额外配置。你可以验证下文件是否存在:
能看到这个文件就说明安装成功啦。ls /usr/local/openresty/lualib/resty/http.lua
方法二:手动下载安装(适合opm用不了的情况)
如果你的OpenResty版本太老导致opm无法正常工作,或者你想手动控制安装细节,就用这个方法:
- 先获取
lua-resty-http的v0.15版本源码包(这个版本和OpenResty 1.13.6.1兼容性较好),可以通过git克隆指定标签,或者手动下载压缩包后上传到服务器:git clone --branch v0.15 git@github.com:openresty/lua-resty-http.git - 进入源码目录(如果是压缩包要先解压):
cd lua-resty-http - 把模块文件复制到OpenResty的Lua库路径下:
cp lib/resty/http.lua /usr/local/openresty/lualib/resty/ cp lib/resty/http/*.lua /usr/local/openresty/lualib/resty/http/ - 同样验证文件是否存在:
ls /usr/local/openresty/lualib/resty/http.lua
验证模块是否能正常使用
安装完之后,咱们写个简单的测试来确认:
- 打开OpenResty的nginx配置文件(比如
/usr/local/openresty/nginx/conf/nginx.conf),添加一个测试用的location:location /test-http-module { content_by_lua_block { local http = require "resty.http" local httpc = http.new() local res, err = httpc:request_uri("http://example.com", { method = "GET", headers = { ["Content-Type"] = "application/json", } }) if not res then ngx.say("请求失败: ", err) return end ngx.say("请求成功,状态码: ", res.status) httpc:close() } } - 重启OpenResty让配置生效:
/usr/local/openresty/nginx/sbin/nginx -s reload - 访问
http://你的服务器IP/test-http-module,如果能看到“请求成功,状态码: 200”,就说明模块正常工作啦。
内容的提问来源于stack exchange,提问作者lonecoder




