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

Web应用开发 William 298浏览 0评论

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

为了让使用HTTP1的网站在可接受的时间内加载完成,我们开发了一系列的技术;事实上是技巧;来竭力提升这个老协议的性能,它们是:

  • 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这些必须作为请求的一部分的东西装满,它们都是未经压缩的。把一堆的东西连成一块并把它做完要比保持请求不同的资源还要快。

第三项技术用于最小化获取素材需要的时间,如果设置了cookie,就必须将它随每个请求发送到请求的域名,这在连接中添加了大量的”浪费的“空间。如果你的素材在不同的域名且不使用cookie,对这些文件的请求将不会随之发送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.

HTTP2

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

不要使用这些HTTP1基础上的最佳实践在一个通过HTTP2提供服务的网站上.

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

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.

所有这些意味着不仅旧的HTTP1技术不再需要,而且它们实际上会让事情变得更慢。你可能要加载被浏览网页不需要的素材(concatenation和spriting似乎就是做这个的),sharding调用DNS查找也会让事情慢下来,尽管HTTP2意味着你从一开始就不需要shard。

总而言之,当你在给一个网站开发前端的时候,而且你知道这个网站将要通过HTTP2服务,你需要确保没有使用旧有的HTTP1性能技术,它们对HTTP2下的网站并不合适。

转载请注明:AspxHtml学习分享网 » HTTP/1 的最佳实践并不适合 HTTP/2(The best practices of HTTP1 are harmful in a HTTP2 world. )

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址