简体   繁体   English

HTTP2和NGINX - 何时使用keepalive指令?

[英]HTTP2 and NGINX - when would I use a keepalive directive?

I am experimenting with migration to http/2. 我正在尝试迁移到http / 2。 I have set up a Wordpress website and configured NGINX to serve it using http/2 (using SSL/TLS). 我已经设置了一个Wordpress网站并配置了NGINX来使用http / 2(使用SSL / TLS)来提供服务。

My understanding of http/2 is that by default it uses one connection to transfer a number of files - rather than opening new connections and repeating SSL handshakes for each file. 我对http / 2的理解是,默认情况下,它使用一个连接来传输大量文件 - 而不是打开新连接并为每个文件重复SSL握手。

To achieve a similar effect for http/1.x, NGINX offered the keepalive directive. 为了达到类似http / 1.x的效果,NGINX提供了keepalive指令。

So does using the keepalive directive make sense when using http/2 exclusively? 那么在使用http / 2时使用keepalive指令是否有意义? Checking my config files with "nginx -t" reports no errors when I include it. 使用“nginx -t”检查我的配置文件时,如果包含它,则报告没有错误。 But does it have any effect? 但它有什么影响吗? Benchmarking showed no difference. 基准测试显示没有差异。

You're misunderstanding how this works. 你误解了它是如何工作的。

Keep Alive's work between requests. 保持Alive 请求之间的工作。

When you download a webpage it downloads the HTML page and discovers it needs another 20 resources say (CSS files, javascript files, images, fonts... etc.). 下载网页时,它会下载HTML页面并发现它需要另外20个资源(CSS文件,javascript文件,图像,字体......等)。

Under HTTP/1.1 you can only request one of these resources at once so typically the web browser fires up another 5 connections (giving 6 in total) and requests 6 of those 20 resources. 在HTTP / 1.1下,您只能同时请求其中一个资源,因此通常Web浏览器会激活另外5个连接(总共提供6个连接)并请求这20个资源中的6个。 Then it requests the remaining 14 resources as those connections free up. 然后,当这些连接释放时,它会请求剩余的14个资源。 Yes keep-alives help in between those requests but that's not its only use as we'll discuss below. 是的,在这些请求之间保持联系,但这不是它的唯一用途,我们将在下面讨论。 The overhead of setting up those connections is small but noticeable and there is a delay in only being able to request 6 resources of those 20 at a time. 设置这些连接的开销很小但很明显,并且只能一次请求这20个资源中的6个资源。 This is why HTTP/1.1 is inefficient for today's usage of the web where a typical web page is made up of 100 resources. 这就是为什么HTTP / 1.1对于今天使用网络来说效率低的原因,其中典型的网页由100个资源组成。

Under HTTP/2 we can fire off all 20 requests at once on the same connection so some good gains there. 在HTTP / 2下,我们可以在同一个连接上同时触发所有20个请求,因此可以获得一些好处。 And yes technically you don't really benefit from keep-alives in between those as connection is still in use until they all arrive - though still benefit from small delay between first HTML request and the other 20. 从技术上讲,你并没有真正受益于那些之间的保持活动,因为连接仍在使用,直到它们全部到达 - 尽管仍然受益于第一个HTML请求和其他20个请求之间的小延迟。

However after that initial load there are likely to be more requests. 但是,在初始加载之后,可能会有更多请求。 Either because you are browsing around the site or because you interact with the page and it makes addition XHR api calls. 要么是因为您正在浏览网站,要么是因为您与该页面进行了交互,而且它会增加XHR api调用。 Those will benefit from keep-alives whether on HTTP/1.1 or HTTP/2. 无论是在HTTP / 1.1还是HTTP / 2上,这些都将受益于保持活动。

So HTTP/2 doesn't negate need for keep-alives. 所以HTTP / 2并不否定对保持活动的需求。 It negates need for multiple connections (amongst other things). 它不需要多个连接(除其他外)。

So the answer is to always use keep-alives unless you've a very good reason not to. 所以答案是始终使用keep-alives,除非你有充分的理由不这样做。 And what type of benchmarking are you doing to say it makes no difference? 您正在做什么类型的基准测试来说它没有区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM