30.3 Keeping Connections Open Upstream
Problem
You need to keep connections open to upstream servers for reuse to
enhance your performance.
问题
需要增加代理服务器与被代理服务器的连接数,提升服务器性能。
Solution
Use the keepalive directive in the upstream context to keep con‐
nections open to upstream servers for reuse:
proxy_http_version 1.1;
proxy_set_header Connection "";
upstream backend {
server 10.0.0.42;
server 10.0.2.56;
keepalive 32;
}
The keepalive directive in the upstream context activates a cache of
connections that stay open for each NGINX worker. The directive
denotes the maximum number of idle connections to keep open per
worker. The proxy modules directives used above the upstream
block are necessary for the keepalive directive to function properly
for upstream server connections. The proxy_http_version direc‐
tive instructs the proxy module to use HTTP version 1.1, which
allows for multiple requests to be made over a single connection
while it’s open. The proxy_set_header directive instructs the proxy
module to strip the default header of close, allowing the connection
to stay open.
解决方案
在 upstream 会计指令中使用 keepalive 指令保持代理服务与被代理服务器连
接以复用:
proxy_http_version 1.1;
proxy_set_header Connection "";
upstream backend {
server 10.0.0.42;
server 10.0.2.56;
keepalive 32;
}
keepalive 指令会为每个 NGINX worker 进程创建一个连接缓存,表示每个
worker 进程能保持打开的空闲连接的最大连接数量。如果要使 keepalive
指令正常工作,在 upstream 指令上使用的 proxy 模块指令则是必须的。
proxy_http_version 指令表示启用的 http 1.1 版本,它允许在单个连接
上发送多个请求;proxy_set_header 指令删除 connection 消息头的默认值
close,这样就允许保持连接的打开状态。
Discussion
You would want to keep connections open to upstream servers to
save the amount of time it takes to initiate the connection, and the
worker process can instead move directly to making a request over
an idle connection. It’s important to note that the number of open
connections can exceed the number of connections specified in the
keepalive directive as open connections and idle connections are
not the same. The number of keepalive connections should be kept
small enough to allow for other incoming connections to your
upstream server. This small NGINX tuning trick can save some
cycles and enhance your performance.
结论
当需要保持代理服务器与被代理服务器的连接打开状态,以节省启动连接
所需的时间;同时,需要将 worker 进程收到的请求分发值空闲的连接直接
处理。有一点需要注意,开启的连接数可以多于 keepalive 配置的连接数,
因为开启的连接数和空闲连接数不是同一个东西。keepalive 配置的连接数
应尽量少,以确保新的请求能够被分发到被代理服务器。这条配置技巧能够
通过减少请求连接的生命周期的手段,提升服务器性能。