lua +nginx 简单的鉴权 && 反向代理 && mac 下安装nginx +lua

背景: QA需要 beta 环境进行测试,已经有服务了,不想再封装一遍接口,但是beta 环境 与release 数据相通,直接暴露接口风险太大,所以想着nginx 反向代理解决一下

 

参考地址:https://www.cnblogs.com/guonan/p/5481296.html

Mac 下 安装 lua-module  && lua && luajit && luarocks && cjson

安装nginx with lua-module
brew tap denji/nginx 
brew options nginx-full
brew install nginx-full --with-lua-module

安装lua && luajit
brew install lua@5.1
brew install luajit

安装luarocks 包管理工具
brew search luarocks
brew install mesca/luarocks/luarocks51

安装lua cjson 
luarocks remove lua-cjson
luarocks install lua-cjson 2.1.0-1

一、nginx 配置

location /commonSign {

      rewrite_by_lua_file  /Users/banxia/work/mydocker/images/lua_file/commsign.lua;

      proxy_pass http://172.17.xxx.xxx:xxx;

  }

 

二、lua代码

-- 需要lua 的 cjson 模块 ,进行json 解析,

 

local json = require "cjson"

   -- local body = ngx.req.get_body_data()

local request_method = ngx.var.request_method

local receive_headers = ngx.req.get_headers()

   -- print(receive_headers['content-type'])

 

 

local args = nil

if "GET" == request_method then

    args = ngx.req.get_uri_args()

elseif "POST" == request_method then

    ngx.req.read_body()

    if "application/json" == receive_headers['content-type'] then

        args = json.decode(ngx.req.get_body_data())

    else

        args = ngx.req.get_post_args()

        ngx.say( "method error")

        return

    end

end

 

 

local user_id = args["user_id"]

 

 

 

local whiteList={

    21324563,

    222227997,

    63241963,

    70288698,

}

 

local flag=nil

for index,value in pairs(whiteList)do

    if tonumber(user_id) == value then

        flag=true

        break

    end

end

 

if not flag then

    response={user_id=user_id,msg='error'}

    ngx.say(json.encode(response))

    -- ngx.say('user_id :',user_id,' error')

    ngx.exit(200);

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值