nginx-0.3.54-RELEASE import

    *) Feature: nginx now logs the subrequest information to the error log.

    *) Feature: the "proxy_next_upstream", "fastcgi_next_upstream", and
       "memcached_next_upstream" directives support the "off" parameter.

    *) Feature: the "debug_connection" directive supports the CIDR address
       form.

    *) Bugfix: if a response of proxied server or FastCGI server was
       converted from UTF-8 or back, then it may be transferred incomplete.

    *) Bugfix: the $upstream_response_time variable had the time of the
       first request to a backend only.

    *) Bugfix: nginx could not be built on amd64 platform; the bug had
       appeared in 0.3.53.
diff --git a/auto/make b/auto/make
index 346770a..4a2cd0c 100644
--- a/auto/make
+++ b/auto/make
@@ -150,10 +150,10 @@
 done
 
 ngx_all_objs=`echo $ngx_all_srcs \
-    | sed -e "s/\([^ ]*\.\)cpp/$NGX_OBJS\/\1$ngx_objext/g" \
-          -e "s/\([^ ]*\.\)cc/$NGX_OBJS\/\1$ngx_objext/g" \
-          -e "s/\([^ ]*\.\)c/$NGX_OBJS\/\1$ngx_objext/g" \
-          -e "s/\([^ ]*\.\)S/$NGX_OBJS\/\1$ngx_objext/g"`
+    | sed -e "s#\([^ ]*\.\)cpp#$NGX_OBJS\/\1$ngx_objext#g" \
+          -e "s#\([^ ]*\.\)cc#$NGX_OBJS\/\1$ngx_objext#g" \
+          -e "s#\([^ ]*\.\)c#$NGX_OBJS\/\1$ngx_objext#g" \
+          -e "s#\([^ ]*\.\)S#$NGX_OBJS\/\1$ngx_objext#g"`
 
 ngx_modules_c=`echo $NGX_MODULES_C | sed -e "s/\//$ngx_regex_dirsep/g"`
 
@@ -213,10 +213,10 @@
 do
     ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
     ngx_obj=`echo $ngx_src \
-        | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
-              -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
-              -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
-              -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+        | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
+              -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
+              -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
+              -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`
 
     cat << END                                                >> $NGX_MAKEFILE
 
@@ -242,10 +242,10 @@
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
         ngx_obj=`echo $ngx_src \
-            | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`
 
         cat << END                                            >> $NGX_MAKEFILE
 
@@ -272,10 +272,10 @@
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
         ngx_obj=`echo $ngx_src \
-            | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`
 
         cat << END                                            >> $NGX_MAKEFILE
 
@@ -302,10 +302,10 @@
             | sed -e "s/\//$ngx_regex_dirsep/g"`
 
         ngx_obj=`echo $ngx_obj \
-            | sed -e "s/^\(.*\.\)cpp$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)cc$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)c$/$ngx_objs_dir\1$ngx_objext/g" \
-                  -e "s/^\(.*\.\)S$/$ngx_objs_dir\1$ngx_objext/g"`
+            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
+                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`
 
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
 
diff --git a/auto/unix b/auto/unix
index 0fe2974..1cf0dc3 100755
--- a/auto/unix
+++ b/auto/unix
@@ -21,7 +21,14 @@
 
 # POSIX types
 
-NGX_INCLUDE_AUTO_CONFIG_H="#include \"../$NGX_AUTO_CONFIG_H\""
+case "$NGX_AUTO_CONFIG_H" in
+    /*)
+        NGX_INCLUDE_AUTO_CONFIG_H="#include \"$NGX_AUTO_CONFIG_H\""
+    ;;
+    *)
+        NGX_INCLUDE_AUTO_CONFIG_H="#include \"../$NGX_AUTO_CONFIG_H\""
+    ;;
+esac
 
 ngx_type="uint64_t"; ngx_types="u_int64_t"; . auto/types/typedef
 
diff --git a/docs/xml/nginx/changes.xml b/docs/xml/nginx/changes.xml
index d894033..fa4ac8f 100644
--- a/docs/xml/nginx/changes.xml
+++ b/docs/xml/nginx/changes.xml
@@ -9,6 +9,73 @@
 <title lang="en">nginx changelog</title>
 
 
+<changes ver="0.3.54" date="11.07.2006">
+
+<change type="feature">
+<para lang="ru">
+nginx ÔÅÐÅÒØ ÚÁÐÉÓÙ×ÁÅÔ × ÌÏÇ ÉÎÆÏÒÍÁÃÉÀ Ï ÐÏÄÚÁÐÒÏÓÁÈ.
+</para>
+<para lang="en">
+nginx now logs the subrequest information to the error log.
+</para>
+</change>
+
+<change type="feature">
+<para lang="ru">
+ÄÉÒÅËÔÉ×Ù proxy_next_upstream, fastcgi_next_upstream É memcached_next_upstream
+ÐÏÄÄÅÒÖÉ×ÁÀÔ ÐÁÒÁÍÅÔÒ off.
+</para>
+<para lang="en">
+the "proxy_next_upstream", "fastcgi_next_upstream",
+and "memcached_next_upstream" directives support the "off" parameter.
+</para>
+</change>
+
+<change type="feature">
+<para lang="ru">
+ÄÉÒÅËÔÉ×Á debug_connection ÐÏÄÄÅÒÖÉ×ÁÅÔ ÚÁÐÉÓØ ÁÄÒÅÓÏ× × ÆÏÒÍÁÔÅ CIDR.
+</para>
+<para lang="en">
+the "debug_connection" directive supports the CIDR address form.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+ÐÒÉ ÐÅÒÅËÏÄÉÒÏ×ÁÎÉÉ ÏÔ×ÅÔÁ ÐÒÏËÓÉÒÏ×ÁÎÎÏÇÏ ÓÅÒ×ÅÒÁ ÉÌÉ ÓÅÒ×ÅÒÁ FastCGI
+× UTF-8 ÉÌÉ ÎÁÏÂÏÒÏÔ ÏÔ×ÅÔ ÍÏÇ ÐÅÒÅÄÁ×ÁÔØÓÑ ÎÅ ÐÏÌÎÏÓÔØÀ.
+</para>
+<para lang="en">
+if a response of proxied server or FastCGI server was converted from UTF-8
+or back, then it may be transferred incomplete.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+ÐÅÒÅÍÅÎÎÁÑ $upstream_response_time ÓÏÄÅÒÖÁÌÁ ×ÒÅÍÑ ÔÏÌØËÏ ÐÅÒ×ÏÇÏ
+ÏÂÒÁÝÅÎÉÑ Ë ÂÜËÅÎÄÕ.
+</para>
+<para lang="en">
+the $upstream_response_time variable had the time of the first
+request to a backend only.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+nginx ÎÅ ÓÏÂÉÒÁÌÓÑ ÎÁ ÐÌÁÔÆÏÒÍÅ amd64;
+ÏÛÉÂËÁ ÐÏÑ×ÉÌÁÓØ × 0.3.53.
+</para>
+<para lang="en">
+nginx could not be built on amd64 platform;
+bug appeared in 0.3.53.
+</para>
+</change>
+
+</changes>
+
+
 <changes ver="0.3.53" date="07.07.2006">
 
 <change type="change">
@@ -55,7 +122,7 @@
 </para>
 <para lang="en">
 if a client was successfully authorized using "satisfy_any on", then anyway
-the "access forbidden by rule" message was written in the log.
+the message "access forbidden by rule" was written in the log.
 </para>
 </change>
 
diff --git a/src/core/nginx.h b/src/core/nginx.h
index 50746ae..1b135e5 100644
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.3.53"
+#define NGINX_VER          "nginx/0.3.54"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_OLDPID_EXT     ".oldbin"
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 7dc1562..7d4cdba 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -1035,22 +1035,30 @@
 #if (NGX_DEBUG)
     ngx_event_conf_t  *ecf = conf;
 
-    in_addr_t       *addr;
-    ngx_str_t       *value;
-    struct hostent  *h;
+    ngx_event_debug_t  *dc;
+    ngx_str_t          *value;
+    struct hostent     *h;
+    ngx_inet_cidr_t     in_cidr;
 
     value = cf->args->elts;
 
     /* AF_INET only */
 
-    addr = ngx_array_push(&ecf->debug_connection);
-    if (addr == NULL) {
+    dc = ngx_array_push(&ecf->debug_connection);
+    if (dc == NULL) {
         return NGX_CONF_ERROR;
     }
 
-    *addr = inet_addr((char *) value[1].data);
+    dc->addr = inet_addr((char *) value[1].data);
 
-    if (*addr != INADDR_NONE) {
+    if (dc->addr != INADDR_NONE) {
+        dc->mask = 0xffffffff;
+        return NGX_OK;
+    }
+
+    if (ngx_ptocidr(&value[1], &in_cidr) == NGX_OK) {
+        dc->mask = in_cidr.mask;
+        dc->addr = in_cidr.addr;
         return NGX_OK;
     }
 
@@ -1062,7 +1070,8 @@
         return NGX_CONF_ERROR;
     }
 
-    *addr = *(in_addr_t *)(h->h_addr_list[0]);
+    dc->mask = 0xffffffff;
+    dc->addr = *(in_addr_t *)(h->h_addr_list[0]);
 
 #else
 
@@ -1096,7 +1105,7 @@
 #if (NGX_DEBUG)
 
     if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
-                       sizeof(in_addr_t)) == NGX_ERROR)
+                       sizeof(ngx_event_debug_t)) == NGX_ERROR)
     {
         return NGX_CONF_ERROR;
     }
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 25f4189..edcee4f 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -189,6 +189,12 @@
 
 
 typedef struct {
+    in_addr_t  mask;
+    in_addr_t  addr;
+} ngx_event_debug_t;
+
+
+typedef struct {
     ngx_int_t  (*add)(ngx_event_t *ev, int event, u_int flags);
     ngx_int_t  (*del)(ngx_event_t *ev, int event, u_int flags);
 
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
index e41f04a..efd09e1 100644
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -198,9 +198,6 @@
         wev->own_lock = &c->lock;
 #endif
 
-        ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0,
-                       "accept: fd:%d c:%d", s, c->number);
-
         if (ls->addr_ntop) {
             c->addr_text.data = ngx_palloc(c->pool, ls->addr_text_max_len);
             if (c->addr_text.data == NULL) {
@@ -220,14 +217,14 @@
 #if (NGX_DEBUG)
         {
 
-        uint32_t            *addr;
         in_addr_t            i;
+        ngx_event_debug_t   *dc;
         struct sockaddr_in  *sin;
 
         sin = (struct sockaddr_in *) sa;
-        addr = ecf->debug_connection.elts;
+        dc = ecf->debug_connection.elts;
         for (i = 0; i < ecf->debug_connection.nelts; i++) {
-            if (addr[i] == sin->sin_addr.s_addr) {
+            if ((sin->sin_addr.s_addr & dc[i].mask) == dc[i].addr) {
                 log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
                 break;
             }
@@ -236,6 +233,9 @@
         }
 #endif
 
+        ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0,
+                       "*%d accept: %V fd:%d", c->number, &c->addr_text, s);
+
         if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
             if (ngx_add_conn(c) == NGX_ERROR) {
                 ngx_close_accepted_connection(c);
diff --git a/src/http/modules/ngx_http_charset_filter_module.c b/src/http/modules/ngx_http_charset_filter_module.c
index 0bc31bc..e3fdb65 100644
--- a/src/http/modules/ngx_http_charset_filter_module.c
+++ b/src/http/modules/ngx_http_charset_filter_module.c
@@ -432,6 +432,17 @@
             b = cl->buf;
 
             if (ngx_buf_size(b) == 0) {
+
+                *ll = ngx_alloc_chain_link(r->pool);
+                if (*ll == NULL) {
+                    return NGX_ERROR;
+                }
+
+                (*ll)->buf = b;
+                (*ll)->next = NULL;
+
+                ll = &(*ll)->next;
+
                 continue;
             }
 
diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
index 172a1a7..28f860e 100644
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -168,6 +168,7 @@
     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
+    { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
     { ngx_null_string, 0 }
 };
 
@@ -1694,6 +1695,11 @@
                                |NGX_HTTP_UPSTREAM_FT_ERROR
                                |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
 
+    if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
+        conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
+                                       |NGX_HTTP_UPSTREAM_FT_OFF;
+    }
+
     ngx_conf_merge_uint_value(conf->upstream.max_fails,
                               prev->upstream.max_fails, 1);
 
diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c
index 000622d..4373692 100644
--- a/src/http/modules/ngx_http_memcached_module.c
+++ b/src/http/modules/ngx_http_memcached_module.c
@@ -45,6 +45,7 @@
     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
     { ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
     { ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
+    { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
     { ngx_null_string, 0 }
 };
 
@@ -560,6 +561,11 @@
                                |NGX_HTTP_UPSTREAM_FT_ERROR
                                |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
 
+    if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
+        conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
+                                       |NGX_HTTP_UPSTREAM_FT_OFF;
+    }
+
     ngx_conf_merge_uint_value(conf->upstream.max_fails,
                               prev->upstream.max_fails, 1);
 
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index ff5b129..5999f2e 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -127,6 +127,7 @@
     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
+    { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
     { ngx_null_string, 0 }
 };
 
@@ -1648,6 +1649,11 @@
                                |NGX_HTTP_UPSTREAM_FT_ERROR
                                |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
 
+    if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
+        conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
+                                       |NGX_HTTP_UPSTREAM_FT_OFF;
+    }
+
     ngx_conf_merge_uint_value(conf->upstream.max_fails,
                               prev->upstream.max_fails, 1);
 
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index df75870..867f8f7 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -19,8 +19,8 @@
 
 typedef ngx_int_t (*ngx_http_header_handler_pt)(ngx_http_request_t *r,
     ngx_table_elt_t *h, ngx_uint_t offset);
-typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r, u_char *buf,
-    size_t len);
+typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r,
+    ngx_http_request_t *sr, u_char *buf, size_t len);
 
 
 #if (NGX_HTTP_CACHE)
@@ -49,6 +49,7 @@
 struct ngx_http_log_ctx_s {
     ngx_str_t           *client;
     ngx_http_request_t  *request;
+    ngx_http_request_t  *current_request;
 };
 
 
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index b417f41..ee0d3ca 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1162,6 +1162,7 @@
 {
     ngx_connection_t              *c;
     ngx_http_request_t            *sr;
+    ngx_http_log_ctx_t            *ctx;
     ngx_http_core_srv_conf_t      *cscf;
     ngx_http_postponed_request_t  *pr, *p;
 
@@ -1273,6 +1274,9 @@
         r->postponed = pr;
     }
 
+    ctx = c->log->data;
+    ctx->current_request = sr;
+
     sr->internal = 1;
     sr->fast_subrequest = 1;
 
diff --git a/src/http/ngx_http_postpone_filter_module.c b/src/http/ngx_http_postpone_filter_module.c
index b117748..37d200c 100644
--- a/src/http/ngx_http_postpone_filter_module.c
+++ b/src/http/ngx_http_postpone_filter_module.c
@@ -162,6 +162,7 @@
 {
     ngx_int_t                      rc;
     ngx_chain_t                   *out;
+    ngx_http_log_ctx_t            *ctx;
     ngx_http_postponed_request_t  *pr;
 
     for ( ;; ) {
@@ -177,6 +178,9 @@
                            "http postpone filter handle \"%V?%V\"",
                            &pr->request->uri, &pr->request->args);
 
+            ctx = r->connection->log->data;
+            ctx->current_request = pr->request;
+
             if (!pr->request->done) {
                 r->connection->data = pr->request;
                 return NGX_AGAIN;
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index bf3b0ed..0145fe7 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -43,8 +43,8 @@
 static void ngx_http_close_connection(ngx_connection_t *c);
 
 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
-static u_char *ngx_http_log_error_handler(ngx_http_request_t *r, u_char *buf,
-    size_t len);
+static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
+    ngx_http_request_t *sr, u_char *buf, size_t len);
 
 #if (NGX_HTTP_SSL)
 static void ngx_http_ssl_handshake(ngx_event_t *rev);
@@ -162,6 +162,7 @@
 
     ctx->client = &c->addr_text;
     ctx->request = NULL;
+    ctx->current_request = NULL;
 
     c->log->connection = c->number;
     c->log->handler = ngx_http_log_error;
@@ -427,6 +428,7 @@
 
     ctx = c->log->data;
     ctx->request = r;
+    ctx->current_request = r;
     r->log_handler = ngx_http_log_error_handler;
 
 #if (NGX_STAT_STUB)
@@ -541,7 +543,6 @@
     ngx_int_t            rc, rv;
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
-    ngx_http_log_ctx_t  *ctx;
 
     c = rev->data;
     r = c->data;
@@ -722,9 +723,6 @@
             }
 
             if (rv == NGX_DECLINED) {
-                ctx = c->log->data;
-                ctx->request = r;
-
                 r->request_line.len = r->header_in->end - r->request_start;
                 r->request_line.data = r->request_start;
 
@@ -1428,10 +1426,14 @@
 {
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
+    ngx_http_log_ctx_t  *ctx;
 
     c = ev->data;
     r = c->data;
 
+    ctx = c->log->data;
+    ctx->current_request = r;
+
     if (ev->write) {
         r->write_event_handler(r);
 
@@ -1445,6 +1447,7 @@
 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
 {
     ngx_http_request_t        *pr;
+    ngx_http_log_ctx_t        *ctx;
     ngx_http_core_loc_conf_t  *clcf;
 
     if (rc == NGX_DONE) {
@@ -1523,6 +1526,9 @@
             r->connection->data = pr;
         }
 
+        ctx = r->connection->log->data;
+        ctx->current_request = pr;
+
         if (pr->postponed) {
 
             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -2364,7 +2370,7 @@
     r = ctx->request;
 
     if (r) {
-        return r->log_handler(r, p, len);
+        return r->log_handler(r, ctx->current_request, p, len);
     }
 
     return p;
@@ -2372,9 +2378,12 @@
 
 
 static u_char *
-ngx_http_log_error_handler(ngx_http_request_t *r, u_char *buf, size_t len)
+ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
+    u_char *buf, size_t len)
 {
-    u_char  *p;
+    u_char                 *p;
+    ngx_http_upstream_t    *u;
+    ngx_peer_connection_t  *peer;
 
     if (r->server_name.data) {
         p = ngx_snprintf(buf, len, ", server: %V", &r->server_name);
@@ -2406,6 +2415,26 @@
         }
     }
 
+    if (r != sr) {
+        p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
+        len -= p - buf;
+        buf = p;
+    }
+
+    u = sr->upstream;
+
+    if (u) {
+        peer = &u->peer;
+
+        p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
+                         &u->conf->schema,
+                         &peer->peers->peer[peer->cur_peer].name,
+                         peer->peers->peer[peer->cur_peer].uri_separator,
+                         &u->uri);
+        len -= p - buf;
+        buf = p;
+    }
+
     return ngx_http_log_error_info(r, buf, len);
 }
 
diff --git a/src/http/ngx_http_request_body.c b/src/http/ngx_http_request_body.c
index c6fa3fc..5cc4d52 100644
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -92,7 +92,7 @@
         rb->bufs->buf = b;
         rb->bufs->next = NULL;
 
-        if (preread >= r->headers_in.content_length_n) {
+        if ((off_t) preread >= r->headers_in.content_length_n) {
 
             /* the whole request body was pre-read */
 
@@ -120,7 +120,7 @@
 
         rb->rest = r->headers_in.content_length_n - preread;
 
-        if (rb->rest <= (size_t) (b->end - b->last)) {
+        if (rb->rest <= (off_t) (b->end - b->last)) {
 
             /* the whole request body may be placed in r->header_in */
 
@@ -242,7 +242,7 @@
 
         size = rb->buf->end - rb->buf->last;
 
-        if (size > rb->rest) {
+        if ((off_t) size > rb->rest) {
             size = (size_t) rb->rest;
         }
 
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 782c14b..5caad00 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -78,9 +78,6 @@
 static u_char *ngx_http_upstream_log_response_time(ngx_http_request_t *r,
     u_char *buf, ngx_http_log_op_t *op);
 
-static u_char *ngx_http_upstream_log_error(ngx_http_request_t *r, u_char *buf,
-    size_t len);
-
 static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf);
 static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
@@ -293,7 +290,6 @@
 void
 ngx_http_upstream_init(ngx_http_request_t *r)
 {
-    ngx_time_t                *tp;
     ngx_connection_t          *c;
     ngx_http_cleanup_t        *cln;
     ngx_http_upstream_t       *u;
@@ -337,8 +333,6 @@
     }
 
     u->peer.log = r->connection->log;
-    u->saved_log_handler = r->log_handler;
-    r->log_handler = ngx_http_upstream_log_error;
 
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
@@ -359,18 +353,6 @@
         return;
     }
 
-    u->state = ngx_array_push(&u->states);
-    if (u->state == NULL) {
-        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
-        return;
-    }
-
-    ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
-
-    tp = ngx_timeofday();
-
-    u->state->response_time = tp->sec * 1000 + tp->msec;
-
     cln = ngx_http_cleanup_add(r, 0);
     if (cln == NULL) {
         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
@@ -528,12 +510,31 @@
 ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
 {
     ngx_int_t          rc;
+    ngx_time_t        *tp;
     ngx_connection_t  *c;
 
     r->connection->log->action = "connecting to upstream";
 
     r->connection->single_connection = 0;
 
+    if (u->state && u->state->response_time) {
+        tp = ngx_timeofday();
+        u->state->response_time = tp->sec * 1000 + tp->msec
+                                  - u->state->response_time;
+    }
+
+    u->state = ngx_array_push(&u->states);
+    if (u->state == NULL) {
+        ngx_http_upstream_finalize_request(r, u,
+                                           NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return;
+    }
+
+    ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
+
+    tp = ngx_timeofday();
+    u->state->response_time = tp->sec * 1000 + tp->msec;
+
     rc = ngx_event_connect_peer(&u->peer);
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -761,15 +762,6 @@
 
 #endif
 
-    /* add one more state */
-
-    u->state = ngx_array_push(&u->states);
-    if (u->state == NULL) {
-        return NGX_ERROR;
-    }
-
-    ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
-
     return NGX_OK;
 }
 
@@ -1988,8 +1980,6 @@
     }
 #endif
 
-    r->log_handler = u->saved_log_handler;
-
     if (rc == NGX_DECLINED) {
         return;
     }
@@ -2457,32 +2447,6 @@
 }
 
 
-static u_char *
-ngx_http_upstream_log_error(ngx_http_request_t *r, u_char *buf, size_t len)
-{
-    u_char                 *p;
-    ngx_http_upstream_t    *u;
-    ngx_peer_connection_t  *peer;
-
-    u = r->upstream;
-    peer = &u->peer;
-
-    p = ngx_snprintf(buf, len,
-                     ", server: %V, URL: \"%V\","
-                     " upstream: \"%V%V%s%V\"",
-                     &r->server_name,
-                     &r->unparsed_uri,
-                     &u->conf->schema,
-                     &peer->peers->peer[peer->cur_peer].name,
-                     peer->peers->peer[peer->cur_peer].uri_separator,
-                     &u->uri);
-    len -= p - buf;
-    buf = p;
-
-    return ngx_http_log_error_info(r, buf, len);
-}
-
-
 static ngx_int_t
 ngx_http_upstream_add_variables(ngx_conf_t *cf)
 {
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 086d184..8c2a901 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -16,14 +16,15 @@
 #include <ngx_http.h>
 
 
-#define NGX_HTTP_UPSTREAM_FT_ERROR           0x002
-#define NGX_HTTP_UPSTREAM_FT_TIMEOUT         0x004
-#define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER  0x008
-#define NGX_HTTP_UPSTREAM_FT_HTTP_500        0x010
-#define NGX_HTTP_UPSTREAM_FT_HTTP_503        0x020
-#define NGX_HTTP_UPSTREAM_FT_HTTP_404        0x040
-#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x080
-#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x100
+#define NGX_HTTP_UPSTREAM_FT_ERROR           0x00000002
+#define NGX_HTTP_UPSTREAM_FT_TIMEOUT         0x00000004
+#define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER  0x00000008
+#define NGX_HTTP_UPSTREAM_FT_HTTP_500        0x00000010
+#define NGX_HTTP_UPSTREAM_FT_HTTP_503        0x00000020
+#define NGX_HTTP_UPSTREAM_FT_HTTP_404        0x00000040
+#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00000080
+#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00000100
+#define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000
 
 
 #define NGX_HTTP_UPSTREAM_INVALID_HEADER     40
@@ -193,8 +194,6 @@
 
     ngx_str_t                       method;
 
-    ngx_http_log_handler_pt         saved_log_handler;
-
     ngx_http_upstream_state_t      *state;
     ngx_array_t                     states;  /* of ngx_http_upstream_state_t */