How to enable HTTP / 2 on Nginx on Linux

HTTP / 2 is a new generation of protocol to replace the HTTP / 1.1 protocol that you use in your daily tasks on the Internet. Based on fabulous SPDYThis new HTTP protocol is focused on providing the best performance ever between network connections and end users. In particular, it aims to improve end-user latency and network congestion. Its main purpose is to allow the use of a single connection between client browsers and a website.

Main features of HTTP / 2

True multiplexing: HTTP / 2 allows requests to be executed in parallel and out of order, and not one at a time, as in the HTTP / 1.1 protocol.

One persistent connection: With true multiplexing functionality, all objects hosted on the site can be downloaded in parallel using only one connection. In HTTP / 1.1, if you wanted to load parallel objects, you were forced to open multiple connections (not efficient in terms of network usage).

Binary encoding: The headers are sent binary encoded. This saves bytes on the way until the information reaches your client’s browser. In HTTP / 1.1, it was sent using plain text, which was more expensive in terms of bandwidth usage.

Compressing headers: Headers are also compressed with compression HPACK, which helps to reduce the total amount of data on the network.

SSL / TLS encryption: With HTTP / 2 SSL / TSL, encryption is required. This is controlled and defined by web browsers (instead in RFCs), which currently support the new HTTP / 2 protocol. HTTP / 2 not only takes care of speed but also your security by adopting SSL / TSL encryption as a requirement.

Notes Before Enabling HTTP / 2 on Nginx

1.Http / 2 in currently available for Nginx users and Nginx Plus users (commercial version of Nginx).

2. If you do not have SSL encryption on your site, you must redirect all SSL / TLS traffic as HTTP / 2 works with SSL / TLS encryption. Make sure your site is fully configured to work with an SSL certificate.

3. SPDY and HTTP / 2 cannot work together. Remove the “SPDY” variant from all directives listen inside Nginx config, and replace it with ‘http2’ and ‘SSL’ parameters.

How can you enable HTTP / 2 on Nginx?

To enable HTTP / 2 support on Nginx, simply add the “http2” option for the directives listenthat previously had. Don’t forget to enable the SSL option, example:

listen 443 ssl http2 default_server;

Full output inside the server {} block:

server {listen 443 ssl http2 default_server
ssl_certificate /etc/nginx/conf/ssl.crt/yoursite.com.crt; ssl_certificate_key /etc/nginx/conf/ssl.key/yoursite.com.key; ... ... }

Update Nginx for the changes to take effect:

nginx -s

How can you check if HTTP / 2 is working on your site?

You need to add a plugin to the browser HTTP / 2 and SPDY indicator… It is available from the Google Chrome and Firefox store.

What about browsers that don’t support HTTP / 2?

People at Nginx Inc. already had this in mind, and because currently 50% of browsers still don’t support HTTP / 2, Nginx engineers have created what they call “Next Protocol Negotiation Negotiation (NPN).” This TLS extension allows Nginx to serve HTTP / 1.x requests if the browser does not fully support HTTP / 2.

Output

As you can see, the web is changing very quickly. In order to continue to grow, you must adopt new protocols and techniques that enable users to reduce page response times and network usage. HTTP / 2 is the next generation of the HTTP protocol. If you want to be part of this new change, try enabling it on your Nginx installation. The results will surprise you.

Sidebar