nginx 实现带burst的并发连接数控制

栏目: Lua · 发布时间: 7年前

内容简介:nginx 实现带burst的并发连接数控制

先安装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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web标准和SEO应用实践

Web标准和SEO应用实践

Aarron Walter / 李清 / 机械工业出版社 / 2008 / 36.00元

本书是关于搜索引擎优化和易发现性的技术指南。. 本书介绍Web标准、可访问性以及Ajax、API、Flash和微格式等内容,包括标记策略、服务器端策略、内容策略、建构易发现的博客、在网站内添加搜索、防止易发现性障碍、用邮件列表挽回流量、将易发现性付诸实践。 本书适合网站开发者与SEO技术业余爱好者等参考。 这不是为营销专家写的一本SEO的书。 针对那些想要找到网站的目标用户......一起来看看 《Web标准和SEO应用实践》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具