ngx_lua【set_by_lua_file】用法

set_by_lua_file 【变量】【lua脚本】【可变参数】

   location / {
        set_by_lua_file $backend /etc/nginx/conf.d/lua/lua-sets.lua "backend01";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://${backend}${request_uri};
        uwsgi_read_timeout 300;
   }
--lua-sets.lua
local s_flow = require("traffic-interceptor")
local arg = { ... } -- 可变脚本
local pro = arg[1] --arg[0]为当前脚本名,arg[1]为第一个参数,以此类推
local flag = s_flow.auth()
local backend = nil

if flag == true then
  backend = upstreams["green_"..pro]
else
  backend = upstreams["blue_"..pro]
end
if backend ~= nil then
  ngx.log(ngx.WARN, "get [=====", backend,"=========] from lua shared memory")
  return backend
end


你可能感兴趣的:(nginx,nginx,开发语言)