[nginx] Upstream: proxy_socket_keepalive and friends.

Vladimir Homutov vl at nginx.com
Wed Oct 3 15:09:12 UTC 2018


details:   http://hg.nginx.org/nginx/rev/8b68d50090e4
branches:  
changeset: 7371:8b68d50090e4
user:      Vladimir Homutov <vl at nginx.com>
date:      Wed Oct 03 14:08:51 2018 +0300
description:
Upstream: proxy_socket_keepalive and friends.

The directives enable the use of the SO_KEEPALIVE option on
upstream connections.  By default, the value is left unchanged.

diffstat:

 src/event/ngx_event_connect.c                |  14 +++++++++++++-
 src/event/ngx_event_connect.h                |   1 +
 src/http/modules/ngx_http_fastcgi_module.c   |  11 +++++++++++
 src/http/modules/ngx_http_grpc_module.c      |  11 +++++++++++
 src/http/modules/ngx_http_memcached_module.c |  11 +++++++++++
 src/http/modules/ngx_http_proxy_module.c     |  11 +++++++++++
 src/http/modules/ngx_http_scgi_module.c      |  11 +++++++++++
 src/http/modules/ngx_http_uwsgi_module.c     |  11 +++++++++++
 src/http/ngx_http_upstream.c                 |   4 ++++
 src/http/ngx_http_upstream.h                 |   1 +
 src/stream/ngx_stream_proxy_module.c         |  16 ++++++++++++++++
 11 files changed, 101 insertions(+), 1 deletions(-)

diffs (331 lines):

diff -r aa1c9f846567 -r 8b68d50090e4 src/event/ngx_event_connect.c
--- a/src/event/ngx_event_connect.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/event/ngx_event_connect.c	Wed Oct 03 14:08:51 2018 +0300
@@ -20,7 +20,7 @@ static ngx_int_t ngx_event_connect_set_t
 ngx_int_t
 ngx_event_connect_peer(ngx_peer_connection_t *pc)
 {
-    int                rc, type;
+    int                rc, type, value;
 #if (NGX_HAVE_IP_BIND_ADDRESS_NO_PORT || NGX_LINUX)
     in_port_t          port;
 #endif
@@ -73,6 +73,18 @@ ngx_event_connect_peer(ngx_peer_connecti
         }
     }
 
+    if (pc->so_keepalive) {
+        value = 1;
+
+        if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
+                       (const void *) &value, sizeof(int))
+            == -1)
+        {
+            ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
+                          "setsockopt(SO_KEEPALIVE) failed, ignored");
+        }
+    }
+
     if (ngx_nonblocking(s) == -1) {
         ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
                       ngx_nonblocking_n " failed");
diff -r aa1c9f846567 -r 8b68d50090e4 src/event/ngx_event_connect.h
--- a/src/event/ngx_event_connect.h	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/event/ngx_event_connect.h	Wed Oct 03 14:08:51 2018 +0300
@@ -62,6 +62,7 @@ struct ngx_peer_connection_s {
 
     unsigned                         cached:1;
     unsigned                         transparent:1;
+    unsigned                         so_keepalive:1;
 
                                      /* ngx_connection_log_error_e */
     unsigned                         log_error:2;
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_fastcgi_module.c
--- a/src/http/modules/ngx_http_fastcgi_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_fastcgi_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -286,6 +286,13 @@ static ngx_command_t  ngx_http_fastcgi_c
       offsetof(ngx_http_fastcgi_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("fastcgi_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("fastcgi_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -2721,6 +2728,7 @@ ngx_http_fastcgi_create_loc_conf(ngx_con
     conf->upstream.force_ranges = NGX_CONF_UNSET;
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
 
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -2824,6 +2832,9 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
                               prev->upstream.connect_timeout, 60000);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_grpc_module.c
--- a/src/http/modules/ngx_http_grpc_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_grpc_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -248,6 +248,13 @@ static ngx_command_t  ngx_http_grpc_comm
       offsetof(ngx_http_grpc_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("grpc_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("grpc_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -4150,6 +4157,7 @@ ngx_http_grpc_create_loc_conf(ngx_conf_t
      */
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -4205,6 +4213,9 @@ ngx_http_grpc_merge_loc_conf(ngx_conf_t 
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
                               prev->upstream.next_upstream_tries, 0);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_memcached_module.c
--- a/src/http/modules/ngx_http_memcached_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_memcached_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -67,6 +67,13 @@ static ngx_command_t  ngx_http_memcached
       offsetof(ngx_http_memcached_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("memcached_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("memcached_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -595,6 +602,7 @@ ngx_http_memcached_create_loc_conf(ngx_c
      */
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -634,6 +642,9 @@ ngx_http_memcached_merge_loc_conf(ngx_co
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
                               prev->upstream.next_upstream_tries, 0);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_proxy_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -324,6 +324,13 @@ static ngx_command_t  ngx_http_proxy_com
       offsetof(ngx_http_proxy_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("proxy_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("proxy_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -2833,6 +2840,7 @@ ngx_http_proxy_create_loc_conf(ngx_conf_
     conf->upstream.force_ranges = NGX_CONF_UNSET;
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
 
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -2953,6 +2961,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
                               prev->upstream.connect_timeout, 60000);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_scgi_module.c
--- a/src/http/modules/ngx_http_scgi_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_scgi_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -143,6 +143,13 @@ static ngx_command_t ngx_http_scgi_comma
       offsetof(ngx_http_scgi_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("scgi_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("scgi_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -1200,6 +1207,7 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t
     conf->upstream.force_ranges = NGX_CONF_UNSET;
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
 
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -1298,6 +1306,9 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t 
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
                               prev->upstream.connect_timeout, 60000);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/modules/ngx_http_uwsgi_module.c
--- a/src/http/modules/ngx_http_uwsgi_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/modules/ngx_http_uwsgi_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -204,6 +204,13 @@ static ngx_command_t ngx_http_uwsgi_comm
       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.local),
       NULL },
 
+    { ngx_string("uwsgi_socket_keepalive"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_keepalive),
+      NULL },
+
     { ngx_string("uwsgi_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -1413,6 +1420,7 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_
     conf->upstream.force_ranges = NGX_CONF_UNSET;
 
     conf->upstream.local = NGX_CONF_UNSET_PTR;
+    conf->upstream.socket_keepalive = NGX_CONF_UNSET;
 
     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
@@ -1519,6 +1527,9 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t
     ngx_conf_merge_ptr_value(conf->upstream.local,
                               prev->upstream.local, NULL);
 
+    ngx_conf_merge_value(conf->upstream.socket_keepalive,
+                              prev->upstream.socket_keepalive, 0);
+
     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
                               prev->upstream.connect_timeout, 60000);
 
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/ngx_http_upstream.c	Wed Oct 03 14:08:51 2018 +0300
@@ -628,6 +628,10 @@ ngx_http_upstream_init_request(ngx_http_
         return;
     }
 
+    if (u->conf->socket_keepalive) {
+        u->peer.so_keepalive = 1;
+    }
+
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
     u->output.alignment = clcf->directio_alignment;
diff -r aa1c9f846567 -r 8b68d50090e4 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/http/ngx_http_upstream.h	Wed Oct 03 14:08:51 2018 +0300
@@ -188,6 +188,7 @@ typedef struct {
     ngx_array_t                     *pass_headers;
 
     ngx_http_upstream_local_t       *local;
+    ngx_flag_t                       socket_keepalive;
 
 #if (NGX_HTTP_CACHE)
     ngx_shm_zone_t                  *cache_zone;
diff -r aa1c9f846567 -r 8b68d50090e4 src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Wed Oct 03 17:02:44 2018 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Wed Oct 03 14:08:51 2018 +0300
@@ -31,6 +31,7 @@ typedef struct {
     ngx_flag_t                       next_upstream;
     ngx_flag_t                       proxy_protocol;
     ngx_stream_upstream_local_t     *local;
+    ngx_flag_t                       socket_keepalive;
 
 #if (NGX_STREAM_SSL)
     ngx_flag_t                       ssl_enable;
@@ -136,6 +137,13 @@ static ngx_command_t  ngx_stream_proxy_c
       0,
       NULL },
 
+    { ngx_string("proxy_socket_keepalive"),
+      NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_STREAM_SRV_CONF_OFFSET,
+      offsetof(ngx_stream_proxy_srv_conf_t, socket_keepalive),
+      NULL },
+
     { ngx_string("proxy_connect_timeout"),
       NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -388,6 +396,10 @@ ngx_stream_proxy_handler(ngx_stream_sess
         return;
     }
 
+    if (pscf->socket_keepalive) {
+        u->peer.so_keepalive = 1;
+    }
+
     u->peer.type = c->type;
     u->start_sec = ngx_time();
 
@@ -1898,6 +1910,7 @@ ngx_stream_proxy_create_srv_conf(ngx_con
     conf->next_upstream = NGX_CONF_UNSET;
     conf->proxy_protocol = NGX_CONF_UNSET;
     conf->local = NGX_CONF_UNSET_PTR;
+    conf->socket_keepalive = NGX_CONF_UNSET;
 
 #if (NGX_STREAM_SSL)
     conf->ssl_enable = NGX_CONF_UNSET;
@@ -1948,6 +1961,9 @@ ngx_stream_proxy_merge_srv_conf(ngx_conf
 
     ngx_conf_merge_ptr_value(conf->local, prev->local, NULL);
 
+    ngx_conf_merge_value(conf->socket_keepalive,
+                              prev->socket_keepalive, 0);
+
 #if (NGX_STREAM_SSL)
 
     ngx_conf_merge_value(conf->ssl_enable, prev->ssl_enable, 0);


More information about the nginx-devel mailing list