How to Configure HTTP/2 in Nginx on Ubuntu 20.04
HTTP/2 is an improved version of the HTTP protocol that allows you to increase the speed of the site and reduce the load on the web server. All this is possible by making multiple simultaneous requests in a single TCP connection. Increasing the speed not only improves the user experience with the site, but potentially even elevates its position in the search engine results. In this tutorial we will look at how to configure HTTP/2 in the Nginx web server on the Ubuntu 20.04 system.
Necessary conditions:
- The Nginx web server is installed and its basic configuration is performed;
- Your site uses the HTTPS protocol. Most web browsers require an encrypted connection to enable HTTP/2.
Preparing the system for HTTP/2
First, you need a fresh version of the Nginx web server to be able to enable HTTP/2. Update the packages in the system.
apt update && apt upgrade
Configuring the HTTP/2 in Nginx
Open the configuration file of your virtual host.
nano /etc/nginx/sites-available/domain-name.com
Find this line in it:
listen 443 ssl;
Add http2 here:
listen 443 ssl http2;
If you see the line, starting with:
listen [::]:443 ssl ...
Then add http2 in it too.
listen [::]:443 ssl http2 ...
Save and close the file, and check the configuration for errors.
nginx -t
Restart Nginx.
systemctl restart nginx
That's it, the HTTP/2 protocol is enabled on your website.
Check if the HTTP/2 is enabled
You can check whether HTTP / 2 is enabled directly in the command line of any Linux machine. To do this, you need to install curl. For example, for Ubuntu:
apt install curl
Now enter this command with your FQDN in it:
curl -I https://domain-name.com
Output:
HTTP/2 200
...
This means that your site is currently using HTTP/2.
Another way to check it is to use this service: https://tools.keycdn.com/http2-test.