nginx+lua控制转发资源

nginx+lua控制转发资源

  • nginx+lua安装配置
https://segmentfault.com/a/1190000018641801
  • nginx.conf
upstream aaa{
        server 47.105.104.145:10086 weight=1;
    }
    upstream bbb{
        server 47.105.104.145:5000 weight=1;
    }
server {
        listen       80;
        server_name  localhost;
        set $target ‘aaa‘;
        location /lua_test {
            rewrite_by_lua_file /opt/lua/test.lua;
            proxy_pass http://$target/;
        }
        
}
  • test.lua
local targetName = "aaa";

function setTarget()
    -- 通过cookie转发
    if (ngx.var.http_cookie == "123")
    then
        ngx.var.target = "bbb";
    else
        ngx.var.target = targetName;
    end
end


function exceptionHandle(err)
    ngx.log(ngx.ERR, err);
end

--添加异常处理
xpcall(setTarget, exceptionHandle);

nginx+lua控制转发资源

上一篇:mac在sd卡上装系统


下一篇:BBN