[PATCH 1 of 1] Upstream: add propagate_connection_close directive

Shawn J. Goff shawgoff at amazon.com
Sat Jan 3 00:03:59 UTC 2015


# HG changeset patch
# User Shawn J. Goff <shawgoff at amazon.com>
# Date 1418438144 28800
# Node ID 739882cc36a6b52b6340d2f1b63e98fb47f4ab8b
# Parent  e9effef98874c619326ec11e25b11333225cf797
Upstream: add propagate_connection_close directive.

This directive ensures that if the upstream response includes a "Connection:
close" header, the response sent back to the downstream client also has the
"Connection: close" header and the downstream connection is closed. This is
useful where the upstream service handles closing connections to keep the load
balanced with the rest of the fleet.

propagate_connection_close is off by default to keep existing behavior.

diff -r e9effef98874 -r 739882cc36a6 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:59 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Fri Dec 12 18:35:44 2014 -0800
@@ -159,6 +159,9 @@
 
 static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf);
 static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
+static void *ngx_http_upstream_create_loc_conf(ngx_conf_t *cf);
+static char *ngx_http_upstream_merge_loc_conf(ngx_conf_t *cf,
+    void *parent, void *child);
 
 #if (NGX_HTTP_SSL)
 static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
@@ -314,6 +317,13 @@
       0,
       NULL },
 
+    { ngx_string("propagate_connection_close"),
+      NGX_HTTP_LOC_CONF|NGX_HTTP_UPS_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_upstream_loc_conf_t, propagate_close),
+      NULL },
+
       ngx_null_command
 };
 
@@ -328,8 +338,8 @@
     NULL,                                  /* create server configuration */
     NULL,                                  /* merge server configuration */
 
-    NULL,                                  /* create location configuration */
-    NULL                                   /* merge location configuration */
+    ngx_http_upstream_create_loc_conf,     /* create location configuration */
+    ngx_http_upstream_merge_loc_conf       /* merge location configuration */
 };
 
 
@@ -1834,9 +1844,12 @@
 static void
 ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
 {
-    ssize_t            n;
-    ngx_int_t          rc;
-    ngx_connection_t  *c;
+    ssize_t                        n;
+    ngx_int_t                      rc;
+    ngx_connection_t              *c;
+    ngx_http_upstream_loc_conf_t  *ulcf;
+
+    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upstream_module);
 
     c = u->peer.connection;
 
@@ -1967,6 +1980,10 @@
         }
     }
 
+    if (ulcf->propagate_close == 1 && u->headers_in.connection_close == 1){
+        r->keepalive = 0;
+    }
+
     if (ngx_http_upstream_process_headers(r, u) != NGX_OK) {
         return;
     }
@@ -5778,3 +5795,30 @@
 
     return NGX_CONF_OK;
 }
+
+
+static void *
+ngx_http_upstream_create_loc_conf(ngx_conf_t *cf)
+{
+    ngx_http_upstream_loc_conf_t  *conf;
+
+    conf = ngx_palloc(cf->pool, sizeof(ngx_http_upstream_loc_conf_t));
+    if (conf == NULL) {
+        return NULL;
+    }
+
+    conf->propagate_close = NGX_CONF_UNSET;
+    return conf;
+}
+
+
+static char *
+ngx_http_upstream_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+    ngx_http_upstream_loc_conf_t *prev = parent;
+    ngx_http_upstream_loc_conf_t *conf = child;
+
+    ngx_conf_merge_value(conf->propagate_close, prev->propagate_close, 0);
+
+    return NGX_CONF_OK;
+}
diff -r e9effef98874 -r 739882cc36a6 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h	Fri Dec 26 16:22:59 2014 +0300
+++ b/src/http/ngx_http_upstream.h	Fri Dec 12 18:35:44 2014 -0800
@@ -72,6 +72,11 @@
                                              /* ngx_http_upstream_srv_conf_t */
 } ngx_http_upstream_main_conf_t;
 
+
+typedef struct {
+    ngx_flag_t propagate_close;
+} ngx_http_upstream_loc_conf_t;
+
 typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;
 
 typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,



More information about the nginx-devel mailing list