Understanding the Keep-alive of HTTP
In a previous article about TCP's keepalive, this article speaks of the HTTP plane keep-alive. Two kinds of keepalive in the spelling above is not the same, just the same pronunciation, so everyone is confused. The HTTP level of the keep-alive is our contact more, but also we usually verbal "keepalive". Now let's talk about HTTP keep-alive.
Short Connections & long connections & parallel connections
And before Keep-alive, let's talk about HTTP short connections & long connections.
Short connection
A short connection is a connection that is made every time a resource is requested, and the connection closes immediately after the request is completed. Each request goes through the process of "create a TCP connection, request resource, and release a connection with a response resource,"
Long connections
The so-called Long connection (persistent connection), is to establish only one connection, multiple resource requests are re-use the connection, after completion of the shutdown. To request 10 images on a page, you only need to establish a TCP connection, then request 10 graphs in turn, wait for the resource to respond, and release the connection.
Parallel connections
The so-called parallel connection (multiple connections) is actually a concurrent short connection.
Keep-alive
The simplest evolution of a specific client and server from short connections to long connections requires the following improvements:
- The client sends an HTTP request header that requires an additional connection:keep-alive field
- The web-server side should be able to identify the Connection:keep-alive field and specify the Connection:keep-alive field in the HTTP response, telling the client that I can provide the keep-alive service, and "Grant" Client I will not close the socket connection for the time being
In http/1.0, in order for a client to Web-server to support a long connection, it must display the specified in the HTTP request header
Connection:keep-alive
In http/1.1, the default is to turn on the keep-alive, to turn off keep-alive need to display the specified in the HTTP request
Connection:close
Most browsers now use http/1.1 by default, so Keep-alive is turned on by default. Once the client and server have reached an agreement, the long connection is established.
The client then sends an HTTP request to the server, continuing with the example above: request 10 images. If each "request-and-response" is independent, it is okay that the contents of the 10 images are independent. However, if the pipeline mode, the previous request is not responding, the next request is issued, so that the concurrent issue of 10 requests, for 10 response client how to distinguish? There is no way to differentiate the HTTP protocol, so in this case the server-side response must be ordered, by Conten-length to differentiate each request, this is only for static resources, that the dynamic resources can not predict the page size of the case? I haven't looked into it yet and can view Https://www.byvoid.com/blog/http-keep-alive-header
Note also: Specifying keep-alive is a convention that the client and server side need to meet as much as possible, and the client and server can shut down keep-alive at any time and should not be affected by each other.
Nginx keepa-alive Configuration
Specific to the Nginx HTTP layer of the KeepAlive configuration has
Syntax: keepalive_timeout timeout [header_timeout]; Default: keepalive_timeout 75s; Context: http, server, location
The first parameter sets a timeout during which a keep-alive client connection would stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the "Keep-alive:timeout=time" Response header field. Parameters may differ.
Syntax: keepalive_requests number; Default: keepalive_requests 100; Context: http, server, location
Sets the maximum number of requests that can be served through one keep-alive connection. After the maximum number of requests be made, the connection is closed.
Can see Nginx about Keepalive_timeout is implemented
./src/http/ngx_http_request.cstatic voidngx_http_finalize_connection (ngx_http_request_t *r) {... if (!ngx_terminate &&!ngx_exiting && r->keepalive && clcf->keepalive_timeout > 0) { Ngx_http_set_keepalive (R); Return }...} Static voidngx_http_set_keepalive (ngx_http_request_t *r) {//if found to be a pipeline request, the condition is that there are N and n+1 requests in the buffer in the presence of the IF (B->pos < ; B->last) {/* The pipelined requests */}//This request has ended, start releasing the Request object resource r->keepalive = 0; Ngx_http_free_request (r, 0); C->data = HC; If you try to read keep-alive the socket return value is incorrect, the client may be close. Then close the socket if (ngx_handle_read_event (rev, 0)! = NGX_OK) {ngx_http_close_connection (c); Return }//Start formal processing pipeline ... rev->handler = Ngx_http_keepalive_handler; ...//Set a timer, the trigger time is Keepalive_timeout settings Ngx_add_timer (rev, Clcf->keepalive_timeout); ...} Static Voidngx_http_keepalive_handler (ngx_event_t *rev) {//DiscoveryTimeout closes socket if (Rev->timedout | | c->close) {ngx_http_close_connection (c); Return }//Read keep-alive settings from socket n = c->recv (c, b->last, size); if (n = = Ngx_again) {if (ngx_handle_read_event (rev, 0)! = NGX_OK) {ngx_http_close_connection (c); Return } ... } There's still doubt here? Ngx_reusable_connection (c, 0); C->data = Ngx_http_create_request (c); Delete Timer Ngx_del_timer (rev); Restart processing request Rev->handler = Ngx_http_process_request_line; Ngx_http_process_request_line (rev);}
Understanding the Keep-alive of HTTP