Body_bytes_sent always 0

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;
}

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:

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: