From 6ab37e105c7a8cd0992d9bd1a35e0b192befefec Mon Sep 17 00:00:00 2001 From: Patrik Wall Date: Mon, 27 Apr 2026 13:22:16 +0200 Subject: [PATCH] Stream: proxy_socket_sndbuf and proxy_socket_rcvbuf directives Similarly to "listen ... sndbuf/rcvbuf", proxy_socket_sndbuf and proxy_socket_rcvbuf can be used to set the SO_SNDBUF and SO_RCVBUF socket options, respectively, on upstream connections. By default, the values are left unchanged. Closes: https://github.com/nginx/nginx/issues/1297 --- src/stream/ngx_stream_proxy_module.c | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/stream/ngx_stream_proxy_module.c b/src/stream/ngx_stream_proxy_module.c index 22826ef79..38c2b2489 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; + size_t socket_rcvbuf; + size_t socket_sndbuf; #if (NGX_STREAM_SSL) ngx_flag_t ssl_enable; @@ -166,6 +168,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_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_conf_set_size_slot, + 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 +473,14 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s) u->peer.so_keepalive = 1; } + 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(); @@ -2381,6 +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_SIZE; + conf->socket_sndbuf = NGX_CONF_UNSET_SIZE; conf->half_close = NGX_CONF_UNSET; #if (NGX_STREAM_SSL) @@ -2442,6 +2468,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_size_value(conf->socket_rcvbuf, prev->socket_rcvbuf, 0); + + ngx_conf_merge_size_value(conf->socket_sndbuf, prev->socket_sndbuf, 0); + ngx_conf_merge_value(conf->half_close, prev->half_close, 0); #if (NGX_STREAM_SSL) -- 2.47.3