nginx 4层负载 OpenResty

需求描述:

根据IP访问间隔,将请求转发到不同证书的负载均衡器上

使用OpenResty进行upstream balancer_by_lua_block配置:

    upstream server {
        # 占位server
        server 127.0.0.1:80;
        balancer_by_lua_block {
               local filer = io.open("somewhere/ipaddrs/".. ngx.var.remote_addr, "r")
               local diffT = 100
               if (filer ~= nil) then
                   local oldT = filer:read()
                   local oldT1 = tonumber(oldT)
                   diffT = os.time() - oldT1
               end
               ngx.log(ngx.ERR, "------------: " .. diffT )
               local file = io.open("somewhere/ipaddrs/".. ngx.var.remote_addr, "w")
               file:write(os.time())
               file:close()
               if diffT < 6 then
               local balancer = require "ngx.balancer"
               -- first ssl nginx
               local host = "ur nginx IP"
               local port = ur nginx Port
               local ok, err = balancer.set_current_peer(host, port)
                    if not ok then
                        ngx.log(ngx.ERR, "failed to set the current peer: ", err)
                        return ngx.exit(500)
                    end
               end
        }
    }

配置多台代理

通过NFS共享ipaddrs目录

你可能感兴趣的:(nginx 4层负载 OpenResty)