Nginx – Passing a timestamp header onto upstream servers with Nginx

nginx

I'd like to have my Nginx load balancer send a timestamp in a header onto the upstream servers.

I tried something like this:

proxy_set_header X-Start-Time $msec;

Of course, $msec is a log variable and Nginx gives an error:

Restarting nginx: [emerg]: unknown "msec" variable

Best Answer

$msec is indeed a variable that lives only during log writing.

The easiest solution would be to build Nginx with Perl module and define a variable backed by Perl subroutine:

# at 'http' context
perl_set $unix_timestamp 'sub {
  time();
}';

# where you want it:
proxy_set_header X-Start-Time $unix_timestamp;