HTTP/2 protocol has been officially published in 2015, but there are still many websites using HTTP/ 1.1 protocol, 9102 years ago, confirm not to upgrade? HTTP /1.1 works fine. What’s the advantage of HTTP /2?

First, let’s take a look at the disadvantages of HTTP /1.1 and the advantages of HTTP /2.

HTTP / 1.1

  1. The HTTP/1.1 protocol allows you to send multiple requests over the same TCP connection, but these requests are in order. The first request must be processed before the next request is processed. If the first request is particularly slow, all subsequent requests need to be queued.

  2. TCP Connection Limit A browser can create a maximum of six to eight TCP connections for a domain name. If ten requests are sent to a page at the same time, the next two to four requests can only be sent after the first six to eight requests are returned. How does that work? Domain name sharding comes into being. This eliminates the limitation by allocating resources to different domains (which can be secondary subdomains), but it also doesn’t work by abusing domain sharding because each TCP connection is time-consuming.

  3. The Header content is extensive, sometimes exceeding the response content, and many fields are repeated each time.

  4. HTTP/1.1 is a text protocol transport and is not secure enough.

Many optimizations have been made based on these shortcomings: Sprite graphics, merging scripts and stylesheets, resource inlining, domain sharding, etc., but these additional operations could have been avoided if the HTTP protocol was good enough.

HTTP/2

New features of HTTP2 over HTTP /1.1 include:

  1. MultiPlexing, single long connection, binary format transmission, request priority setting
  2. Header compression
  3. The Server pushes Server Push

Let’s introduce them one by one.

MultiPlexing

HTTP/2 multiplexes TCP connections. In a connection, both the client and the server can send multiple requests or responses at the same time. These requests or responses are logically divided into streams. And there is no one-to-one correspondence in order (but the same request or response frame must be ordered, different can be disorderly), so as to avoid “queue congestion”, reduce the number of TCP connections and TCP connection slow start problems. Http2 can also assign priority to the stream, with higher priority being the first to respond. For example, you can set js and CSS to a higher priority and let them download and execute first. The priority can also be changed dynamically.

Multiplexing diagram:

Header compression

HTTP is stateless, and some information needs to be attached to each request. But many fields are duplicated, which can waste bandwidth and affect speed. HTTP/2 uses the HPACK compression algorithm for header information to reduce the size of the header. In this way, the common names and values in the header information correspond to an index, and a static dictionary is maintained with the index ranging from 1 to 61. For example, : method:GET is mapped to 2, so that the header can be compressed. However, for some dynamic resources, such as user-Agent, it is necessary to maintain a common dynamic dictionary that can be added dynamically. This dynamic dictionary is gradually established during data transmission, and index starts from 62. Then the mapped data is encoded by Huffman.

Static dictionary table:

Server push

In the past, the server sent whatever the client asked for, which was stingy. Now with server push, the client asks the server for a drop of water, and the server can go back to the whole forest. This allows the server to provide the browser with the resources it needs to render the page without the browser having to make another round of requests after receiving and parsing the page, saving load time. For example, the browser requests a page from the server. After the browser receives the page and parses the HTML, it finds that the page references static resources. The browser then sends the request to the server for static resources. But now the server can simply return the page along with the static resources it needs.

The server push needs to be manually configured by the developer. Multiplexing and header compression described earlier can be implemented by the browser and server, and developers do not need to care about them.

How to configure nginx

Nginx starts with HPPT/2 as simple as adding HTTP2 to HTTPS Settings.

server {
    listen 443 ssl http2;
}
Copy the code

With all the advantages of HTTP/2, are you sure you don’t want to upgrade? After the upgrade, you can optimize say Goodbye for Sprite images, merge scripts and stylesheets, resource inlining, and domain sharding. After who ask you website optimization has what methods, the above several do not say again.