upstream开启keepalive
解释说明
配置keepalive
的主要意图:解决在高延迟网络上建立TCP
连接的延迟问题。
当nginx
与上游服务器之间需要持续保持一定数量的连接时,keepalive
很有用。
开启Keep-Alive
连接对性能有很大的影响:减少了打开和关闭连接所需的CPU
和网络开销。
通过在nginx
中启用HTTP keepalive
,降低了nginx
连接上游服务器的延迟,从而提高了性能,并减少了nginx
耗尽临时端口的可能性。
nginx
将重用现有的TCP
连接,而不创建新的TCP
,
这可以极大地减少繁忙服务器上TIME_WAIT TCP
连接中的套接字数量(减少操作系统建立新连接的工作,减少网络上的数据包)
注意: 仅在HTTP/1.1
时支持Keep-Alive
连接。
配置样例
```nginx configuration
Upstream context:
upstream backend {
Sets the maximum number of idle keepalive connections to upstream servers
that are preserved in the cache of each worker process.
keepalive 16;
}
Server/location contexts:
server {
...
location / {
# By default only talks HTTP/1 to the upstream,
# keepalive is only enabled in HTTP/1.1:
proxy_http_version 1.1;
# Remove the Connection header if the client sends it,
# it could be "close" to close a keepalive connection:
proxy_set_header Connection "";
...
}
} ```