nginx自定义token校验插件问题记录
怎么引用同级目录的自定义公共模块
方法 1:修改 lua_package_path 包含当前目录(推荐)
在 Nginx 配置 中添加以下指令:
http {
# 添加当前目录到 Lua 模块搜索路径
lua_package_path "/app/openresty/nginx/conf/fsbase-gateway-auth/?.lua;;";
server {
location /sfimplat {
content_by_lua_file /app/openresty/nginx/conf/fsbase-gateway-auth/file-gateway-auth.lua;
}
}
}
方法 2:在 Lua 代码中动态添加路径
在 file-gateway-auth.lua 的开头添加:
-- 获取当前脚本所在目录
local script_dir = string.match(debug.getinfo(1, "S").source:sub(2), "^.*/")
package.path = package.path .. ";" .. script_dir .. "?.lua"
-- 现在可以正确加载同目录的 tools.lua
local tools = require("tools")
- ?.lua:匹配任意 .lua 文件。
- ;;:保留默认的搜索路径
报错:module 'resty.http_headers' not found:
解决方案:完整安装 lua-resty-http
# 使用 opm 安装(推荐)
opm install ledgetech/lua-resty-http
# 或手动安装
wget https://github.com/ledgetech/lua-resty-http/archive/master.zip
unzip master.zip
cd lua-resty-http-master
cp -r lib/resty /usr/local/openresty/lualib/
报错: failed to load module resty.openssl.*, mTLS isn't supported without lua-resty-openssl:
安装 lua-resty-openssl
# 使用 opm 安装(推荐)
opm install fffonion/lua-resty-openssl
# 或手动安装
wget https://github.com/fffonion/lua-resty-openssl/archive/master.zip
unzip master.zip
cd lua-resty-openssl-master
cp -r lib/resty/openssl /usr/local/openresty/lualib/resty/
安装后依然报错,检查后发现lua-resty-openssl 模块需要 OpenSSL 1.1.1+ 才能正常运行(因为它依赖 OpenSSL_version_num 等新 API)。以下是完整的解决方案:
# 查看当前 OpenSSL 路径
which openssl # 通常为 /usr/bin/openssl
# 备份旧版本(谨慎操作)
mv /usr/bin/openssl /usr/bin/openssl.bak
# 安装 OpenSSL 1.1.1+
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -xzf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config --prefix=/usr/local/openssl-1.1.1w --openssldir=/usr/local/openssl-1.1.1w
make && make install
# 更新符号链接
ln -sf /usr/local/openssl-1.1.1w/bin/openssl /usr/bin/openssl
ldconfig
# 验证版本
openssl version # 应输出 OpenSSL 1.1.1w

浙公网安备 33010602011771号