Nginx upstream keepalive上连接

Nginx 对上游服务器使用keepalive长连接。

功能:通过服用连接,降低nginx与上游服务器建立,关闭连接的消耗,提升吞吐量的同时降低时延。

默认已经编译进nginx , 通过–without-http_upstream_keepalive_module 移除。

对上游http头部设定:

#为了防止客户端请求的是HTTP1.0协议,指定上游连接时http1.1
proxy_http_version 1.1;
#修改客户端header头中connection值为close
proxy_set_header Connection "";

upstream_keepalive指令:

#最多保持多少个空闲连接
Syntax: keepalive connections;
Default: ---
Context: upstream

#一条tcp连接上最多支持多少个请求
Syntax: keepalive_requests number;
Default: keepalive_requests 100;
Context: upstream

#一条tcp连接超时时间
Syntax: keepalive_timeout timeout;
Default: keepalive_timeout 60s;
Context: upstream

指定上游服务域名解析的resolver指令

Syntax: resolver address ... [valid=time] [ipv6=on|off]
Default: ---
Context: http,server,location

Syntax: resolver_timeout time;
Default: resolver_timeout 30s;
Context: http,server,location

配置示例

upstream up_servers {
server 127.0.0.1:8011 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
server 127.0.0.1:8012;
keepalive 100;
}

server {
server_name localhost;
error_log logs/errors.log info;

location / {
proxy_pass http://up_servers;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}

添加评论

电子邮件地址不会被公开。 必填项已用*标注