Alex, I have a LUA version of the PHP script. I am not sure about the multiple rewrite or conditions Posted at Nginx Forum: https://forum.nginx.org/read.php?2,274815,274870#msg-274870