How to Enable HTTP/2 in Nginx on Ubuntu and CentOS


HTTP/2 is a major revision of the HTTP network protocol and it focuses on performance improvements. Its goal is to reduce the latency as well as to make the web applications faster by allowing multiple concurrent requests between the web browser and the server across a single TCP connection. In this tutorial, we are going to show you how to enable HTTP/2 in Nginx on a Linux VPS using Ubuntu or CentOS as an operating system. If you use Apache, you can check our tutorial on how to enable HTTP/2 in Apache on Ubuntu.

Prerequisites

In order to be able to follow the instructions and enable HTTP/2 on your server, you need to have Nginx already preinstalled. Make sure that it is functional and there are no errors with its configuration. You can check this using the command below:

sudo nginx -t

Additionally, you need to have root access to the server or at least you need to have a non-root system user with sudo privileges so you can make changes in the Nginx configuration files without having permission problems. Finally, you need to have a domain name and valid SSL certificate issued for the domain name.

Enable HTTP/2 in Nginx on Ubuntu

To enable HTTP/2 in Nginx on an Ubuntu VPS you should edit the default Nginx server block. We will use nano but you can use a text editor of your choice.

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

Add the following server block:

server {  
        server_name domain.com www.domain.com;
        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.com.crt;
        ssl_certificate_key /etc/nginx/ssl/domain.com.key;
}

server {
       listen         80;
       server_name    domain.com www.domain.com;
       return         301 https://$server_name$request_uri;
}

Make sure that domain.com is replaced with your real domain name. Additionally, the document root and the path to the SSL certificate and key should be correctly set.

Once you finish with editing the server block, save and close the file. Check if there are errors in the Nginx configuration using the command:

sudo nginx -t

And then restart Nginx for the changes to take effect

sudo systemctl restart nginx.service

If you’d like to enable HTTP/2 for another domain name, you can check our blog post on how to set up Nginx server blocks on Ubuntu and CentOS.

Enable HTTP/2 in Nginx on CentOS

To enable HTTP/2 on a CentOS VPS you need to follow the exact same steps as on Ubuntu. The location of the Nginx block file is the only difference. To edit the default Nginx server block on CentOS you should look into the /etc/nginx/conf.d directory.

# nano /etc/nginx/conf.d/default.conf

Once again, check if there are errors with the configuration, save and close the file, then restart the Nginx service using the command below:

# systemctl restart nginx.service

 

To check whether HTTP/2 is enabled in Nginx you can use some online HTTP/2 checker tool.

Of course you don’t have to do any of this if you use one of our Linux VPS hosting services, in which case you can simply ask our expert Linux admins to help you enable HTTP/2 in Nginx on your server. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a comment. Thanks.

Tutorials
Install Open Web Analytics (OWA) on CentOS 7
Tutorials
How to install and integrate DKIM with OpenDKIM and Postfix on a CentOS 6 VPS
Tutorials
How to install Chive on a Debian Wheezy VPS
  • Suresha B

    Suresha BSuresha B

    Author Reply

    Hello,

    Thanks for sharing such an amazing and helpful post. HTTP/2 is more essential for every site.