openresty 흐름 제한 (redis + lua) 메모

2266 단어 nginx
githunb 에서 lua - resty - limit - traffic 다운로드 
/ usr / openv / server / lualib / restylimit 디 렉 터 리 에 압축 을 풀 었 습 니 다.server 는 openresty 를 위해 디 렉 터 리 를 설치 합 니 다.
/ usr / openv / server / nginx / conf / lua 디 렉 터 리
새 limit. lua 
local limit_req = require "resty.limit.req"
   local redis = require "resty.redis"
   local instance = redis.new();
    instance.connect(instance,'127.0.0.1','6379')
   local rate =tonumber(instance:get("limitrate"))
    local burst =tonumber(instance:get("limitburst"))
    --local rate=3       redis       rate=3    
    --local burst =10
    local error_status = 503
   local nodelay = false
   local lim, err = limit_req.new("limit_req_store", rate, burst)
  if not lim then
       ngx.exit(error_status)
   end
   
   local key = ngx.var.binary_remote_addr
  
   local delay, err = lim:incoming(key, true)
   
   if not delay and err == "rejected" then
       ngx.exit(error_status)
   end
   
   if delay > 0 then
       if nodelay then
   
       else
           ngx.sleep(delay)
      end
   end

/usr/openv/servers/nginx/conf
lua.conf
 server { 
    listen 80; 
    server_name _; 


 location /limit {
              access_by_lua_file conf/lua/limit.lua;
              
               default_type 'text/html';
                   lua_code_cache off;
                   content_by_lua 'ngx.say("hello world")';
                #   proxy_pass http://proxy/_cat/master;
                #     proxy_redirect  off;
            #   proxy_set_header        Host    $http_host;
          # proxy_set_header        X-Real-IP       $remote_addr;
          #  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          #     proxy_set_header   Cookie $http_cookie;
 
 }
}


/usr/openv/servers/nginx/conf
nginx. conf 에서 증가:
 http {
      include       mime.types;
 include lua.conf;
 default_type  application/octet-stream;
 lua_package_path "/usr/servers/lualib/?.lua;;";
 lua_package_cpath "/usr/servers/lualib/?.so;;";
 lua_shared_dict limit_req_store 10m;

좋은 웹페이지 즐겨찾기