If I understand correctly you want to restrict access to the proxies resource using your Lua code below. Check out access_by_lua_file
https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#access_by_lua_file - you can run your auth checks in the access_by_lua_file block. Exit with 401/403 (as appropriate) if the checks fail, and return normally if not, then in the nginx config put the proxy_pass underneath the access_by_lua_file block. If all the checks pass, the execution should fall through to the proxy_pass block.