Question on Nginx Proxy Installation

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
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
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 .
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 which
you to execute java handlers through nginx, but not really sure how to
it with “proxy_pass”.

Thanks in advance for your help.
Best Regards,