How to Enable HTTP/2.0 in Nginx

HTTP/2 is the latest standard for the HTTP protocol, it is the successor of HTTP/1.1. It is becoming increasingly popular due to the benefits it brings to web developers and users in general. It provides an optimized transport for HTTP semantics by supporting all the core features of HTTP/1.1 but aims to be more efficient in multiple ways.

There are a lot of features on top of HTTP/2 that give you more possibilities to optimize a web site/application. It offers true multiplexing and concurrency, better header compression (binary encoding), better prioritization, better flow control mechanisms, and a new interaction mode called “server push” that enables a server to push responses to a client. Not to mention, HTTP/2 is based on Google’s experimental SPDY protocol.

Therefore, 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.

In this article, you will learn how to enable Nginx with HTTP/2 support in Linux servers.

Prerequisites:

  • A working installation of NGINX version 1.9.5 or higher, built with the ngx_http_v2_module module.
  • Make sure that your site uses SSL/TLS certificate, if you don’t have one, you can obtain from Let’s Encrypt or use a self-signed certificate.

You can install NGINX or deploy it with a LEMP stack as described in the following guides:

How to Enable HTTP/2.0 in NGINX

If you have NGINX installed, verify that it was built with the ngx_http_v2_module module by running the following command.

# strings /usr/sbin/nginx | grep _module | grep -v configure| sort | grep ngx_http_v2_module
Check Nginx HTTP/2 Module
Check Nginx HTTP/2 Module

Once you have a web site/application being served by NGINX with HTTPS configured, open your websites virtual server block (or virtual host) file for editing.

# vi /etc/nginx/conf.d/example.com.conf                    [On CentOS/RHEL]
$ sudo nano /etc/nginx/sites-available/example.com.conf    [On Ubuntu/Debian]

You can enable HTTP/2 support by simply adding the http2 parameter to all listen directives as shown in the following screenshot.

listen 443 ssl http2;

The sample server block configuration looks like below.

server {
        server_name example.com www.example.com;
        access_log  /var/log/nginx/example.com_access.log;
        error_log  /var/log/nginx/example.com_error.log;

        listen [::]:443 ssl ipv6only=on http2; # managed by Certbot
        listen 443 ssl http2; # managed by Certbot

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot    
}
Enable HTTP/2 Support in Nginx
Enable HTTP/2 Support in Nginx

Save the changes in the file and close it.

Then check the NGINX’s configuration syntax, if it’s OK, restart the Nginx service.

# nginx -t
# systemctl restart nginx

Next, open a web browser to verify if your website is being served over HTTP/2.

http://www.example.com

To access the HTTP headers, right-click on the displayed web page, select Inspect from the list of options to open the developer tools, then click the Network tab, and reload the page.

Check under Protocols to see the one your site is using (if you don’t see the Protocols header, right-click on any of the headers e.g Name, then check Protocol from the list to display it as a header).

If your site is running on HTTP/1.1, under Protocol, you will see http/1.1 as shown in the following screenshot.

Website Running on HTTP/1.1
Website Running on HTTP/1.1

If it is running on HTTP/2, under Protocol, you will see h2 as shown in the following screenshot. You may want to disable the browser cache to view the latest content being served directly from the webserver.

Website Running on HTTP/2.0
Website Running on HTTP/2.0

That’s all! For more information, see the ngx_http_v2_module module documentation. Do not hesitate to ask questions via the feedback form below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.