30.2 Keeping Connections Open to Clients

Problem

You need to increase the number of requests allowed to be made

over a single connection from clients and the amount of time idle

connections are allowed to persist.

问题

增加单个连接的请求数,同时增加空闲连接(idle connections)的的连接时长。


Solution

Use the keepalive_requests and keepalive_timeout directives to

alter the number of requests that can be made over a single connec‐

tion and the time idle connections can stay open:

http {
    keepalive_requests 320;
    keepalive_timeout 300s;
    ...
}

The keepalive_requests directive defaults to 100, and the

keepalive_timeout directive defaults to 75 seconds.

解决方案

keepalive_requests 和 keepalive_timeout 指令允许变更单个连接的最大

请求数和空闲连接的连接时长:

http {
    keepalive_requests 320;
    keepalive_timeout 300s;
    ...
}

keepalive_requests 默认为 100,keepalive_timeout 的默认值为 75 秒。


Discussion

Typically the default number of requests over a single connection

will fulfill client needs because browsers these days are allowed to

open multiple connections to a single server per fully qualified

domain name. The number of parallel open connections to a

domain is still limited typically to a number less than 10, so in this

regard, many requests over a single connection will happen. A trick

commonly employed by content delivery networks is to create mul‐

tiple domain names pointed to the content server and alternate

which domain name is used within the code to enable the browser

to open more connections. You might find these connection opti‐

mizations helpful if your frontend application continually polls your

backend application for updates, as an open connection that allows a

larger number of requests and stays open longer will limit the num‐

ber of connections that need to be made.

结论

一般情况下,keepalive_requests 和 keepalive_timeout 的默认配置,能够满足

客户端的请求,因为,现代浏览器能为不同域名打开多个连接。但对于同一个域名

仅能同时发起 10 以内的请求,这将带来性能瓶颈。CDN 的实现原理是启用多个域

名指向内容服务器,并以编码的方式指定使用的域名,以使浏览器能够打开更多的

连接。你会发现使用更多的请求连接数和连接时长配置,在客户端需要频繁更新数

据能提升服务器性能。

results matching ""

    No results matching ""