HTTP/2 Support

Introduction

I’ve been reading about HTTP/2 and wanted to implement it on my site. Luckily for me, implementing HTTP/2 is as easy as installing a newer version of Nginx.

About HTTP/2

HTTP/2 is the successor of HTTP/1.1, which itself is a successor of HTTP/1.0. HTTP/2 was first discussed as a next-generation protocol when it was shown that the SPDY protocol was a major improvement over HTTP/1.1. HTTP/2 has a few key changes over HTTP/1.1, as listed below. HTTP/2:

  • is binary, instead of textual
  • is fully multiplexed, instead of ordered and blocking
  • can therefore use one connection for parallelism
  • uses header compression to reduce overhead
  • allows servers to “push” responses proactively into client caches

More information about HTTP/2 is available on this site. However, a major point to make is that although the HTTP/2 protocol does not require SSL/TLS encryption, all major browsers will not support HTTP/2 without it. This means that in order for your audience to view your HTTP/2 site, you’ll need to serve it over SSL/TLS.

About Nginx

Nginx is available in two main branches: stable and mainline. Explained in a blog post (and the picture below), stable is a snapshot of mainline at a certain point in time. Between versions, stable will only include major bug fixes, while mainline will include new features, updates, and bug fixes. Interestingly, the Nginx team generally recommends you “deploy the NGINX mainline branch at all times” (because it has new features, updates, and bug fixes).

20160401_001

 

Nginx is also available in a paid version, called Nginx Plus. Nginx Plus is a fork of Nginx mainline, as shown below. The differences between these version are described on the Nginx site.

20160401_002

Get Nginx

You’ll need to use a version of Nginx that supports HTTP/2.  According to the Nginx website, Nginx 1.9.5 was the first version to support the HTTP/2 module (ngx_http_v2_module). At the time of this writing, only the mainline branch of Nginx is at version 1.9.5, so I’ll be using that version.

To install Nginx, I’m using a PPA from the Nginx Launchpad site. In this case, the development PPA is the mainline branch of Nginx. I’m using the Launchpad PPA because I use the nginx-extras package (it has additional pre-compiled modules), as described in a previous post.

sudo apt-get remove nginx*
sudo apt-add-repository ppa:nginx/development
sudo apt-get update && sudo apt-get install nginx-extras
sudo service nginx restart

Update Nginx config file

Using HTTP/2 is as easy as adding http2 after the listen directive in your configuration file. The HTTP/2 module replaces the SPDY module (ngx_http_spdy_module), so I’m going to replace spdy with http2 in my file.

Before…

server {
        listen 80;                      #Listen on IPv4
        listen [::]:80;                 #Listen on IPv6
        server_name loganmarchione.com loganmarchione.com www.loganmarchione.bit loganmarchione.bit;
        return 301 https://$host$request_uri;           #Redirect HTTP to HTTPS
}

server {
        listen 443 ssl spdy default_server;            #Listen on IPv4
        listen [::]:443 ssl spdy default_server;       #Listen on IPv6
        server_name loganmarchione.com loganmarchione.com www.loganmarchione.bit loganmarchione.bit;

        ssl on;
        ...
        rest of config file here
        ...
}

After…

server {
        listen 80;                      #Listen on IPv4
        listen [::]:80;                 #Listen on IPv6
        server_name loganmarchione.com loganmarchione.com www.loganmarchione.bit loganmarchione.bit;
        return 301 https://$host$request_uri;           #Redirect HTTP to HTTPS
}

server {
        listen 443 ssl http2 default_server;            #Listen on IPv4
        listen [::]:443 ssl http2 default_server;       #Listen on IPv6
        server_name loganmarchione.com loganmarchione.com www.loganmarchione.bit loganmarchione.bit;

        ssl on;
        ...
        rest of config file here
        ...
}

After you save your config, I recommend testing it before reloading Nginx.

sudo nginx -t
sudo service nginx reload

Test HTTP/2 support

You can use this site to test HTTP/2 support on your Nginx site. There is also an add-on for Chrome that will show if the site you’re visiting is using SPDY or HTTP/2.

 

Let me know how HTTP/2 works for you!

Logan

Leave a Comment