```
location / {
access_by_lua_block {
local limit_dict = ngx.shared.my_limit_dict
local ua = ngx.var.http_user_agent
local limit_key = "ua:" .. ngx.md5(ua)
local limit_count = limit_dict:get(limit_key) or 0
-- 设置速度限制,例如每秒最多 5 个请求
local limit_rate = 5
if limit_count > limit_rate then
ngx.exit(ngx.HTTP_TOO_MANY_REQUESTS)
else
limit_dict:incr(limit_key, 1)
end
}
# 其他处理逻辑...
}
```