setup http2 in nginx

How To Enable HTTP2 in NGINX

Last updated on June 24th, 2024 at 09:05 am

HTTP/2 is a network protocol that is a major upgrade to the traditional HTTP protocol used by websites. It offers many advantages such as parallel processing, full multiplex, header compression, and even server push. It also improves data transfer speed and website security, without requiring any changes at the client end. It is important to set up HTTP2 in NGINX to improve website speed and performance. In this article, we will look at how to enable HTTP2 in NGINX.

What is HTTP2?

HTTP2 is a network protocol to facilitate running of websites on the internet. It is supported by all major web browsers and offers tremendous advantages over HTTP protocol. It improves performance and speed by changing the way data is compressed and transferred between server and web browser. So the users don’t need to make any changes at their end. It uses the same request format, headers and status codes as requests using HTTP protocol. The magic happens during data transmission. The protocol to be used for data transmission is decided during connection establishment. Thereafter, using the same data as HTTP headers, the HTTP2 protocol gives better performance. Please note, it also works seamlessly on old browsers that support only HTTP protocols.

Why Enable HTTP2 in NGINX

There are several compelling reasons for upgrading your website to HTTP2.

  1. Multiplexing – HTTP2 provides parallel processing by sending resources as multiple streams of data at once. On the other hand, HTTP loads data sequentially. So a large resource can hold up others. This doesn’t happen with HTTP2.
  2. Prioritization – HTTP does not allow you to decide which resource to load first on a web page. It is basically first-cum-first-serve method. HTTP2 allows developers to decide which parts of your page to load first. For example, you can choose to load visible text and images before loading background items, making your web page load faster.
  3. Header Compression – HTTP2 performs better compression of request/response headers than HTTP, by smartly identifying and eliminating redundant data.
  4. Server Push – HTTP2 even allows you to push content to client browsers before it is requested, thereby loading pages quickly.
  5. Security – HTTP2 requires you to use SSL/TLS so it is naturally more secure than HTTP.

How To Enable HTTP2 in NGINX

Here are the steps to enable HTTP2 in NGINX. Before proceeding, please ensure:

  1. You are using NGINX 1.9.5 or above. You can NGINX version with nginx -v command
  2. You have enabled HTTPS/SSL. Here are the steps to configure SSL in NGINX.

1. Backup NGIN configuration file

First, it is essential to take a backup of your NGINX configuration file since it is the backbone of your server and you do not want to be able to easily rollback changes if things do not go as expected. Open terminal and run the following command to do a backup of this file.

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx-backup.conf

Please note, in the next step, if you are going to use virtual host file instead of NGINX configuration file then take its backup instead.

$ sudo cp /etc/nginx/sites-enabled/example.conf /etc/nginx/sites-enabled/backup-example.conf

2. Open NGINX configuration file

Open terminal and run the following command to open NGINX configuration file.

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/example.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/example.conf

3. Setup HTTP/2

If you have enabled SSL in NGINX, you will already have the following line in your server configuration.

listen 443 ssl;

Change it to the following to enable HTTP/2. Just add ‘http2’ to listen directive as shown.

listen 443 ssl http2;

Your HTTPS server block will look something like

server { 
    listen 443 ssl http2; 
    ssl_certificate ...
    ssl_certificate_key ...
 }

4. Restart NGINX Server

Finally, run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

5. Verify HTTP/2

Use a third party tools like KeyCDN HTTP2 checker that allow you to check if HTTP/2 is enabled on your website.

After you enter your website, with HTTPS URL, it will tell you if HTTP/2 is enabled in it or not.

As you can see, it is quite easy to enable HTTP/2 in NGINX.

Conclusion

In this article, we have learnt what is HTTP2 protocol, why you should enable it on your NGINX server and also how to enable it. We highly recommend that you setup HTTP2 on your server. It is free of cost. You will start seeing immediate benefits after the upgrade. It will give better user experience, network bandwidth and server costs.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

Also read
How to Fix NGINX 413 Request too large error
How to Remove WWW from Domain URL in NGINX
How to Redirect HTTP to HTTPS in NGINX
How to Configure Basic Authentication in NGINX