openresty

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;
}

易错点与排错

相关命令

读完自测

合上页面,先回答,再展开对照。

  1. OpenResty 吞吐下降但 CPU 不高,先怀疑什么?
  2. 多个 worker 之间共享限流计数用什么?
  3. 在 content_by_lua_block 里随意混用 proxy_pass 为什么不对?
答案
  1. Lua 中是否有同步网络/文件等阻塞操作,它会阻塞 Nginx worker。
  2. lua_shared_dict,注意容量和过期。
  3. 两者不能随意混用,要按阶段选择。