HTTP: add connect_timeout for proxy_next_upstream

吕海涛 lv at haitao.dev
Sun Mar 24 08:57:54 UTC 2019


commit c6f872f4dfa4abecc953f09cdb3e1537d866d2f4
Author: 吕海涛 <lv at haitao.dev>
Date:   Sun Mar 24 16:46:56 2019 +0800

    HTTP: add connect_timeout for proxy_next_upstream
    
    Nginx support both timeout and non_idempotent flag for
    proxy_next_upstream.  However, in some environment,
    even the GET method is non_idempotent.
    
    We need nginx try the next upstream if and only if the
    tcp connect is timeout.

diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index 3aafb999..6a5419e3 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -212,6 +212,7 @@ static ngx_conf_post_t  ngx_http_proxy_lowat_post =
 static ngx_conf_bitmask_t  ngx_http_proxy_next_upstream_masks[] = {
     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
+    { ngx_string("connect_timeout"), NGX_HTTP_UPSTREAM_FT_CONNECT_TIMEOUT },
     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index a7391d09..b491db6d 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -4158,8 +4158,17 @@ ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
     }
 
     if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
-        ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
-                      "upstream timed out");
+        if (!u->request_sent
+            && (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_CONNECT_TIMEOUT))
+        {
+            ft_type = NGX_HTTP_UPSTREAM_FT_CONNECT_TIMEOUT;
+
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
+                          "upstream connect timed out");
+        } else {
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
+                          "upstream timed out");
+        }
     }
 
     if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
@@ -4171,6 +4180,7 @@ ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
 
     case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
     case NGX_HTTP_UPSTREAM_FT_HTTP_504:
+    case NGX_HTTP_UPSTREAM_FT_CONNECT_TIMEOUT:
         status = NGX_HTTP_GATEWAY_TIME_OUT;
         break;
 
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 6079d723..8449b463 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -31,6 +31,7 @@
 #define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00001000
 #define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00002000
 #define NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT  0x00004000
+#define NGX_HTTP_UPSTREAM_FT_CONNECT_TIMEOUT 0x00010000
 #define NGX_HTTP_UPSTREAM_FT_NOLIVE          0x40000000
 #define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000
 





More information about the nginx-devel mailing list