虚拟化容器,大数据,DBA,中间件,监控。

Nginx keepalived 长连接

21 12月
作者:admin|分类:应用管理

An Introduction to HTTP and Keepalive Connections


HTTP keepalive connections are a necessary performance feature that reduce latency and allow web pages to load faster.

HTTP保持连接是一项必要的性能特点,可减少延迟并允许网页加载更快。 

HTTP is a simple, text‑based protocol. If you’ve not done so before, take a look at the output from an HTTP debugging tool such as the one in your web browser, and check out the standard request and response structure:

HTTP是一个简单的基于文本的协议。如果您以前从未这样做过,请查看HTTP调试工具(例如Web浏览器中的工具)的输出,并检查标准的请求和响应结构:

 

In its simplest implementation, an HTTP client creates a new TCP connection to the destination server, writes the request, and receives the response. The server then closes the TCP connection to release resources.

在最简单的实现中,HTTP客户端与目标服务器建立一个新TCP连接,写入请求并接收响应。然后服务器关闭TCP连接释放资源。

 

This mode of operation can be very inefficient, particularly for complex web pages with a large number of elements or when network links are slow. Creating a new TCP connection requires a ‘three‑way handshake’, and tearing it down also involves a two‑way shutdown procedure. Repeatedly creating and closing TCP connections, one for each message, is akin to hanging up and redialing after each person speaks in a phone conversation.

这种操作模式可能效率很低,特别是对于具有大量信息的复杂网页或网络链接速度较慢的情况。创建新的TCP连接需要进行“三向握手”,而将其断开也需要进行双向关闭过程。重复创建和关闭TCP连接(每个消息一个),类似于在每个人在电话交谈中讲话后挂断并重拨。

HTTP uses a mechanism called keepalive connections to hold open the TCP connection between the client and the server after an HTTP transaction has completed. If the client needs to conduct another HTTP transaction, it can use the idle keepalive connection rather than creating a new TCP connection.

HTTP使用一种称为keepalive连接机制来在HTTP事务完成后保持打开客户端与服务器之间的TCP连接。如果客户端需要执行另一个HTTP事务,则可以使用空闲的keepalive连接,而不是创建新的TCP连接。

Clients generally open a number of simultaneous TCP connections to a server and conduct keepalive transactions across them all. These connections are held open until either the client or the server decides they are no longer needed, generally as a result of an idle timeout.

客户端通常会打开与服务器的多个同时TCP连接,并在所有客户端之间进行keepalive事务。这些连接一直保持打开状态,直到客户端或服务器决定不再需要它们(通常是由于空闲超时)。

Modern web browsers typically open 6 to 8 keepalive connections and hold them open for several minutes before timing them out. Web servers may be configured to time these connections out and close them sooner.

浏览器通常会打开6到8个keepalive连接,并在超时之前将其保持几分钟的打开状态。可以配置Web服务器使这些连接超时并尽快关闭它们。

 

Nginx长连接keepalived


keepalive可以有效的提升我们连接的使用效率,这里的keepalive是指Http协议里面的keepalive而不是TCP协议里面的keepalive。keepalive分为对下游客户端keepalive行为也分为对上游tomcat行为。这里指的是keepalive对客户端的行为。

多个HTTP请求复用同一个TCP连接,比如浏览器只需要建立一个TCP连接就可以运行多个HTTP请求。复用一个TCP连接减少了建立连接断开连接的次数,减少了握手次数。

 

keepalive语法


Syntax: keepalive_disable none | browser ...;#某些浏览器对keepalive支持不好,默认是IE6就不使用keepalive
Default: keepalive_disable msie6;
Context: http, server, location

Syntax: keepalive_requests number; #在一个TCP连接当上最多执行多少个HTTP请求,默认是100
Default: keepalive_requests 100;
Context: http, server, location

Syntax: keepalive_timeout timeout [header_timeout]; #timeout:用户一个HTTP请求完成以后最多经过timeout时间,如果还是没有新的请求进来就会关连接,默认75s。
Default: keepalive_timeout 75s;
Context: http, server, location

Nginx 处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提升 Nginx 的性能。

HTTP 是一种无状态协议,客户端向服务器发送一个 TCP 请求,服务端响应完毕后断开连接。如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。

HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

Nginx 使用 keepalive_timeout 来指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。

浏览574 评论0
返回
目录
返回
首页
Haproxy 超详细总结 四层负载均衡VS七层负载均衡 LVS VS Haproxy VS Nginx CentOS 7下Docker桥接网络,实现独立IP与独立端口效果