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 [email protected]
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章