HTTP2 and NGINX - when would I use a keepalive directive?
Asked Answered
B

1

13

I am experimenting with migration to http/2. I have set up a Wordpress website and configured NGINX to serve it using http/2 (using 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.

To achieve a similar effect for http/1.x, NGINX offered the keepalive directive.

So does using the keepalive directive make sense when using http/2 exclusively? Checking my config files with "nginx -t" reports no errors when I include it. But does it have any effect? Benchmarking showed no difference.

Berners answered 4/7, 2016 at 15:43 Comment(0)
C
24

You're misunderstanding how this works.

Keep Alive's work between requests.

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.).

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. Then it requests the remaining 14 resources as those connections free up. 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. 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.

Under HTTP/2 we can fire off all 20 requests at once on the same connection so some good gains there. 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.

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. Those will benefit from keep-alives whether on HTTP/1.1 or HTTP/2.

So HTTP/2 doesn't negate need for keep-alives. 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. And what type of benchmarking are you doing to say it makes no difference?

Castrate answered 4/7, 2016 at 18:3 Comment(4)
Thanks for pointing out that distinction. The trouble is that setting "keep-alive" seems to make no difference in testing (benchmarking was the wrong word, sorry). When using curl -I --http1.1 with keep-alive the reply includes Connection: keep-alive. But using curl -I --http2 shows no such entry.Berners
Under HTTP/2 keep alives are the default and HTTP/2 doesn't use the connection header (see section 8.1.2.2 of the HTTP/2 spec here: http2.github.io/http2-spec/#HttpHeaders).Castrate
If in HTTP/2 keep-alives are the default, I don't have to set anything in the webserver config (except switching on HTTP/2)? Or is this specific to each webserver? And how would I find out if keep-alive is "on" or "off"?Berners
Sorry just spotted this and sorry I wasn’t clear previously. Under HTTP/1.1 keep-Alice’s are the default but can be turned off by setting a keep-live: close. Under HTTP/2 they cannot be turned off at a HTTP level so are always on.Castrate

© 2022 - 2024 — McMap. All rights reserved.