Archive for March 23rd, 2011
Just been trying to figure your why headers set to $body_bytes_sent where always 0 even after a successful download of a file from nginx.
We’ll after much head scratching I tried something that I didn’t think would work but it did. It turns out that the first time $body_bytes_sent is accessed in a request its value is cached and reused for subsequent references. We where using an include to configure proxy_pass headers for all our stages of the download process which had X-Bytes-Sent being configured to $body_bytes_sent.
Unfortunately because $body_bytes_sent is not flagged as none cacheable its value is fixed to then it is first used, which in our case was prior to any body being sent and hence the problem.
The fix for us was simple remove it from the include and only specify it in the one place we wanted it but this may not be possible for all uses so the correct fix may be to set NGX_HTTP_VAR_NOCACHEABLE on flag body_bytes_sent in src/http/ngx_http_variables.c for nginx.