user root;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
lua_shared_dict limit_conn_store 100m;
server {
listen 9090;
server_name localhost;
location / {
access_by_lua_block {
local limit_conn = require "resty.limit.conn"
local lim, err = limit_conn.new("limit_conn_store", 7, 1, 0.5)
if not lim then
ngx.log(ngx.ERR,"failed to instantiate a resty.limit.conn object: ", err)
return ngx.exit(500)
end
ngx.log(ngx.INFO,err)
-- ngx.log(ngx.INFO, "lim.max ", lim.max)
local key = ngx.var.binary_remote_addr
local delay, err = lim:incoming(key, true)
ngx.log(ngx.INFO,"delay", delay)
ngx.log(ngx.INFO,"err", rr)
if not delay then
if err == "rejected" then
return ngx.exit(503)
end
ngx.log(ngx.ERR, "failed to limit req: ", err)
return ngx.exit(500)
end
if lim:is_committed() then
local ctx = ngx.ctx
ctx.limit_conn = lim
ctx.limit_conn_key = key
ctx.limit_conn_delay = delay
end
local conn = err
if delay >= 0.001 then
ngx.log(ngx.WARN, "delaying conn, excess ", delay, "s per binary_remote_addr by limit_conn_store")
ngx.sleep(delay)
end
}
log_by_lua_block {
local ctx = ngx.ctx
local lim = ctx.limit_conn
if lim then
local latency = tonumber(ngx.var.request_time) - ctx.limit_conn_delay
local key = ctx.limit_conn_key
assert(key)
local conn, err = lim:leaving(key, latency)
ngx.log(ngx.INFO,"conn_1", conn)
ngx.log(ngx.INFO,"err_1", err)
if not conn then
ngx.log(ngx.ERR,
"failed to record the connection leaving ",
"request: ", err)
end
end
}
proxy_pass http://10.128.3.68;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
发现:
- 重启nginx之后,第一次用15并发请求,会返回8个503,但是第二次用15个并发在请求,发现返回的都是200,之后都是返回200
- 是不是配置文件错误