Nginx整合Openresty打造高性能网关lua

nginx有11个处理阶段,如下图所示:

每一个处理阶段描述

指令所处处理阶段使用范围解释
init_by_lua
init_by_lua_file
loading-confighttpnginx Master进程加载配置时执行;通常用于初始化全局配置/预加载Lua模块
init_worker_by_lua
init_worker_by_lua_file
starting-workerhttp每个Nginx Worker进程启动时调用的计时器,如果Master进程不允许则只会在init_by_lua之后调用;通常用于定时拉取配置/数据,或者后端服务的健康检查
set_by_lua
set_by_lua_file
rewriteserver,server if,location,location if设置nginx变量,可以实现复杂的赋值逻辑;此处是阻塞的,Lua代码要做到非常快;
rewrite_by_lua
rewrite_by_lua_file
rewrite tailhttp,server,location,location ifrrewrite阶段处理,可以实现复杂的转发/重定向逻辑;
access_by_lua
access_by_lua_file
access tailhttp,server,location,location if请求访问阶段处理,用于访问控制
content_by_lua
content_by_lua_file
contentlocation,location if内容处理器,接收请求处理并输出响应
header_filter_by_lua
header_filter_by_lua_file
output-header-filterhttp,server,location,location if设置header和cookie
body_filter_by_lua
body_filter_by_lua_file
output-body-filterhttp,server,location,location if对响应数据进行过滤,比如截断、替换。
log_by_lua
log_by_lua_file
loghttp,server,location,location iflog阶段处理,比如记录访问量/统计平均响应时间

一般我们在开发过程中常用到的阶段如下:

set_by_lua、rewrite_by_lua、access_by_lua、content_by_lua、header_filter_by_lua、body_filter_by_lua、log_by_lua、

1.set_by_lua

   做流程分支判断,判断变量初始化

2.rewrite_by_lua

   转发重定向,缓存功能

3.access_by_lua

   ip准入,接口合法权限判断,根据iptable做防火墙的功能

4.content_by_lua

   内容生产

5.header_filter_by_lua

   增加头部信息

6.body_filter_by_lua

   内容过滤

7.log_by_lua

    记录日志

先开发好lua脚本:ikong_api.lua
-- Require Modules
local conf = require "ik_config"

local cjson = require "cjson.safe"

local precheck = require "ik_precheck"
-- Performance Optimization
local pcall = pcall
local error = error
local str_format = string.format
local io_open = io.open
local math_randomseed = math.randomseed
local math_rand = math.random
local new_timer = ngx.timer.at
local DEBUG = ngx.DEBUG
local INFO = ngx.INFO
local ERR = ngx.ERR
local HTTP_OK = ngx.HTTP_OK
local cjson_encode = cjson.encode
local debug_mode = ngx.config.debug
local ngx_log = ngx.log
local ngx_var = ngx.var
local ngx_now = ngx.now
local ngx_redirect = ngx.redirect
local ngx_exit = ngx.exit
local ngx_req_read_body = ngx.req.read_body
local ngx_req_get_body_data = ngx.req.get_body_data
local ngx_header = ngx.header
local ngx_say = ngx.say
local ngx_shm = ngx.shared
local ik_conf_server = conf.ik_server
local ikong_ctrl_shm = ngx_shm[conf.ik_ctrl_shared_dict_name]

local ik_xx_key = conf.ik_xx_key

local _M = {}
_M._VERSION = '1.0.0'

-- enable check
local function enable()

    ngx_req_read_body()
    local body = ngx_req_get_body_data() //这里获取到body,必须先调用上面ngx_req_read_body函数,才能拿到body的data

    ngx_header.content_type = "application/json"
    if succ then

        -- set to file
        -- enable
        if ikong_ctrl_shm:get(ik_xx_key) == false then
            ikong_ctrl_shm:set(ik_xx_key, true, 0)
            info("enable ik module")
        end

        -- response data
        ngx_say(cjson_encode({
            ["success"] = true,
            ["msg"] = ""
        }))
    else
        ngx_say(cjson_encode({
            ["success"] = false,
            ["msg"] = err
        }))
    end

end


-- disable  check
local function disable()

    ngx_exit(HTTP_OK)

end


local function update_conf()

    ngx_exit(HTTP_OK)

end


-- check timeout
local function check_timeout()

    ngx_exit(HTTP_OK)

end


-- get status
local function status()

    ngx_exit(HTTP_OK)

end



if ngx_var.request_method == "POST" then
    if ngx_var.uri == conf.ik_api_enable then
        return enable()
    end
    if ngx_var.uri == conf.ik_api_disable then
        return disable()
    end
    if ngx_var.uri == conf.ik_api_conf then
        return update_conf()
    end
    if ngx_var.uri == conf.ik_api_check_timeout then
        return check_timeout()
    end
elseif ngx_var.request_method == "GET" then
    if ngx_var.uri == conf.ik_api_status then
        return status()
    end
end

return ngx_exit(HTTP_FORBIDDEN)

nginx下加入conf的配置

lua_shared_dict                 ikong_ctrl_shm     1M; //定义shm缓存


# ikong server
server {
    listen 8009;

    location ~ ^/ikong-api/([-_a-zA-Z0-9]+) {
        allow 127.0.0.1;
        deny all;

        content_by_lua_file     /Users/ikong/ikong_api.lua;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码者人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值