OpenResty Nginx + Lua 平台
OpenResty 基于 Nginx 和 LuaJIT,在 Nginx 配置中嵌入 Lua 处理请求、鉴权、限流和网关逻辑。命令与 Nginx 基本兼容。
openresty -t
openresty -s reload
openresty -V
resty -e 'print(ngx.version)'
resty -I ./lua -e 'local m = require("app"); print(m.hello())'
配置:
location /hello {
default_type 'text/plain';
content_by_lua_block {
ngx.say('hello from openresty')
}
}
location /auth {
access_by_lua_block {
local token = ngx.req.get_headers()['Authorization']
if not token then
return ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
}
proxy_pass http://backend;
}
lua_shared_dict,注意容量和过期。lua_package_path 要配置正确;模块找不到时看 error.log。content_by_lua_block 与 proxy_pass 不能随意混用,按阶段选择。openresty — Nginx + Luanginx — 配置语法resty — Lua 命令行合上页面,先回答,再展开对照。
content_by_lua_block 里随意混用 proxy_pass 为什么不对?lua_shared_dict,注意容量和过期。