This commit is contained in:
Patrik 2026-07-09 08:16:14 +00:00 committed by GitHub
commit 968dd2e576
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 209 additions and 3 deletions

View file

@ -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;

View file

@ -73,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;
};

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
prev->upstream.socket_sndbuf, 0);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_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);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_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);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
prev->upstream.socket_sndbuf, 0);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
prev->upstream.socket_sndbuf, 0);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
prev->upstream.socket_sndbuf, 0);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

View file

@ -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_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_conf_set_size_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_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;
@ -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_size_value(conf->upstream.socket_rcvbuf,
prev->upstream.socket_rcvbuf, 0);
ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
prev->upstream.socket_sndbuf, 0);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

View file

@ -680,6 +680,14 @@ ngx_http_upstream_init_request(ngx_http_request_t *r)
u->peer.so_keepalive = 1;
}
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);
u->output.alignment = clcf->directio_alignment;

View file

@ -257,8 +257,10 @@ typedef struct {
#endif
ngx_str_t module;
size_t socket_rcvbuf;
size_t socket_sndbuf;
NGX_COMPAT_BEGIN(5)
NGX_COMPAT_BEGIN(3)
NGX_COMPAT_END
} ngx_http_upstream_conf_t;

View file

@ -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)