美文网首页
nginx用lua允许指定ip通过

nginx用lua允许指定ip通过

作者: Stupboy | 来源:发表于2019-12-18 10:56 被阅读0次

nginx用的是宝塔安装~~,自用mark一下nginx配置

location / {
        access_by_lua '
            local redis = require "resty.redis";
            local red = redis:new();
            red:set_timeout(200);
            local ok, err = red:connect("127.0.0.1", 6379);
            local allow_ips, err = red:hvals("task_map");
            -- task_map 为存允许ip集合
            if err then
                return ngx.exit(ngx.HTTP_FORBIDDEN);
            else
                local allowEnter = false
                for index, allow_ip in ipairs(allow_ips) do
                    if allow_ip == ngx.var.remote_addr then
                        allowEnter = true
                        break;
                    end
                end
                if allowEnter then
                        -- 允许通过
                else
                    return ngx.exit(ngx.HTTP_FORBIDDEN);
                end 
            end
            ';
    }

相关文章

网友评论

      本文标题:nginx用lua允许指定ip通过

      本文链接:https://www.haomeiwen.com/subject/zviynctx.html