passing real IP address from one nginx instance to another and writing it in the logs

The server receives X-Real-IP as one of the headers in the HTTP request. Nginx makes each of the request header values available as a variable.

The variable name for http request header values begins with $http_. The header name is converted to lowercase, and each hyphen is replaced with an underscore.

For example X-Real-IP is available as $http_x_real_ip

See this document for details.