• openresty 防DDOS攻击


    nginx配置

    nginx.conf:

        http{
        [......]
        lua_shared_dict limit 10m;
        lua_shared_dict jsjump 10m;
         
            server {
        #lua_code_cache off;
                listen       80;
                server_name  www.centos.bz;
         
                location / {
        default_type  text/html;
        content_by_lua_file "/usr/local/openresty/nginx/conf/lua";
                }
                location @cc {
                    internal;
                    root   html;
                    index  index.html index.htm;
                }
            }
        }
    

    /usr/local/openresty/nginx/conf/lua文件:

        local ip = ngx.var.binary_remote_addr
        local limit = ngx.shared.limit
        local req,_=limit:get(ip)
        if req then
                if req > 20 then
                        ngx.exit(503)
                else
                        limit:incr(ip,1)
                end
        else
                limit:set(ip,1,10)
        end
         
        local jsjump = ngx.shared.jsjump
        local uri = ngx.var.request_uri
        local jspara,flags=jsjump:get(ip)
        local args = ngx.req.get_uri_args()
        if jspara then
            if flags then
                ngx.exec("@cc")
            else
                        local p_jskey=''
                        if args["jskey"] and type(args["jskey"])=='table' then
                                 p_jskey=args["jskey"][table.getn(args["jskey"])]
                        else
                                 p_jskey=args["jskey"]
                        end
                if p_jskey and p_jskey==tostring(jspara) then
                                jsjump:set(ip,jspara,3600,1)
                                ngx.exec("@cc")
                else
                                local url=''
                                if ngx.var.args then
                                       url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..jspara
                                else
                                       url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..jspara
                                end
                                local jscode="window.location.href='"..url.."';"
                                ngx.say(jscode)
                end
            end
        else
        math.randomseed( os.time() );
            local random=math.random(100000,999999)
            jsjump:set(ip,random,60)
            local url=''
            if ngx.var.args then
                url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..random
            else
                url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..random
            end
            local jscode="window.location.href='"..url.."';"
            ngx.say(jscode)
        end
    

    lua代码部分解释:
    1、1-12行是限速功能实现,第5和第10行表示10秒钟内容最多只能请求20次。
    2、14-48行是验证部分,24行中的3600表示验证通过后,白名单时间为3600秒,即1小时。

    update: 2013.5.26
    1、修复JS无限跳转bug
    2、增加随机种子

  • 相关阅读:
    使用libgdx及其中的box2d 2.1的注意事项
    Android.mk file syntax specification(ndkr8)
    NDK Note
    Problems of Android NDK
    Android.mk of NDK
    Android IM Note
    Regular Expression
    Some Efficient Algorithms
    libgdx use TexturePacker
    The Conversion Of JNI
  • 原文地址:https://www.cnblogs.com/interdrp/p/15920674.html
Copyright © 2020-2023  润新知