Configure HTTP/2 in Nginx on Ubuntu 20.04 - Best way to do it ?

The HTTP/2 protocol is an improvement of its predecessor HTTP/1.1. It provides numerous benefits and improvements such as faster page loading speeds and increased security. If you are running Nginx on HTTP/1.1, then you should consider migrating to HTTP/2.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Nginx Http queries.

In this context, we shall look into How to enable HTTP/2 on Nginx on Ubuntu 20.04.

Steps to configure HTTP/2 on your Nginx server

Before you set up HTTP/2 on your Nginx webserver, ensure you have the following:

  • Nginx
  • OpenSSL

The Nginx web server should be v 1.9.5 or greater. Previously, We dealt with a guide on how to install Nginx on Ubuntu 20.04 . To check the version of Nginx installed in the system, simply run the below command:

$ nginx -V

Also, ensure that you have OpenSSL 1.0.2 or later versions. To verify the version you are running, issue the command:

$ openssl version

Note that for this to work correctly, you need to have the webserver encrypted with an SSL certificate. Here, our webserver is encrypted using the Let's Encrypt SSL certificate. Already we have this set up where our domain name is and pointed to our virtual server's IP address. Additionally, we have configured a virtual host file for the domain.

Finally, your server must be using TLS v 1.2 and later versions.

Now, perform the below steps.

1. Test that your site is using HTTP/1

To get started out, we need to verify that our web server is currently using HTTP/1.1. To achieve this, we will run the curl command shown:

$ curl -I https://domain-name

In our case, this is going to be:

$ curl -I

The first line of the output clearly indicates that we are using HTTP/1.1

2. Enable HTTP/2 by editing the virtual host file

To enable HTTP/2 we need to edit or modify the domain's virtual host file.

$ sudo vim /etc/nginx/sites-available/

Locate this line:

listen 443 ssl 

Append the attribute http2 just after ssl.

In case you have a line that starts as follows:

listen [::]:443 ssl …

Add the http2 attribute as well.

Save the changes and exit.

3. Verify if Nginx configuration is Ok

Once you have exited the virtual host file, restart the Nginx webserver:

$ sudo systemctl restart nginx

Next, check if the Nginx configuration is sound:

$ sudo nginx -t

From the output, if the Nginx configuration file syntax and test is fine, then Nginx configuration is OK.

4. Check if HTTP/2 is enabled

Finally, to verify if HTTP/2 is enabled, execute the below curl command:

$ curl -I

You will see from the output that HTTP/2 is now enabled.

[Need assistance in fixing Nginx configuration issues? We can help you. ]

This article covers how to enable HTTP/2 on the Nginx webserver on Ubuntu 20.04. In fact, the primary focus of HTTP/2 is to reduce overall web page loading time, thus improving performance. It also focuses on network and server resource usage as well as security because, with HTTP/2, SSL/TLS encryption is mandatory.

If the Nginx server is sitting behind a web application firewall, ensure that the web application firewall (WAF) is capable of parsing HTTP/2 traffic.

HTTP/2 protocol main advantage over its predecessor HTTP 1.1 is that it has a higher transfer speed required for content-rich websites.

Related Posts