通过lua脚本自定义nginx路由规则,如:通过请求参数路由至不同集群,实现冷热流量隔离。
homebrew
openresty 继承了LuaJit 模块,后续编译lua模块到nginx的时候需要用到。
61brew install openresty
2# 执行成功后安装路径在:/opt/homebrew/Cellar/openresty/
3# 目前的最新版本是:1.21.4.2_1
4# 需要配置以下环境变量 用于编译lua模块
5export LUAJIT_LIB="/opt/homebrew/Cellar/openresty/1.21.4.2_1/luajit/lib"
6export LUAJIT_INC="/opt/homebrew/Cellar/openresty/1.21.4.2_1/luajit/include/luajit-2.1"
以上三个下载后解压备用
这两个需要手动 make install 一下(可能需要root权限)
11sudo make install
我的安装路径是
11/usr/local/lib/lua
91# 配置时需要添加 lua-nginx-module 和 ngx_devel_kit 对应模块路径
2./configure \
3--prefix=/path/to/nginx-1.21.6/dist \
4--with-http_stub_status_module \
5--with-pcre=/path/to/pcre-8.45 \
6--add-module=/path/to/lua-nginx-module-0.10.22 \
7--add-module=/path/to/ngx_devel_kit-0.3.2
8
9make && make install
在 http 块里配置:
21# 3.2 里的路径
2lua_package_path "/usr/local/lib/lua/?.lua;;";
41location /hello {
2 default_type 'text/plain';
3 content_by_lua 'ngx.say("hello, lua")';
4}