HTTP/1 的最佳实践并不适合 HTTP/2(The best practices of HTTP1 are harmful in a HTTP2 world. )

By | 2018年7月10日

HTTP1.x is the version of HTTP we are all familiar with. It’s an old
protocol that was designed before we knew what the world wide web would
become. While it does the job, it’s just not very efficient at it
anymore because what we demand of it is quite a lot more complex than
what it was designed for.

To get websites to load in an acceptable time using HTTP1 we have
developed a series of techniques; hacks really; to eke performance out
of this old protocol. They are:

  • Spriting: taking multiple images, combining them into one image,
    and using CSS to only show part of that image in a particular place.

  • Concatenating: Taking multiple CSS or JS files and sticking them into one large file.

  • Serving assets from a cookie-less domain.

  • Sharding: creating different domains or sub-domains to host assets like images.

HTTP 1.x 是我们都熟悉的HTTP版本,它是一个老的协议,在我们还不知道万维网将会是什么样子之前,它就已经被设计出来了。当它做这项工作的时候,它显得并不很有效率,因为我们需求的比它当初设计的要复杂的多的多。


  • Spriting(雪碧图):将多张图片组合成一张图片,使用CSS来在特定的位置显示图的一部分。

  • Concatenating:将多个CSS或JS文件组合成一个大文件。

  • 从一个cookie-less域名提供素材。

  • Sharding:创建不同的域名或子域名来提供素材,如图片。

The first two techniques are aimed at avoiding multiple HTTP requests.
In HTTP1 a request is a very costly thing and takes a lot of time, each
request may be loaded down with cookies that must be sent as part of the
request, and none of it is compressed. It’s faster to lump a bunch of
things together and get it all done in one go than to keep asking for
different resources.

The third technique is used to minimise the time required to get
assets; cookies, if set, must be sent to the requested domain along with
every request – that adds up to a lot of ‘wasted’ space on the line. If
your assets are on a different domain that doesn’t use cookies, then
requests for those files won’t need to send cookies with them, and it’s
all a bit faster.

前两项技术的目地是避免大量的HTTP 请求。在HTTP1中,HTTP请求是个很高昂的事情,它消耗大量的时间,每个请求可能都会被cookie这些必须作为请求的一部分的东西装满,它们都是未经压缩的。把一堆的东西连成一块并把它做完要比保持请求不同的资源还要快。


The last technique, sharding, is because browsers used to only allow
two simultaneous HTTP requests per domain. If you create a new domain
for some of your assets, then you double the amount of simultaneous
connections the browser will allow in order to get your files. Thus, you
can pull the website content down faster. In reality, sharding hasn’t
been too useful in the last couple of years because browser vendors
decided the ‘two connections’ restriction was daft, and they ignored it.


Do not use those HTTP1 based best practices with a website being served over HTTP2.

HTTP2 is almost here, it’s based on SPDY, and it makes everything much
more efficient. It also means that all of those HTTP1 performance
techniques are harmful. They will make a HTTP2 website slower, not
faster – don’t use them.

HTTP2 makes the cost of multiple requests far less because of a number of techniques it does itself.

  • It can leave the connection open for re-use for very extended
    periods of time, so there’s no need for that costly handshake that HTTP1
    requires for every request.

  • HTTP2 also uses compression, unlike HTTP1, and so the size of the request is significantly smaller – and thus faster.

  • HTTP2 multiplexes; it can send and receive multiple things at the same time over one connection.

最后一项技术sharding,是因为浏览器过去每个域名只允许同时建立两个请求. 如果你给你的一些素材创建一个新的域名, 你将会获得浏览器允许的双倍的同时连接来获得你的文件.因此, 你可以更快的下载网站内容. 实际上, sharding在最近的几年并不很有效因为浏览器供应商认为“两个连接”的限制是愚蠢的, 他们忽略了它.



HTTP2就要到来了, 基于SPDY,它让所有事情都变得更有效率.这同时意味着所有的HTTP1性能技术是不合适的.它们会使HTTP2网站变慢,而不是更快- 所以不要使用它们.


  • 它可以在较长的时间内让连接保持开启以便重新使用, 所以不需要像HTTP1那样每个请求都花费时间握手.

  • HTTP2同时使用压缩, 不像HTTP1, 所以请求的大小显著减小 – 因此更快.

  • HTTP2多路传输; 它可以在一个连接中同时发送和接收多个事物.

All that means not only are the old HTTP1 techniques not needed,
they’ll actually make things slower. You may be loading assets that are
not required for the page being viewed (concatenation and spriting are
likely to do this), and sharding invokes DNS lookups which slow things
down, despite HTTP2 meaning you don’t need to shard in the first place.

The long and short of it is; when you build a front-end to a website,
and you know it’s going to be served over HTTP2 – you need to ensure
you’re not using legacy HTTP1 performance techniques that are going to
harm the site under HTTP2.

Learning more

There’s an excellent write-up by Daniel Stenberg going into much more detail available as a PDF.




电子邮件地址不会被公开。 必填项已用*标注