Question on Nginx Proxy Installation

Hello,
Following is a problem I am facing while using nginx as a proxy, please
provide me feedback and ideas to solve it.
I have a zookeeper based service discovery setup. For some of the
services
I want to use nginx as a proxy.

Flow I am planning to set is like this…
Client connects to nginx with some user id, based on userid nginx
handler
need to do a “lookup in ZK” and find out a service instance. Then pass
request to service using “proxy_pass” .

Problem I am facing is how to do this “lookup” along with proxy_pass. I
understand that I may need to register my handler in one of the phase
explained here http://wiki.nginx.org/Phases .
Most likely it will “rewrite”, but that allows on regex. How I can
invoke a
functionality bundled in jar?

Please note I am also aware of http://nginx-clojure.github.io/ which
allows
you to execute java handlers through nginx, but not really sure how to
use
it with “proxy_pass”.

Thanks in advance for your help.
Best Regards,
Vikrant