Cookie and first load balancing

The only reason I need haproxy in my software stack is for “cookie
persistence” and “first” load balancing. I’d like to use nginx instead.

Can I do the equivalent of haproxy “cookie persistence” in nginx by
testing if the request has a cookie set and then testing this cookie’s
value to decide which upstream to use? Seems possible to me.

As for haproxy’s “first” load balancing, is there a variable in nginx
that I can test for a given server which yields the current number of
active connections that nginx has with that server? I haven’t found any
such variable in nginx’s documentation.