美文网首页
Nginx+Lua:过滤特定接口请求

Nginx+Lua:过滤特定接口请求

作者: superxcp | 来源:发表于2020-06-15 15:25 被阅读0次

技术需求点:在12:00:00~12:05:00时间内,对"/service/get_list.do"的接口请求直接拒绝,返回FORBIDDEN
使用前提:
使用openresty:openresty本身是集成了lua组件的nginx,相当于把部分的后端服务使用lua集成到反向代理里面,而单纯的nginx是做不到这一点的;

# 开启读取body体
lua_need_request_body on;

# 在12:00:00~12:05:00时间段内,对"/service/get_list.do"接口的请求直接拒绝,返回FORBIDDEN
access_by_lua '
    local method = ngx.var.request_method
    #获取系统时间
    local now=os.date("%X")
    local regex="(/service/get_list.do)"
    if method == "POST" then
            if (ngx.var.request ~= nil) and ngx.re.match(ngx.var.request, regex) and now>"12:00:00" and now<"12:05:00" then
                    ngx.exit(ngx.HTTP_FORBIDDEN)
            else
                    return
            end
    end
';

相关文章

网友评论

      本文标题:Nginx+Lua:过滤特定接口请求

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