背景: 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 |