Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nginx

http2

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.

like image 351
michbona Avatar asked Jul 04 '16 15:07

michbona


People also ask

Does HTTP2 have Keep-Alive?

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.

Should I use Keep-Alive?

The Benefits of Connection Keep Alive The HTTP keep-alive header maintains a connection between a client and your server, reducing the time needed to serve files. A persistent connection also reduces the number of TCP and SSL/TLS connection requests, leading to a drop in round trip time (RTT).

What is Keep-Alive in Nginx?

In Nginx, keepalive is a directive that is utilized for keeping the connection open for a certain number of requests to the server or until the request timeout period has expired.

What is the use of Keep-Alive?

A keepalive (KA) is a message sent by one device to another to check that the link between the two is operating, or to prevent the link from being broken.


1 Answers

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?

like image 72
Barry Pollard Avatar answered Oct 26 '22 09:10

Barry Pollard