How to enable HTTP / 2.0 support on Nginx

In this article, we will show you How to enable HTTP / 2.0 support in Nginx on Ubuntu 16.04 LTS… For those of you who don’t know, HTTP / 2 is the new and updated HTTP protocol and should be much more efficient than the now deprecated version of HTTP / 1.1. Its purpose is to reduce latency and also make web applications faster by allowing multiple concurrent requests between the web browser and the server over a single TCP connection. If you want to speed up the loading time of your website or blog, then you must enable HTTP / 2.0 in your web server.

This article assumes that you at least have basic Linux knowledge, know how to use the shell, and most importantly, you host your site on your own VPS. Installation is very straightforward and assumes that you are running under the root account unless you can add “sudo” to the commands to gain superuser privileges. We will show you step by step how to enable HTTP / 2.0 support on Ubuntu 16.04 (Xenial Xerus) running an Nginx server.

Enabling HTTP / 2.0 Support on Nginx

Step 1. System update

First make sure all system packages are up to date by running these apt-get commands in terminal.

sudo apt-get update
sudo apt-get upgrade

Step 2. Enabling HTTP / 2.0 on the Nginx web server.

Make sure your version of Nginx supports HTTP / 2 protocol. If you haven’t installed Nginx or are using an older version, update it first:

apt-get install nginx

You can use the following to check your Nginx version:

nginx -v
# nginx version: nginx/1.10.1

To enable HTTP / 2 in Nginx on Ubuntu VPS, you must change the default Nginx server block:

nano /etc/nginx/sites-available/default

Add the http2 keywords for the virtual host block / server configuration:

server {
        server_name andreyex.ru www.andreyex.ru;
        listen 443 ssl http2 default_server;
        root /var/www/html;
        index index.html;
 
        location / {
                try_files $uri $uri/ =404;
        }
 
        ssl_certificate /etc/nginx/ssl/domain.ru.crt;
        ssl_certificate_key /etc/nginx/ssl/domain.ru.key;
}
 
server {
       listen         80;
       server_name    domain.com www.domain.com;
       return         301 https://$server_name$request_uri;
}

Enabling is really easy, just change the current SSL line to do this:

server {
  listen        443 ssl;
  ...
}

to this:

server {
  listen        443 ssl http2;
  ...
}

After you are done editing the server block, save and close the file. Check if there are any errors in the Nginx configuration with the command:

nginx -t

And then restart Nginx for the changes to take effect:

systemctl restart nginx.service

Step 3. Checking HTTP / 2.

Go to https://tools.keycdn.com/http2-test and check for HTTP / 2.0 detection on the domain:

https://tools.keycdn.com/http2-test

Congratulations! You have successfully enabled HTTP / 2.0 on your web server. Thank you for using this article to enable HTTP / 2.0 support on Nginx running Ubuntu 16.04 LTS (Xerus Xerus). For further assistance or useful information, we recommend that you visit official nginx website

Sidebar