# lua-resty-limit-req
lua-resty-limit-req - Limit the request processing rate between multiple NGINX instances
## Synopsis
lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";
lua_package_path "/path/to/lua-resty-limit-req/lib/?.lua;;";
server {
listen 9090;
location /t {
access_by_lua '
local req = require "resty.limit.req"
local ok = req.limit{ key = ngx.var.remote_addr, zone = "one",
rate = "2r/s", interval = 2, log_level = ngx.NOTICE,
rds = { host = "127.0.0.1", port = 6379 }}
if not ok then
return ngx.exit(503)
end
';
echo Logged in;
}
}
----
最近参考 ngx_http_limit_req_module 写了一个 ngx_lua 的版本,在此分享给大家 :-)
特别地,利用了 redis 作为后端状态存储服务,这样就能使得多个 NGINX 实例之间能够共享和维护这些状态,相当与 ngx_limit_req 的集群版本。
项目地址:https://github.com/timebug/lua-resty-limit-req