功能:通过复用连接,降低Nginx与上游服务器建立,关闭连接的消耗,提升吞吐量的同时降低时间延迟.
模块:ngx_http_upstream_keepalive_module是默认编入到Nginx中的,如果不想编进去可以用
--without-http_upstream_keepalive_module对这个模块进行移除.
(/home/muten/module/nginx-1.13.7中执行./configure --help |more 搜索http_upstream_keepalive_module可验证)
关于upstream_keepalive的指令:
(1)keepalive
Activates the cache for connections to upstream servers.
The connections parameter sets the maximum number of idle keepalive connections to
upstream servers that are preserved in the cache of each worker process. When this
number is exceeded, the least recently used connections are closed.
表示上游服务器中最多保持多少个用于keepalive请求的空闲的连接.
这个命令会促使上游服务器中每个worker进程中开辟出来一块缓存用于keepalive连接,
当超过这个数字的时候,将会利用LRU算法将一些连接关闭.
因为http1.0协议是不支持长连接的,为了防止用户发来的是http1.0,我们需要重置http版本,将其置成1.1,这样就可以一直使用keep_alive长连接;
为了防止用户的Connection头部给我们传递的是Close而不是Keepalive,我们主动设置向上游发送的connection.
When using load balancing methods other than the default round-robin method, it is necessary to activate them
before the keepalive directive.
当使用的负载均衡方法不是默认的轮询算法时,需要在keepalive指令之前激活他们
语法:
Syntax: keepalive connections;
Default: —
Context: upstream
This directive appeared in version 1.1.4.
Syntax: proxy_http_version 1.0 | 1.1;
Default:
proxy_http_version 1.0;
Context: http, server, location
This directive appeared in version 1.1.4.
Syntax: proxy_set_header field value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location
对上游连接的http头部设定:
proxy_http_version 1.1;
proxy_set_header Connection "";
配置
upstream http_backend {
server 127.0.0.1:8080;
keepalive 16;
keepalive_timeout 60s;
}
server {
...
location /http/ {
proxy_pass
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}