diff src/http/ngx_http_upstream.c @ 6654:c131f20c9562

Upstream: the $upstream_bytes_received variable. Unlike $upstream_response_length that only counts the body size, the new variable also counts the size of response header and data received after switching protocols when proxying WebSockets.
author Vladimir Homutov <vl@nginx.com>
date Wed, 10 Aug 2016 16:46:39 +0300
parents 3c87b82b17d4
children 9cf2dce316e5
line wrap: on
line diff
--- a/src/http/ngx_http_upstream.c	Mon Aug 08 17:11:29 2016 +0300
+++ b/src/http/ngx_http_upstream.c	Wed Aug 10 16:46:39 2016 +0300
@@ -391,6 +391,10 @@
       ngx_http_upstream_response_length_variable, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
+    { ngx_string("upstream_bytes_received"), NULL,
+      ngx_http_upstream_response_length_variable, 1,
+      NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
 #if (NGX_HTTP_CACHE)
 
     { ngx_string("upstream_cache_status"), NULL,
@@ -2136,6 +2140,8 @@
             return;
         }
 
+        u->state->bytes_received += n;
+
         u->buffer.last += n;
 
 #if 0
@@ -2642,6 +2648,7 @@
             return;
         }
 
+        u->state->bytes_received += n;
         u->state->response_length += n;
 
         if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
@@ -3215,6 +3222,10 @@
                 do_write = 1;
                 b->last += n;
 
+                if (from_upstream) {
+                    u->state->bytes_received += n;
+                }
+
                 continue;
             }
 
@@ -3411,6 +3422,7 @@
             }
 
             if (n > 0) {
+                u->state->bytes_received += n;
                 u->state->response_length += n;
 
                 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
@@ -4095,6 +4107,8 @@
         u->state->response_time = ngx_current_msec - u->state->response_time;
 
         if (u->pipe && u->pipe->read_length) {
+            u->state->bytes_received += u->pipe->read_length
+                                        - u->pipe->preread_size;
             u->state->response_length = u->pipe->read_length;
         }
     }
@@ -5242,7 +5256,13 @@
     state = r->upstream_states->elts;
 
     for ( ;; ) {
-        p = ngx_sprintf(p, "%O", state[i].response_length);
+
+        if (data == 1) {
+            p = ngx_sprintf(p, "%O", state[i].bytes_received);
+
+        } else {
+            p = ngx_sprintf(p, "%O", state[i].response_length);
+        }
 
         if (++i == r->upstream_states->nelts) {
             break;