From 224aeafad4af66d4e203e669fd44488517d410d6 Mon Sep 17 00:00:00 2001 From: Patrik Wall Date: Thu, 30 Apr 2026 14:43:49 +0200 Subject: [PATCH 1/5] Events: support for SO_SNDBUF on outbound peer connections. ngx_event_connect_peer() honors SO_RCVBUF via pc->rcvbuf but had no equivalent for SO_SNDBUF. This adds pc->sndbuf and the matching setsockopt() call, mirroring the existing SO_RCVBUF path. Existing callers leaving sndbuf at 0 retain prior behavior, since setsockopt() is only invoked when the field is non-zero. The new field is consumed by the stream proxy module in a follow-up commit. --- src/event/ngx_event_connect.c | 10 ++++++++++ src/event/ngx_event_connect.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c index 668084a7b..a021f567b 100644 --- a/src/event/ngx_event_connect.c +++ b/src/event/ngx_event_connect.c @@ -73,6 +73,16 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc) } } + if (pc->sndbuf) { + if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, + (const void *) &pc->sndbuf, sizeof(int)) == -1) + { + ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno, + "setsockopt(SO_SNDBUF) failed"); + goto failed; + } + } + if (pc->so_keepalive) { value = 1; diff --git a/src/event/ngx_event_connect.h b/src/event/ngx_event_connect.h index e428e7376..0604fef0f 100644 --- a/src/event/ngx_event_connect.h +++ b/src/event/ngx_event_connect.h @@ -57,6 +57,7 @@ struct ngx_peer_connection_s { int type; int rcvbuf; + int sndbuf; ngx_log_t *log; From 9f30b88cbb23f7e2b8a6fc205da50861983558f5 Mon Sep 17 00:00:00 2001 From: Patrik Wall Date: Mon, 27 Apr 2026 13:22:16 +0200 Subject: [PATCH 2/5] Stream: proxy_socket_rcvbuf and proxy_socket_sndbuf directives. Until now, the only way to size socket buffers in nginx was the "sndbuf="/"rcvbuf=" parameters of the listen directive, which only affect listener-derived sockets. Outbound sockets created for proxy_pass therefore always used the OS default, which is small when TCP buffer autotuning is disabled. Both directives accept a size, the literal "max" (sets the buffer to INT_MAX so that the kernel clamps to its configured maximum, e.g. wmem_max on Linux or net.inet.tcp.{send,recv}buf_max on FreeBSD and macOS), or "off" (default; do not call setsockopt). The directives populate pc->rcvbuf and pc->sndbuf at upstream connect time; SO_SNDBUF use requires the support added to ngx_event_connect_peer() in the preceding commit. Closes: https://github.com/nginx/nginx/issues/1297 --- src/stream/ngx_stream_proxy_module.c | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/stream/ngx_stream_proxy_module.c b/src/stream/ngx_stream_proxy_module.c index 22826ef79..90b8d0340 100644 --- a/src/stream/ngx_stream_proxy_module.c +++ b/src/stream/ngx_stream_proxy_module.c @@ -34,6 +34,8 @@ typedef struct { ngx_flag_t half_close; ngx_stream_upstream_local_t *local; ngx_flag_t socket_keepalive; + ngx_int_t socket_rcvbuf; + ngx_int_t socket_sndbuf; #if (NGX_STREAM_SSL) ngx_flag_t ssl_enable; @@ -92,6 +94,8 @@ static char *ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); #if (NGX_STREAM_SSL) @@ -166,6 +170,20 @@ static ngx_command_t ngx_stream_proxy_commands[] = { offsetof(ngx_stream_proxy_srv_conf_t, socket_keepalive), NULL }, + { ngx_string("proxy_socket_rcvbuf"), + NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, + ngx_stream_proxy_socket_buf, + NGX_STREAM_SRV_CONF_OFFSET, + offsetof(ngx_stream_proxy_srv_conf_t, socket_rcvbuf), + NULL }, + + { ngx_string("proxy_socket_sndbuf"), + NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, + ngx_stream_proxy_socket_buf, + NGX_STREAM_SRV_CONF_OFFSET, + offsetof(ngx_stream_proxy_srv_conf_t, socket_sndbuf), + NULL }, + { ngx_string("proxy_connect_timeout"), NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, @@ -457,6 +475,9 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s) u->peer.so_keepalive = 1; } + u->peer.rcvbuf = (int) pscf->socket_rcvbuf; + u->peer.sndbuf = (int) pscf->socket_sndbuf; + u->peer.type = c->type; u->start_sec = ngx_time(); @@ -2381,6 +2402,8 @@ ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf) conf->proxy_protocol = NGX_CONF_UNSET; conf->local = NGX_CONF_UNSET_PTR; conf->socket_keepalive = NGX_CONF_UNSET; + conf->socket_rcvbuf = NGX_CONF_UNSET; + conf->socket_sndbuf = NGX_CONF_UNSET; conf->half_close = NGX_CONF_UNSET; #if (NGX_STREAM_SSL) @@ -2442,6 +2465,10 @@ ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->socket_keepalive, prev->socket_keepalive, 0); + ngx_conf_merge_value(conf->socket_rcvbuf, prev->socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->socket_sndbuf, prev->socket_sndbuf, 0); + ngx_conf_merge_value(conf->half_close, prev->half_close, 0); #if (NGX_STREAM_SSL) @@ -2847,3 +2874,44 @@ ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } + + +static char * +ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + char *p = conf; + + ngx_int_t *np; + ngx_str_t *value; + ssize_t size; + + np = (ngx_int_t *) (p + cmd->offset); + + if (*np != NGX_CONF_UNSET) { + return "is duplicate"; + } + + value = cf->args->elts; + + if (ngx_strcmp(value[1].data, "off") == 0) { + *np = 0; + return NGX_CONF_OK; + } + + if (ngx_strcmp(value[1].data, "max") == 0) { + *np = NGX_MAX_INT32_VALUE; + return NGX_CONF_OK; + } + + size = ngx_parse_size(&value[1]); + + if (size == NGX_ERROR || size <= 0 || size > NGX_MAX_INT32_VALUE) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "invalid value \"%V\"", &value[1]); + return NGX_CONF_ERROR; + } + + *np = (ngx_int_t) size; + + return NGX_CONF_OK; +} From 54475ac261c60ff930667989b9997487682c1ddc Mon Sep 17 00:00:00 2001 From: Patrik Wall Date: Wed, 13 May 2026 13:49:00 +0200 Subject: [PATCH 3/5] HTTP: upstream socket buffer directives. The directives mirror the stream proxy directives and accept a size, "max", or "off". The configured values are propagated to pc->rcvbuf and pc->sndbuf when creating upstream peer connections. --- src/event/ngx_event_connect.h | 7 +-- src/http/modules/ngx_http_fastcgi_module.c | 22 ++++++++++ src/http/modules/ngx_http_grpc_module.c | 22 ++++++++++ src/http/modules/ngx_http_memcached_module.c | 22 ++++++++++ src/http/modules/ngx_http_proxy_module.c | 22 ++++++++++ src/http/modules/ngx_http_scgi_module.c | 22 ++++++++++ src/http/modules/ngx_http_uwsgi_module.c | 22 ++++++++++ src/http/ngx_http_upstream.c | 45 ++++++++++++++++++++ src/http/ngx_http_upstream.h | 4 ++ src/stream/ngx_stream_proxy_module.c | 18 ++++---- 10 files changed, 194 insertions(+), 12 deletions(-) diff --git a/src/event/ngx_event_connect.h b/src/event/ngx_event_connect.h index 0604fef0f..25c5bb09b 100644 --- a/src/event/ngx_event_connect.h +++ b/src/event/ngx_event_connect.h @@ -57,7 +57,6 @@ struct ngx_peer_connection_s { int type; int rcvbuf; - int sndbuf; ngx_log_t *log; @@ -74,8 +73,10 @@ struct ngx_peer_connection_s { /* ngx_connection_log_error_e */ unsigned log_error:2; - NGX_COMPAT_BEGIN(1) - NGX_COMPAT_END +#if (NGX_COMPAT) + int spare; +#endif + int sndbuf; }; diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c index f7f0696a8..94f202f3a 100644 --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -296,6 +296,20 @@ static ngx_command_t ngx_http_fastcgi_commands[] = { offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("fastcgi_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("fastcgi_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -2899,6 +2913,8 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -3005,6 +3021,12 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); diff --git a/src/http/modules/ngx_http_grpc_module.c b/src/http/modules/ngx_http_grpc_module.c index 32bdc964c..ad0b50a9f 100644 --- a/src/http/modules/ngx_http_grpc_module.c +++ b/src/http/modules/ngx_http_grpc_module.c @@ -276,6 +276,20 @@ static ngx_command_t ngx_http_grpc_commands[] = { offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("grpc_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("grpc_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -4438,6 +4452,8 @@ ngx_http_grpc_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = 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; @@ -4504,6 +4520,12 @@ ngx_http_grpc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries, prev->upstream.next_upstream_tries, 0); diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c index 11bbd9165..40627e700 100644 --- a/src/http/modules/ngx_http_memcached_module.c +++ b/src/http/modules/ngx_http_memcached_module.c @@ -74,6 +74,20 @@ static ngx_command_t ngx_http_memcached_commands[] = { offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("memcached_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("memcached_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -607,6 +621,8 @@ ngx_http_memcached_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = 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; @@ -649,6 +665,12 @@ ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries, prev->upstream.next_upstream_tries, 0); diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c index 7e08df702..a1e44c896 100644 --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -291,6 +291,20 @@ static ngx_command_t ngx_http_proxy_commands[] = { offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("proxy_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("proxy_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -3527,6 +3541,8 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -3662,6 +3678,12 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c index 406b5f88f..015ff1707 100644 --- a/src/http/modules/ngx_http_scgi_module.c +++ b/src/http/modules/ngx_http_scgi_module.c @@ -151,6 +151,20 @@ static ngx_command_t ngx_http_scgi_commands[] = { offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("scgi_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("scgi_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -1317,6 +1331,8 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -1418,6 +1434,12 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c index cb03ca77c..63a24aa43 100644 --- a/src/http/modules/ngx_http_uwsgi_module.c +++ b/src/http/modules/ngx_http_uwsgi_module.c @@ -219,6 +219,20 @@ static ngx_command_t ngx_http_uwsgi_commands[] = { offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("uwsgi_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("uwsgi_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_sndbuf), + 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, @@ -1566,6 +1580,8 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -1680,6 +1696,12 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c index bd6ae5f07..850412d6d 100644 --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -680,6 +680,9 @@ ngx_http_upstream_init_request(ngx_http_request_t *r) u->peer.so_keepalive = 1; } + u->peer.rcvbuf = u->conf->socket_rcvbuf; + u->peer.sndbuf = u->conf->socket_sndbuf; + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); u->output.alignment = clcf->directio_alignment; @@ -6985,6 +6988,48 @@ ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, } +char * +ngx_http_upstream_socket_buffer_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + char *p = conf; + + int *np; + ngx_str_t *value; + ssize_t size; + + np = (int *) (p + cmd->offset); + + if (*np != NGX_CONF_UNSET) { + return "is duplicate"; + } + + value = cf->args->elts; + + if (ngx_strcmp(value[1].data, "off") == 0) { + *np = 0; + return NGX_CONF_OK; + } + + if (ngx_strcmp(value[1].data, "max") == 0) { + *np = NGX_MAX_INT32_VALUE; + return NGX_CONF_OK; + } + + size = ngx_parse_size(&value[1]); + + if (size == NGX_ERROR || size <= 0 || size > NGX_MAX_INT32_VALUE) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "invalid value \"%V\"", &value[1]); + return NGX_CONF_ERROR; + } + + *np = (int) size; + + return NGX_CONF_OK; +} + + static ngx_int_t ngx_http_upstream_set_local(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_upstream_local_t *local) diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h index 4560bbe9a..54d49e919 100644 --- a/src/http/ngx_http_upstream.h +++ b/src/http/ngx_http_upstream.h @@ -257,6 +257,8 @@ typedef struct { #endif ngx_str_t module; + int socket_rcvbuf; + int socket_sndbuf; NGX_COMPAT_BEGIN(5) NGX_COMPAT_END @@ -442,6 +444,8 @@ ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags); char *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +char *ngx_http_upstream_socket_buffer_set_slot(ngx_conf_t *cf, + ngx_command_t *cmd, void *conf); char *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf, diff --git a/src/stream/ngx_stream_proxy_module.c b/src/stream/ngx_stream_proxy_module.c index 90b8d0340..d2a9b1901 100644 --- a/src/stream/ngx_stream_proxy_module.c +++ b/src/stream/ngx_stream_proxy_module.c @@ -34,8 +34,8 @@ typedef struct { ngx_flag_t half_close; ngx_stream_upstream_local_t *local; ngx_flag_t socket_keepalive; - ngx_int_t socket_rcvbuf; - ngx_int_t socket_sndbuf; + int socket_rcvbuf; + int socket_sndbuf; #if (NGX_STREAM_SSL) ngx_flag_t ssl_enable; @@ -475,8 +475,8 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s) u->peer.so_keepalive = 1; } - u->peer.rcvbuf = (int) pscf->socket_rcvbuf; - u->peer.sndbuf = (int) pscf->socket_sndbuf; + u->peer.rcvbuf = pscf->socket_rcvbuf; + u->peer.sndbuf = pscf->socket_sndbuf; u->peer.type = c->type; u->start_sec = ngx_time(); @@ -2881,11 +2881,11 @@ ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { char *p = conf; - ngx_int_t *np; - ngx_str_t *value; - ssize_t size; + int *np; + ngx_str_t *value; + ssize_t size; - np = (ngx_int_t *) (p + cmd->offset); + np = (int *) (p + cmd->offset); if (*np != NGX_CONF_UNSET) { return "is duplicate"; @@ -2911,7 +2911,7 @@ ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - *np = (ngx_int_t) size; + *np = (int) size; return NGX_CONF_OK; } From 854ea99716aa451aa6082a42552065fb8e481c6c Mon Sep 17 00:00:00 2001 From: Patrik Wall Date: Wed, 13 May 2026 14:48:42 +0200 Subject: [PATCH 4/5] HTTP: tunnel socket buffer directives. The tunnel module uses HTTP upstream peer connections, so add the matching tunnel_socket_rcvbuf and tunnel_socket_sndbuf directives. --- src/http/modules/ngx_http_tunnel_module.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/http/modules/ngx_http_tunnel_module.c b/src/http/modules/ngx_http_tunnel_module.c index a249fe270..106843142 100644 --- a/src/http/modules/ngx_http_tunnel_module.c +++ b/src/http/modules/ngx_http_tunnel_module.c @@ -73,6 +73,20 @@ static ngx_command_t ngx_http_tunnel_commands[] = { offsetof(ngx_http_tunnel_loc_conf_t, upstream.socket_keepalive), NULL }, + { ngx_string("tunnel_socket_rcvbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_tunnel_loc_conf_t, upstream.socket_rcvbuf), + NULL }, + + { ngx_string("tunnel_socket_sndbuf"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_socket_buffer_set_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_tunnel_loc_conf_t, upstream.socket_sndbuf), + NULL }, + { ngx_string("tunnel_connect_timeout"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, @@ -332,6 +346,8 @@ ngx_http_tunnel_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -366,6 +382,12 @@ ngx_http_tunnel_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); + ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + prev->upstream.socket_rcvbuf, 0); + + ngx_conf_merge_value(conf->upstream.socket_sndbuf, + prev->upstream.socket_sndbuf, 0); + ngx_conf_merge_msec_value(conf->upstream.connect_timeout, prev->upstream.connect_timeout, 60000); From 48777b5cc726b193a7d125ecdcccdb12fc2157c3 Mon Sep 17 00:00:00 2001 From: Anuj Date: Mon, 6 Jul 2026 13:12:32 +0000 Subject: [PATCH 5/5] Simplify: rcvbuf and sndbuf changes for outbound connection --- src/http/modules/ngx_http_fastcgi_module.c | 12 ++-- src/http/modules/ngx_http_grpc_module.c | 12 ++-- src/http/modules/ngx_http_memcached_module.c | 12 ++-- src/http/modules/ngx_http_proxy_module.c | 12 ++-- src/http/modules/ngx_http_scgi_module.c | 12 ++-- src/http/modules/ngx_http_tunnel_module.c | 12 ++-- src/http/modules/ngx_http_uwsgi_module.c | 12 ++-- src/http/ngx_http_upstream.c | 51 ++------------- src/http/ngx_http_upstream.h | 8 +-- src/stream/ngx_stream_proxy_module.c | 68 +++++--------------- 10 files changed, 67 insertions(+), 144 deletions(-) diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c index 94f202f3a..cb34dcc8d 100644 --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -298,14 +298,14 @@ static ngx_command_t ngx_http_fastcgi_commands[] = { { ngx_string("fastcgi_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("fastcgi_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_fastcgi_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -2913,8 +2913,8 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -3021,10 +3021,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, diff --git a/src/http/modules/ngx_http_grpc_module.c b/src/http/modules/ngx_http_grpc_module.c index ad0b50a9f..4af621b65 100644 --- a/src/http/modules/ngx_http_grpc_module.c +++ b/src/http/modules/ngx_http_grpc_module.c @@ -278,14 +278,14 @@ static ngx_command_t ngx_http_grpc_commands[] = { { ngx_string("grpc_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("grpc_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_grpc_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -4452,8 +4452,8 @@ ngx_http_grpc_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; 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; @@ -4520,10 +4520,10 @@ ngx_http_grpc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries, diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c index 40627e700..757a32f63 100644 --- a/src/http/modules/ngx_http_memcached_module.c +++ b/src/http/modules/ngx_http_memcached_module.c @@ -76,14 +76,14 @@ static ngx_command_t ngx_http_memcached_commands[] = { { ngx_string("memcached_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("memcached_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_memcached_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -621,8 +621,8 @@ ngx_http_memcached_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; 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; @@ -665,10 +665,10 @@ ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries, diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c index a1e44c896..8a9bffe10 100644 --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -293,14 +293,14 @@ static ngx_command_t ngx_http_proxy_commands[] = { { ngx_string("proxy_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("proxy_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -3541,8 +3541,8 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -3678,10 +3678,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c index 015ff1707..05cbdded0 100644 --- a/src/http/modules/ngx_http_scgi_module.c +++ b/src/http/modules/ngx_http_scgi_module.c @@ -153,14 +153,14 @@ static ngx_command_t ngx_http_scgi_commands[] = { { ngx_string("scgi_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("scgi_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -1331,8 +1331,8 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -1434,10 +1434,10 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, diff --git a/src/http/modules/ngx_http_tunnel_module.c b/src/http/modules/ngx_http_tunnel_module.c index 106843142..5925a6376 100644 --- a/src/http/modules/ngx_http_tunnel_module.c +++ b/src/http/modules/ngx_http_tunnel_module.c @@ -75,14 +75,14 @@ static ngx_command_t ngx_http_tunnel_commands[] = { { ngx_string("tunnel_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_tunnel_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("tunnel_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_tunnel_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -346,8 +346,8 @@ ngx_http_tunnel_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -382,10 +382,10 @@ ngx_http_tunnel_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c index 63a24aa43..f5841375f 100644 --- a/src/http/modules/ngx_http_uwsgi_module.c +++ b/src/http/modules/ngx_http_uwsgi_module.c @@ -221,14 +221,14 @@ static ngx_command_t ngx_http_uwsgi_commands[] = { { ngx_string("uwsgi_socket_rcvbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_rcvbuf), NULL }, { ngx_string("uwsgi_socket_sndbuf"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_http_upstream_socket_buffer_set_slot, + ngx_conf_set_size_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_sndbuf), NULL }, @@ -1580,8 +1580,8 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf) conf->upstream.local = NGX_CONF_UNSET_PTR; conf->upstream.socket_keepalive = NGX_CONF_UNSET; - conf->upstream.socket_rcvbuf = NGX_CONF_UNSET; - conf->upstream.socket_sndbuf = NGX_CONF_UNSET; + conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; @@ -1696,10 +1696,10 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->upstream.socket_keepalive, prev->upstream.socket_keepalive, 0); - ngx_conf_merge_value(conf->upstream.socket_rcvbuf, + ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf, prev->upstream.socket_rcvbuf, 0); - ngx_conf_merge_value(conf->upstream.socket_sndbuf, + ngx_conf_merge_size_value(conf->upstream.socket_sndbuf, prev->upstream.socket_sndbuf, 0); ngx_conf_merge_msec_value(conf->upstream.connect_timeout, diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c index 850412d6d..3f865345b 100644 --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -680,8 +680,13 @@ ngx_http_upstream_init_request(ngx_http_request_t *r) u->peer.so_keepalive = 1; } - u->peer.rcvbuf = u->conf->socket_rcvbuf; - u->peer.sndbuf = u->conf->socket_sndbuf; + if (u->conf->socket_rcvbuf) { + u->peer.rcvbuf = (int) u->conf->socket_rcvbuf; + } + + if (u->conf->socket_sndbuf) { + u->peer.sndbuf = (int) u->conf->socket_sndbuf; + } clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); @@ -6988,48 +6993,6 @@ ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, } -char * -ngx_http_upstream_socket_buffer_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, - void *conf) -{ - char *p = conf; - - int *np; - ngx_str_t *value; - ssize_t size; - - np = (int *) (p + cmd->offset); - - if (*np != NGX_CONF_UNSET) { - return "is duplicate"; - } - - value = cf->args->elts; - - if (ngx_strcmp(value[1].data, "off") == 0) { - *np = 0; - return NGX_CONF_OK; - } - - if (ngx_strcmp(value[1].data, "max") == 0) { - *np = NGX_MAX_INT32_VALUE; - return NGX_CONF_OK; - } - - size = ngx_parse_size(&value[1]); - - if (size == NGX_ERROR || size <= 0 || size > NGX_MAX_INT32_VALUE) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid value \"%V\"", &value[1]); - return NGX_CONF_ERROR; - } - - *np = (int) size; - - return NGX_CONF_OK; -} - - static ngx_int_t ngx_http_upstream_set_local(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_http_upstream_local_t *local) diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h index 54d49e919..f3f4066b8 100644 --- a/src/http/ngx_http_upstream.h +++ b/src/http/ngx_http_upstream.h @@ -257,10 +257,10 @@ typedef struct { #endif ngx_str_t module; - int socket_rcvbuf; - int socket_sndbuf; + size_t socket_rcvbuf; + size_t socket_sndbuf; - NGX_COMPAT_BEGIN(5) + NGX_COMPAT_BEGIN(3) NGX_COMPAT_END } ngx_http_upstream_conf_t; @@ -444,8 +444,6 @@ ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags); char *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -char *ngx_http_upstream_socket_buffer_set_slot(ngx_conf_t *cf, - ngx_command_t *cmd, void *conf); char *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf, diff --git a/src/stream/ngx_stream_proxy_module.c b/src/stream/ngx_stream_proxy_module.c index d2a9b1901..38c2b2489 100644 --- a/src/stream/ngx_stream_proxy_module.c +++ b/src/stream/ngx_stream_proxy_module.c @@ -34,8 +34,8 @@ typedef struct { ngx_flag_t half_close; ngx_stream_upstream_local_t *local; ngx_flag_t socket_keepalive; - int socket_rcvbuf; - int socket_sndbuf; + size_t socket_rcvbuf; + size_t socket_sndbuf; #if (NGX_STREAM_SSL) ngx_flag_t ssl_enable; @@ -94,8 +94,6 @@ static char *ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -static char *ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, - void *conf); #if (NGX_STREAM_SSL) @@ -172,14 +170,14 @@ static ngx_command_t ngx_stream_proxy_commands[] = { { ngx_string("proxy_socket_rcvbuf"), NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, - ngx_stream_proxy_socket_buf, + ngx_conf_set_size_slot, NGX_STREAM_SRV_CONF_OFFSET, offsetof(ngx_stream_proxy_srv_conf_t, socket_rcvbuf), NULL }, { ngx_string("proxy_socket_sndbuf"), NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, - ngx_stream_proxy_socket_buf, + ngx_conf_set_size_slot, NGX_STREAM_SRV_CONF_OFFSET, offsetof(ngx_stream_proxy_srv_conf_t, socket_sndbuf), NULL }, @@ -475,8 +473,13 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s) u->peer.so_keepalive = 1; } - u->peer.rcvbuf = pscf->socket_rcvbuf; - u->peer.sndbuf = pscf->socket_sndbuf; + if (pscf->socket_rcvbuf) { + u->peer.rcvbuf = (int) pscf->socket_rcvbuf; + } + + if (pscf->socket_sndbuf) { + u->peer.sndbuf = (int) pscf->socket_sndbuf; + } u->peer.type = c->type; u->start_sec = ngx_time(); @@ -2402,8 +2405,8 @@ ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf) conf->proxy_protocol = NGX_CONF_UNSET; conf->local = NGX_CONF_UNSET_PTR; conf->socket_keepalive = NGX_CONF_UNSET; - conf->socket_rcvbuf = NGX_CONF_UNSET; - conf->socket_sndbuf = NGX_CONF_UNSET; + conf->socket_rcvbuf = NGX_CONF_UNSET_SIZE; + conf->socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->half_close = NGX_CONF_UNSET; #if (NGX_STREAM_SSL) @@ -2465,9 +2468,9 @@ ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->socket_keepalive, prev->socket_keepalive, 0); - ngx_conf_merge_value(conf->socket_rcvbuf, prev->socket_rcvbuf, 0); + ngx_conf_merge_size_value(conf->socket_rcvbuf, prev->socket_rcvbuf, 0); - ngx_conf_merge_value(conf->socket_sndbuf, prev->socket_sndbuf, 0); + ngx_conf_merge_size_value(conf->socket_sndbuf, prev->socket_sndbuf, 0); ngx_conf_merge_value(conf->half_close, prev->half_close, 0); @@ -2874,44 +2877,3 @@ ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } - - -static char * -ngx_stream_proxy_socket_buf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) -{ - char *p = conf; - - int *np; - ngx_str_t *value; - ssize_t size; - - np = (int *) (p + cmd->offset); - - if (*np != NGX_CONF_UNSET) { - return "is duplicate"; - } - - value = cf->args->elts; - - if (ngx_strcmp(value[1].data, "off") == 0) { - *np = 0; - return NGX_CONF_OK; - } - - if (ngx_strcmp(value[1].data, "max") == 0) { - *np = NGX_MAX_INT32_VALUE; - return NGX_CONF_OK; - } - - size = ngx_parse_size(&value[1]); - - if (size == NGX_ERROR || size <= 0 || size > NGX_MAX_INT32_VALUE) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid value \"%V\"", &value[1]); - return NGX_CONF_ERROR; - } - - *np = (int) size; - - return NGX_CONF_OK; -}