How to Enable HTTP/2 Support in Nginx

r00t October 26, 2017

Enable HTTP/2 Support in Nginx

In this tutorial we’ll learn how to enable HTTP/2 support in Nginx. We will also install and configure its prerequisites. I’ve been a keen follower of these exciting changes to the HTTP specification over the past couple of years — with the launch of the HTTP/2 standard in 2015. If you are unfamiliar with http/2 there are a number of great videos available which outline just what it is and how it is different from http 1.1 — which we’ve been so accustomed to over the years.

In light of these changes and of the ever increasing browser support with this feature (over 70% when employing an SSL/TLS connection), I wanted to give it a try. I had a website on a Digital Ocean droplet, running on Ubuntu 16.04 (LTS) and the LAMP stack already with an SSL certificate setup (the perfect candidate for getting http/2 working). After some digging around on various blogs which were discussing the topic, I came across a few guides that helped, this guide is a combination of a few.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Enable HTTP/2 Support in Nginx

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing latest version of Nginx.

HTTP/2 protocol supported over nginx version >=1.9.5. So make sure that your nginx version is supporting HTTP/2 protocol or not. If you have not installed Nginx or older version use one of following method to install or upgrade it:

echo "deb http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list

Use the following commands to install it:

curl http://nginx.org/keys/nginx_signing.key | apt-key add -
sudo apt-get update
sudo apt-get install nginx

Verify Nginx version:

nginx -v

Step 3. Enable HTTP/2 module.

To enable HTTP/2 in Nginx on an Ubuntu you should edit the default Nginx server block:

sudo nano /etc/nginx/sites-available/default

Change:

listen 443 ssl;

To:

listen 443 ssl http2;

Restart Nginx to apply your changes:

systemctl restart nginx

Example server block config file:

server {
 listen 443 ssl http2;
 server_name exmple.com;

location / {
 root /var/www/example.com;
 index index.html index.htm;
 }

ssl on;
 ssl_certificate /etc/nginx/ssl/example.com.crt;
 ssl_certificate_key /etc/nginx/ssl/example.com.key;

}

Step 4. Verify if Website Support HTTP/2.

  • Manually – open IE/Chrome/Firefox and press F12 >> go to network tab and you should see protocol as HTTP/2
  • Online – you can use HTTP/2 Test tool

Congratulation’s! You have successfully enable HTTP/2 support in Nginx on your Ubuntu 16.04 server. Thanks for using this tutorial enable HTTP/2 on Ubuntu 16.04 LTS (Xenial Xerus) system.

Save

The Tags:

Leave a Comment

Comments are closed.