如何利用Nginx+Lua实现动态限频?有没有完整的代码?

提问者:帅平 问题分类:运维
如何利用Nginx+Lua实现动态限频?有没有完整的代码?
1 个回答
ろ身
ろ身
代码示例如下:
lua_shared_dict limit_counter 10m;
server {
    location / {
        access_by_lua_block {
            local limit_counter = ngx.shared.limit_counter
            local key = ngx.var.binary_remote_addr
            local req,_ = limit_counter:get(key)
            if req then
                if req > 100 then  # 每秒100次阈值
                    ngx.exit(503)
                else
                    limit_counter:incr(key,1)
                end
            else
                limit_counter:set(key,1,1)  # 过期时间1秒
            end
        }
    }
}
发布于:2周前 (03-13) IP属地:四川省
我来回答