# Body\_bytes\_sent always 0

**URL:** <https://www.ruby-forum.com/t/body-bytes-sent-always-0/204879>\
**Category:** NGINX\
**Created:** [March 23, 2011, 11:13pm UTC](https://www.ruby-forum.com/t/body-bytes-sent-always-0/204879 "2011-03-23T23:13:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 23, 2011, 11:13pm UTC](https://www.ruby-forum.com/t/body-bytes-sent-always-0/204879/1 "2011-03-23T23:13:02Z")

</div>

I’m trying to configure a post\_action to submit details of downloads to  
our app but when I do the body\_bytes\_sent is always 0 even after a  
successful download.

The flow we have is:-

1. external request
2. proxy pass to app for authorisation
3. app returns X-Accel-Redirect if authorised then process download
4. post\_action to internal location
5. internal location proxy passes to app to register download including  
size which is used to determine if download completed

Its #5 where the issue appears as the download size passed via  
X-Bytes-Sent is always 0

Here’s the config for #4 and #5

location /files {  
alias /data/files/;  
internal;  
post\_action /internal/finish\_download;  
}

location = /internal/finish\_download {  
internal;  
proxy\_set\_header Host internal;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Original-Host $server\_name;  
proxy\_set\_header X-Original-Uri $request\_uri;  
proxy\_set\_header X-Bytes-Sent $body\_bytes\_sent;  
proxy\_set\_header X-Range $http\_range;  
proxy\_pass [http://balancer/internal/finish\_download](http://balancer/internal/finish_download);  
}

Everything else seems to work fine its just X-Bytes-Sent is always 0,  
I’ve checked in the access entry and size is set correctly so it seems  
that $body\_bytes\_sent is lost by the time it gets into the post\_action  
location.

Posted at Nginx Forum:

> **[body\_bytes\_sent always 0](https://forum.nginx.org/read.php?2,185270,185270#msg-185270)**
>
> I'm trying to configure a post\_action to submit details of downloads to our app but when I do the body\_bytes\_sent is always 0 even after a successful download. The flow we have is:- 1. external request 2. proxy pass to app for authorisation 3. app...

---

<div class="post-metadata">

**Author:** ![steveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@steveh](https://www.ruby-forum.com/u/steveh)\
**Post date:** [March 24, 2011, 12:20am UTC](https://www.ruby-forum.com/t/body-bytes-sent-always-0/204879/2 "2011-03-24T00:20:36Z")

</div>

Ok I’ve figured this out but I’m not sure if its a bug or a “feature”  
the issue is that in stage #2 I was also setting:  
proxy\_set\_header X-Bytes-Sent $body\_bytes\_sent;

This appears to cause $body\_bytes\_sent to be evaluated and “stored” so  
that subsequent uses of $body\_bytes\_sent don’t call the method  
ngx\_http\_variable\_body\_bytes\_sent to determine the amount of body bytes  
sent but instead uses the already stored value, which is now out of  
date.

In this case the solution is to not set X-Bytes-Sent to $body\_bytes\_sent  
in previous phase of the request, however there may be other cases where  
this is not possible, and where dynamic variable is required instead of  
a static one.

Looking through the source the fix for this could be to set  
NGX\_HTTP\_VAR\_NOCACHEABLE for body\_bytes\_sent?

Posted at Nginx Forum:

> **[Re: body\_bytes\_sent always 0](https://forum.nginx.org/read.php?2,185270,185284#msg-185284)**
>
> I'm trying to configure a post\_action to submit details of downloads to our app but when I do the body\_bytes\_sent is always 0 even after a successful download. The flow we have is:- 1. external request 2. proxy pass to app for authorisation 3. app...
