Coder Social home page Coder Social logo

resty-api-waf's Introduction

安装

依赖项(详见 Dockerfile)

使用示例

docker-compose up -d resty

说明

两个共享内存

  • lua_shared_dict waf 1m; 存放 waf 配置等信息
  • lua_shared_dict limiter 10m; 存放请求频率限制器信息

执行流程

    1. init_worker_by_lua 阶段, 读入默认配置, 并从 redis 获取最新配置信息, 合并两者放入共享内存
    1. access_by_lua 阶段, 从共享内存读取配置, 顺序执行对应模块

配置的结构

  • matcher 一些匹配规则, 可在各模块间共用
  • response 自定义响应格式, 可在各模块间共用
  • modules 模块配置

filter 模块

  • 用于过滤请求uri, header 信息

limiter 模块

  • 用于建立请求频率限制
  • 可设立仅针对IP的规则, 也可设立仅针对uri的规则, 默认为ip + uri 合并的规则

manager 模块

  • 用于 waf 的管理, 提供以 /waf 开头的路由, 需要进行 Basic Authorizaton 认证
  • /waf/config, 获取当前应用的配置
  • /waf/config/reload, 立即更新配置

自定义配置

  • 自定义配置存放在 redis 中以 waf:config: 为开头的hset
  • 目前支持四个配置项, 硬编码在shared.lua 中, 分别为 matcher, response, modules.filter.rules, modules.limiter.rules

参考项目


OpenResty 生命周期

OpenResty 变量共享

模块里的变量

  • 处于模块级别的变量在每个 worker 间是相互独立的,且在 worker 的生命周期中是只读的, 只在第一次导入模块时初始化.
  • 模块里函数的局部变量,则在调用时初始化

ngx.var.*

  • lua-nginx-module#ngxvarvariable
  • 使用代价较高
  • 续先预定义才可使用(可在server 或 location 中定义)
  • 类型只能是字符串
  • 内部重定向会破坏原始请求的 ngx.var.* 变量 (如 error_page, try_files, index 等)

ngx.ctx.*

  • lua-nginx-module#ngxctx
  • 内部重定向会破坏原始请求的 ngx.ctx.* 变量 (如 error_page, try_files, index 等)

ngx.shared.DICT.*

resty.lrucache

  • lua-resty-lrucache
  • 不同 worker 间数据相互隔离
  • 同一 worker 不同请求共享数据

https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker

table 与 metatable

https://www.cnblogs.com/liekkas01/p/12728712.html

OpenResty LuaJIT2

Lua 手册

resty-api-waf's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.