先安装openresty,直接去官网下载https://openresty.org。
lua插件https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/conn.md
conf/conf.d/xxx.com.conf
lua_shared_dict limit_conn_store 8m; server { listen 8080; server_name xxx.com; root /srv/www/xxx/public; include php; }
conf/php
if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } location ~* \.php($|/) { access_by_lua_file lua/conn_access.lua; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SERVER_NAME $server_name; fastcgi_param PATH_INFO $fastcgi_script_name; log_by_lua_file lua/conn_log.lua; }
lua/conn_access.lua
-- well, we could put the require() and new() calls in our own Lua -- modules to save overhead. here we put them below just for -- convenience. local limit_conn = require "resty.limit.conn" -- limit the requests under 200 concurrent requests (normally just -- incoming connections unless protocols like SPDY is used) with -- a burst of 100 extra concurrent requests, that is, we delay -- requests under 300 concurrent connections and above 200 -- connections, and reject any new requests exceeding 300 -- connections. -- also, we assume a default request time of 0.5 sec, which can be -- dynamically adjusted by the leaving() call in log_by_lua below. local lim, err = limit_conn.new("limit_conn_store", 1, 30, 0.01) if not lim then ngx.log(ngx.ERR, "failed to instantiate a resty.limit.conn object: ", err) return ngx.exit(500) end -- the following call must be per-request. -- here we use the remote (IP) address as the limiting key local key = ngx.var.binary_remote_addr local delay, err = lim:incoming(key, true) 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 -- the 2nd return value holds the current concurrency level -- for the specified key. local conn = err if delay >= 0.001 then -- the request exceeding the 200 connections ratio but below -- 300 connections, so -- we intentionally delay it here a bit to conform to the -- 200 connection limit. -- ngx.log(ngx.WARN, "delaying") ngx.sleep(delay) end
lua/conn_log.lua
local ctx = ngx.ctx local lim = ctx.limit_conn if lim then -- if you are using an upstream module in the content phase, -- then you probably want to use $upstream_response_time -- instead of ($request_time - ctx.limit_conn_delay) below. 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) if not conn then ngx.log(ngx.ERR, "failed to record the connection leaving ", "request: ", err) return end end