Such dynamics are better solved with Lua by keeping the actual state in memory pulled from a database.
Drop this into google: "nginx lua dynamic proxy"
Drop this into google: "nginx lua dynamic proxy"