这篇文章是一个由两部分组成的系列文章的第1部分detailing HTTP/2 best practices. Part two—Implementing HTTP/2 in Production Environments—covers what’s required to implement and debug HTTP/2-capable Web applications in production environments.
超文本传输协议(HTTP)为万维网和网络空间支撑。如果听起来很日期,请考虑最常用的协议版本HTTP 1.1已近20年。什么时候ratifiedbackin 1997, floppy drives and modems were must-have digital accessories and Java was a new, up-and-coming programming language. Ratified in May 2015, HTTP/2 was created to address some significant performance problems with HTTP 1.1 in the modern Web era. Adoption of HTTP/2 has increased in the past year as browsers, Web servers, commercial proxies, and major content delivery networks have committed to or released support.
Unfortunately for people who write code for the Web, transitioning to HTTP/2 isn't always straightforward and a speed boost isn’t automatically guaranteed. The new protocol challenges some common wisdom when building performant Web applications and many existing tools—such as debugging proxies—don’t support it yet. This post is an introduction to HTTP/2 and how it changes Web performance best practices.
Binary frames: The ‘fundamental unit’ of HTTP/2
http 1.1的好处之一(至少超过非安全连接)是,它支持使用telnet session在端口80上的telnet会话中的文本交互:键入GET / HTTP/1.1
returns an HTML document on most Web servers. Because it’s a text protocol, debugging is relatively straightforward.
Instead of text, requests and responses in HTTP/2 are represented by a stream of binary frames, described as a “basic protocol unit” in theHTTP/2 RFC。Each frame has a type that serves a different purpose. The authors of HTTP/2 realized that HTTP 1.1 will exist indefinitely (theGopher protocol毕竟仍然在那里)。HTTP/2请求映射的二进制帧映射到HTTP 1.1请求,以确保向后兼容。
There are some new features in HTTP/2 that don’t map to HTTP 1.1, however. Server push (also known as “cache push”) and stream reset are features that correspond to types of binary frames. Frames can also have a priority that allows clients to give servers hints about the priority of some assets over others.
Other than usingWireshark 2.0, one of the easiest ways to actually see the individual binary frames is by using the net-internals tab of Google Chrome (typechrome://net-internals/#http2
进入地址栏)。对于大型网页,数据可能很难理解。Rebecca Murpheyhelpfully wrote a视觉显示的有用工具in the command line.
Additionally, the protocol used to fetch assets can be displayed in the Chrome Web developer tools—right click on the column header and select “Protocol”:
All of the HTTP/2 requests in this listing use a secure connection over Transport Layer Security (TLS). All major browsers require HTTP/2 connections to be secure. This is done for a practical reason: an extension of TLS called Application-Layer Protocol Negotiation (ALPN) lets servers know the browser supports HTTP/2 (among other protocols) and avoids an additional round-trip. This also helps services that don’t understand HTTP/2, such as proxies—they see only encrypted data over the wire.
Reducing latency with multiplexing
A key performance problem with HTTP 1.1 is latency, or the time it takes to make a request and receive a response. This issue has become more pronounced as the number of images and amount of JavaScript and CSS on a typical Web page continue to增加。Every time an asset is fetched, a new TCP connection is generally needed. This requirement is important for two reasons: the number of simultaneous open TCP connections per host is limited by browsers and there’s a performance penalty incurred when establishing new connections. If a physical Web server is far away from users (for example, a user in Singapore requesting a page hosted at a data center on the U.S. East Coast), latency also increases. This scenario is not uncommon—one recent report表示,超过70%的全球互联网流量通过了北弗吉尼亚州未标记的数据中心。
HTTP 1.1为延迟问题提供了不同的解决方法,包括管道和靠近标头。但是,管道上的实施从未被广泛实施,并且保持距离的标头遭受线路阻挡:当前请求必须在发送下一个请求之前完成。
In HTTP/2, multiple asset requests can reuse a single TCP connection. Unlike HTTP 1.1 requests that use the Keep-Alive header, the requests and response binary frames in HTTP/2 are interleaved and head-of-line blocking does not happen. The cost of establishing a connection (the well-known “three-way handshake”) has to happen only once per host. Multiplexing is especially beneficial for secure connections because of the performance cost involved with multiple TLS negotiations.
Implications for Web performance: goodbye inlining, concatenation, and image sprites?
HTTP/2多路复用对前端Web开发人员具有广泛的影响。它消除了几个长期存在的解决方法,旨在通过捆绑相关资产来减少连接的数量,包括:
- Concatenating JavaScript and CSS files:Combining smaller files into a larger file to reduce the total number of requests.
- Image spriting:Combining multiple small images into one larger image.
- 域碎片:Spreading requests for static assets across several domains to increase the total number of open TCP connections allowed by the browser.
- 内部资产:与HTML文档源捆绑资产,包括基本-64编码图像或直接编写JavaScript代码
标签。
With unbundled assets, there is greater opportunity to aggressively cache smaller pieces of a Web application. It’s easiest to explain this with an example:
一个常见的串联模式是将应用程序中不同页面的样式显示文件捆绑到单个CSS文件中,以减少资产请求的数量。然后,将此大文件用文件名中的MD5哈希(MD5)在文件名中进行指纹打印,以便可以通过浏览器积极缓存。不幸的是,这种方法意味着对网站的视觉布局的更改(例如更改标题的字体样式)需要再次下载整个串联文件。
When smaller asset files are fingerprinted, significant amounts of JavaScript and CSS components that don’t change frequently can be cached by browsers—a small refactor of a single function no longer invalidates a massive amount of JavaScript application code or CSS.
Lastly, deprecating concatenation can reduce frontend build infrastructure complexity. Instead of having several pre-build steps that concatenate assets, they can be included directly in the HTML document as smaller files.
使用HTTP / 2的潜在的负面影响真正的我们rld
Optimizing only for HTTP/2 clients potentially penalizes browsers that don’t yet support it. Older browsers still prefer bundled assets to reduce the number of connections. As of February 2016,caniuse.comreports globalHTTP/2的浏览器支持为71%。就像丢弃Internet Explorer 8.0支持一样,必须使用相关数据每次站点做出采用HTTP/2或使用混合方法的决定。
As described in apost by Kahn Academy Engineeringthat analyzed HTTP/2 traffic on its site, unbundling a large number of assets can actually increase the total number of bytes transferred. WithZlib, compressing a single large file is more efficient than compressing many small files. The effect can be significant on an HTTP/2 site that has unbundled hundreds of assets.
在浏览器中使用HTTP/2也需要通过TLS交付资产。设置TLS证书对于初学者可能很麻烦。幸运的是,开源项目,例如Let’s Encryptare working on making certificate registration more accessible.
A work in progress
Most users don’t care what application protocol your site uses—they just want it to be fast and work as expected. Although HTTP/2 has been officially ratified for almost a year, developers are still learning best practices when building faster websites on top of it. The benefits of switching to HTTP/2 depend largely on the makeup of the particular website and what percentage of its users have modern browsers. Moreover, debugging the new protocol is challenging and easy-to-use developers tools are still under construction.
Despite these challenges, HTTP/2 adoption is growing. According to researchers scanning popular Web properties, thenumber of top sites that use HTTP/2 is increasing, especially afterCloudFlare和WordPressannounced their support in late 2015. When considering a switch, it’s important to carefully measure and monitor asset- and page-load time in a variety of environments. As vendors and Web professionals educate themselves on the implications of this massive change, making decisions from real user data is critical. In the midst of a网站肥胖危机, now is a great time to cut down on the total number of assets regardless of the protocol.
In Part 2 of this series on HTTP/2, we’ll focus on practical implementation details of HTTP/2 and how to enable it on your server and debug real traffic.
Additional resources
- Forgo js packaging? Not so fast.
- HTTP/2 for Web Developers
- HTTP/2 Explained
- Building for HTTP/2
- HTTP/2工具列表
Be sure to read part two of this two-part series:Implementing HTTP/2 in Production Environments
杰夫·马滕斯(Jeff Martens), Product Manager forNew Relic Browser, and Web performance expertAndy Daviescontributed to this post with technical feedback and invaluable suggestions.
背景图courtesy ofShutterstock.com。