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

...

}

} ```

Copyright © weiliang-ms 2021 all right reserved,powered by Gitbook本书发布时间: 2024-05-30 16:49:59

results matching ""

    No results matching ""