"if" (as operator of module "rewrite") works before server gets answer from upstream. That's why $upstream_* variables are empty for him. In my case operator "map" helps me. Posted at Nginx Forum: http://forum.nginx.org/read.php?2,226331,226384#msg-226384