HTTP/2 is the new version of now obsolete HTTP/1.1 protocol which was standardized way back in 1999. A lot has changed on the web since then. Our applications are more complex than they were back than, so to cope with that, change in the underlying transport protocol was necessary. The most important thing about HTTP/2 is that it will make your web page faster for the end-users.

Here are the quick facts about HTTP protocol version 2:

  • HTTP/2 is binary protocol
  • HTTP/2 is backward compatible with HTTP/1.1
  • RFC 7540 published in 2015
  • HTTP/2 is comprised of 2 specifications:
    • Hypertext Transfer Protocol version 2 - RFC7540
    • HPACK - Header Compression for HTTP/2 - RFC7541
  • HTTP/2 will make our apps faster, simpler and more robust.
  • Primary goals for HTTP/2 are to reduce latency
  • HTTP/2 is based on Google's SPDY protocol
  • Browsers require modern TLS for HTTP/2
  • The HTTP/2 spec ended up specifying that TLS should be at least version 1.2 and there are cipher suite restrictions.
  • TLS 1.2 is enforced. If a server negotiates HTTP/2 with a lower TLS version, it is treated as a protocol error
  • Some key fatures of HTTP/2 are multiplexing, header compression, prioritization, server push and protocol negotiation
  • HTTP/2 protocol is negotiated via NPN or ALPN
  • HTTP semantics are mostly the same, methods, status codes, header fields, URLs are are intact.

Requirements

  • Nginx version 1.9.5 or above or Nginx Plus version R7 or above (nginx -v)
  • OpenSSL version 1.0.2 or higher (openssl version && openssl version -a)
  • SSL/TLS Certificate (Let's Encrypt is free option)
  • TLSv1.2 Protocol is required - implementations of HTTP/2 must use TLS version 1.2 or higher for HTTP/2 over TLS.

Install Nginx

Install Nginx from the official Nginx repository:

wget https://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
printf "deb https://nginx.org/packages/mainline/ubuntu/ `lsb_release -sc` nginx \ndeb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -sc` nginx \n" >> /etc/apt/sources.list.d/nginx_mainline.list
apt update
apt install -y nginx

This should install the latest mainline version of Nginx.

Nginx configuration for HTTP/2

Here is the minimal amount of configuration that is needed to enable HTTP/2 in Nginx. You should notice http2 on the listen directive. That's the only necessary parameter to enable HTTP/2. Also, it's good to be aware of TLS 1.2 requirement for HTTP/2. That's why you need to set at least TLSv1.2 to ssl_protocols directive.

server {
  listen 80;
  return 301 https://$host$request_uri;
}

server {

  listen 443 ssl http2 default_server;

  server_name example.com www.example.com;
  
  root /path/to/public;
  
  ssl_certificate /path/to/certificate.pem;
  ssl_certificate_key /path/to/private_key.pem;
  
  ssl_protocols TLSv1.2 TLSv1.3;
  
}

And that's all folks. Really it's very simple once you know all the requirements. Not much to say about it. If you use some other Linux distro, it's only a matter of changing few commands to install needed software and all other is the same. Cheers!