Enabling HTTP/2 on Nginx

The HTTP/2 protocol is the latest craze with web servers at the moment. The updated protocol has many performance enhancements over the older HTTP/1.1 protocol mainly due to the fact requests are downloaded in parallel, so therefore its multiplexed over a single connection.

While HTTP/2 will work with non-ssl enabled websites, popular browsers such as Firefox and Chrome will only support HTTP/2 for SSL enabled websites. So how do we get started with HTTP/2 on Nginx?

Nginx makes HTTP/2 simple! The HTTP/2 protocol has been included in Nginx since version 1.9.5, so it works in CentOS 6 and 7 as well as Ubuntu 14.04, 16.04 and 18.04.

A special note about Ubuntu 14.04 which is set to go EOL on 4/2019, the default Nginx package that gets installed via apt is very old (v1.4.6) and therefore does not support HTTP/2. However if you install Nginx from the ppa:nginx/stable PPA, you can make use of HTTP/2. You can install nginx from the PPA by:

# This applies to Ubuntu 14.04 ONLY.
[root@web01-ubuntu1404 ~]# apt-get install python-software-properties
[root@web01-ubuntu1404 ~]# add-apt-repository ppa:nginx/stable
[root@web01-ubuntu1404 ~]# apt-get update
[root@web01-ubuntu1404 ~]# apt-get install nginx

For CentOS 6 and 7, you can install Nginx from EPEL. On Ubuntu 16.04 and 18.04, HTTP/2 will work fine with the default Nginx packages provided by Ubuntu.

From here, enabling HTTP/2 is as easy as updating the listen directive within the Nginx vhost config to include ‘http2’ as shown below:

[root@web01 ~]# vim /etc/nginx/conf.d/example.com.conf
...
listen 443 ssl http2

Now check the Nginx configuration to ensure there are no problems:

[root@web01 ~]# nginx -t

Finally, restart Nginx to apply the change:

[root@web01 ~]# service nginx restart

You can test to ensure its working by checking for the protocol header as shown below:

[root@workstation ~]# curl -IL https://www.example.com
...
HTTP/2 200
...