How To Enable HTTP 2 On Nginx
How To Enable HTTP 2 On Nginx
July 05, 2020

Hypertext Transfer Protocol Version 2 (HTTP/2) is the latest version of the HTTP protocol. It adds several new features (Binary Protocols, Multiplexing, Header Compression, Server Push, Security) as compared to the previous version of the HTTP protocol i.e. HTTP/1.1. The most important feature is the usage of binary format instead of plain text format i.e. it encapsulates all the messages in the binary format following the HTTP semantics including verbs, methods, and headers. This makes sure that the servers and clients can continue with the existing applications. Also, HTTP/2 makes a persistent connection to the server which allows clients to send multiple requests without waiting for the response. The server can push resources to the client since the connection remains open. The HTTP/2 protocol allows only encrypted connections which further increases the application security.

The Nginx Web Server supports HTTP/2 protocol since version 1.9.5. The current version of Nginx on Ubuntu 18.04 LTS repositories is 1.14.0 and Ubuntu 20.04 LTS repositories is 1.17.10. We can always install Nginx using it's official PPA as explained at How To Install And Configure Nginx on Ubuntu 18.04 LTS and How To Install And Configure Nginx on Ubuntu 20.04 LTS. We can install the most recent version of Nginx i.e. Nginx 1.19.0 on Ubuntu 18.04 LTS and Ubuntu 20.04 LTS using official PPA. This tutorial provides the steps to configure the Nginx Web Server to use the HTTP/2 protocol to send and receive the HTTP messages. This tutorial provides all the steps for Ubuntu 20.04 LTS. The steps should be similar on other versions of Ubuntu and Linux systems.

Prerequisites

This tutorial assumes that the virtual hosts are configured to allow only HTTPS requests. You can follow Configure Virtual Host Or Server Block On NginxHow To Install Let's Encrypt For Nginx On Ubuntu, and Redirect HTTP to HTTPS on Nginx to enable HTTPS only.

It also assumes that the Nginx Web Server is configured to use TLS >= 1.2. You can follow How To Enable TLS 1.2 and TLS 1.3 in Nginx. Your client or the browser should also support HTTP/2.

We can check the Nginx and OpenSSL version as shown below.

# Nginx Version
nginx -v

# Output
nginx version: nginx/1.17.10 (Ubuntu)

# OpenSSL Version
openssl version

# Output
OpenSSL 1.1.1f 31 Mar 2020

Check HTTP Version

Now write a simple program and check the HTTP version by inspecting the page as shown in Fig 1.

Enable HTTP 2 On Nginx - HTTP/1

Fig 1

Enable HTTP/2

This section provides the steps to enable HTTP/2 on Nginx Web Server. We can update the virtual host to enable HTTP/2 as shown below.

# Update Virtual Host
sudo nano /etc/nginx/sites-available/example.com

# Update
server {
listen 80;
server_name example.com www.example.com;

# Redirect to https
return 301 https://$host$request_uri;
}

server {
#listen 80;
server_name example.com www.example.com;

root /var/www/example.com/html;

index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}

listen 443 ssl http2;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

# Save and Exit - Ctrl + o -> Enter + Ctrl + x

# Reload Nginx
sudo systemctl reload nginx
# OR
sudo service nginx reload

Instead of specifying the SSL Protocol in a virtual host, we can also update the SSL configuration of Nginx for all the virtual hosts as shown in How To Enable TLS 1.2 and TLS 1.3 in Nginx.

Now again open the demo page. It should reflect HTTP/2 as shown in Fig 2.

Enable HTTP 2 On Nginx - HTTP/2

Fig 2

This is how we can enable the HTTP 2 protocol on Nginx Web Server.

Summary

This tutorial provided the steps required to enable the HTTP 2 protocol on the Nginx Web Server.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS