The author | a curious alum

Address | http://www.jianshu.com/p/be29d679cbff

Statement | this article is a curious, original, release has been authorized, without author permission please do not reprint

The history of HTTP

HTTP was created primarily to deliver hypertext Markup Language (HTML) documents from a Web server to a client’s browser. For the front end, the HTML page we write will be put on our Web server, the user side through the browser to access the URL address to get the display content of the page, but since Web 2.0, our page has become complicated, not only some simple words and pictures, At the same time, our HTML pages have CSS, Javascript, to enrich the presentation of our pages, and when Ajax comes along, we have a way to get data from the server side, which is actually based on HTTP protocol. Also in the era of mobile Internet, our pages can run in the mobile browser, but compared with PC, the network situation of mobile is more complex, which makes us have to have a deep understanding of HTTP and continue to optimize the process.

2. Basic optimization of HTTP

There are two main factors that affect an HTTP network request: bandwidth and latency.

  • Bandwidth: If we were still in the dial-up phase, bandwidth would be a serious problem, but now that the network infrastructure has improved bandwidth so much that we don’t have to worry about bandwidth affecting speed, all that’s left is latency.

  • Delay:

    • HOL blocking: Browsers block requests for a number of reasons. A browser can have only four connections for the same domain name at a time (this may vary depending on the browser kernel). If the number of connections exceeds the threshold, subsequent requests will be blocked.

    • DNS Lookup: The browser needs to know the IP address of the target server to establish a connection. The system that resolves domain names into IP addresses is called DNS. This can often be done with DNS caching results to reduce this time.

    • Initial Connection: HTTP is based on TCP. The browser can set up an actual connection only after the third handshake. However, these connections cannot be reused, resulting in three handshakes and slow startup for each request. The effect of three-way handshake is more obvious in the case of high latency, and the effect of slow startup is more significant in the case of large file requests.

Some differences between HTTP1.0 and HTTP1.1

HTTP1.0 was first used in 1996 for simple web pages and web requests, while HTTP1.1 was widely used in 1999 for web requests from major browsers, and is currently the most widely used HTTP protocol. The main differences are as follows:

  1. HTTP1.0 mainly uses if-modified-since,Expires in the header. HTTP1.1 introduces more cache control policies such as Entity tag. If-unmodified-since, if-match, if-none-match, etc.

  2. Bandwidth optimization and network connection, HTTP1.0, there are some waste of bandwidth, such as the client only needs a part of an object, and the server will send the whole object over, and does not support breakpoint continuation function, HTTP1.1 is introduced in the request header range header field, which allows only a part of the resource request, The return code is 206 (Partial Content), which makes it easy for developers to make the most of bandwidth and connections.

  3. Error notification management, HTTP1.1 added 24 error status response code, such as 409 (Conflict) indicates that the requested resource and the current state of the resource Conflict; 410 (Gone) Indicates that a resource on the server is permanently deleted.

  4. The Host header processing, in HTTP1.0, assumes that each server is bound to a unique IP address, so the URL in the request message does not pass the hostname. However, with the development of virtual hosting technology, there can be multiple virtual hosts (multi-homed Web Servers) on a physical server, and they share the same IP address. HTTP1.1 both Request and response messages should support the Host header field, and an error (400 Bad Request) will be reported if there is no Host header field in the Request message.

  5. HTTP 1.1 supports long Connections and Pipelining processing that delivers multiple HTTP requests and responses over a SINGLE TCP connection, reducing the cost and latency of establishing and closing connections. Connection: keep-alive is enabled by default in HTTP1.1, somewhat compensating for the fact that HTTP1.0 creates a Connection on every request.

Some differences between HTTPS and HTTP

  • For HTTPS, you need to apply for a certificate from a CA. Generally, a free certificate is rare and requires a fee.

  • HTTP runs on TOP of TCP, and all transmitted content is in plain text. HTTPS runs on top of SSL/TLS, and SSL/TLS runs on top of TCP, and all transmitted content is encrypted.

  • HTTP and HTTPS use completely different connections and use different ports, the former 80 and the latter 443.

  • HTTPS can effectively prevent carrier hijacking, solving a big problem of anti-hijacking.

SPDY: http1.x optimization

In 2012, SPDY was proposed to optimize http1. X request latency and solve the security of http1. X.

  1. Reduce latency. For HTTP’s high latency problem, SPDY elegantly takes multiplexing. Multiplexing can share a TCP connection through multiple request streams, which solves the problem of HOL blocking, reduces latency and improves bandwidth utilization.

  2. Request priorities. A new problem with multiplexing is that critical requests can be blocked on a shared connection basis. SPDY allows you to prioritize each request so that important requests get a response first. For example, when the browser loads the home page, the HTML content of the home page should be displayed first, and then all kinds of static resource files and script files are loaded, so as to ensure that users can see the content of the web page in the first time.

  3. The header compression. The http1.x header mentioned earlier is often redundant. Choosing the right compression algorithm can reduce the size and number of packets.

  4. HTTPS based encryption protocol transmission, greatly improves the reliability of transmitted data.

  5. For example, my web page has a sytl. CSS request. When the client receives sytl. CSS data, the server will push the sytl. js file to the client. When the client tries to retrieve sytle.js again, it can be retrieved directly from the cache without having to request it again. SPDY composition diagram:

SPDY sits below HTTP, on top of TCP and SSL, making it easy to accommodate older versions of HTTP (encapsulating http1.x content into a new frame format) while using existing SSL functionality.

Six, HTTP2.0 performance is amazing

HTTP/2: The Future of the Internet, https://link.zhihu.com/?target=https://http2.akamai.com/demo is the company set up an official demonstration, To illustrate the performance improvement of HTTP/2 over previous HTTP/1.1. 379 images were requested at the same time, and the comparison of Load time shows the speed advantage of HTTP/2.

HTTP2.0: SPDY upgrade

HTTP2.0 is an updated version of SPDY. However, there are some differences between HTTP2.0 and SPDY.

Differences between HTTP2.0 and SPDY:

  1. HTTP2.0 supports plaintext HTTP transport, while SPDY enforces the use of HTTPS

  2. HTTP2.0 header compression algorithm using HPACK http://http2.github.io/http2-spec/compression.html, Rather than SPDY using DEFLATE http://zh.wikipedia.org/wiki/DEFLATE

New features of HTTP2.0 compared to Http1.x

  • The new Binary Format, http1.x parsing is text-based. There are natural defects in format parsing based on text protocol. There are various forms of text expression, and many scenarios must be considered in order to achieve robustness. Binary is different, only recognizing the combination of 0 and 1. Based on this consideration HTTP2.0 protocol parsing decision to adopt binary format, implementation is convenient and robust.

  • MultiPlexing means that each request is used as a connection sharing mechanism. A request corresponds to an ID. In this way, a connection can have multiple requests. The requests of each connection can be randomly mixed together, and the receiver can assign the requests to different server requests according to the REQUEST ID.

  • HTTP2.0 uses encoder to reduce the size of the headers that need to be transferred. The communication parties cache a table of header fields. This avoids duplicate header transmission and reduces the size of the required transmission.

  • Server push. Like SPDY, HTTP2.0 has server push functionality.

HTTP2.0 upgrade

  • HTTP2.0 can support non-https, but now mainstream browsers like chrome, firefox, or only support TLS deployment protocol HTTP2.0, so if you want to upgrade to HTTP2.0 or HTTPS first.

  • If you are using NGINX, simply enable the corresponding protocol in your configuration file. See the NGINX whitepicker. NGINX configuration HTTP2.0 official guide to https://www.nginx.com/blog/nginx-1-9-5/.

  • HTTP2.0 is fully compatible with the semantics of http1.x. For browsers that do not support HTTP2.0, NGINX will automatically be backward compatible.

Ten, note

What is the difference between multiplexing in HTTP2.0 and long connection multiplexing in HTTP1.x?

  • HTTP/1.* Once request-response, establish a connection, close when used up; Each request establishes a connection;

  • HTTP/1.1 Pipeling (Pipeling) is a multi-threaded process that queues up multiple requests to perform any of the following operations at any time.

  • HTTP/2 Multiple requests can be executed simultaneously in parallel on a single connection. A request task is time-consuming and does not affect the normal execution of other connections. Specific figure:

What exactly is server push? Server push can send the resources required by the client to the client along with index. HTML, saving the step of repeated requests by the client. Because there are no requests, connections, etc., static resources can be pushed by the server to greatly improve the speed. Details are as follows:

  • Normal client request process:

  • The process of server push:

Why do I need head compression? Given that a page has 100 resources to load (a conservative amount for today’s Web) and each request has 1KB headers (again not uncommon because of cookies, references, etc.), it takes at least 100KB more to get those headers. HTTP2.0 can maintain a dictionary that differentiates HTTP headers, greatly reducing traffic generated by header transfers. Specific reference: HTTP/2 header compression technology introduction

How good is HTTP2.0 multiplexing? The key to HTTP performance optimization is not high bandwidth, but low latency. TCP connections “tune” themselves over time, limiting the maximum speed of the connection at first and increasing the speed of the transfer over time if the data is successfully transferred. This tuning is called TCP slow start. For this reason, HTTP connections that are inherently abrupt and short become very inefficient. HTTP/2 enables more efficient use of TCP connections by having all data flows share the same connection, allowing high bandwidth to truly serve HTTP’s performance gains.

Xi. Reference

What are the major improvements in HTTP/2.0 over 1.0? HTTP/2 header compression: HTTP/2 header compression

day

more

fine

The color

Please fasten your seat belt

The public,

“Code Student”