HTTP/2 is the second major version of HTTP protocol released in 2015.

http-protocol-history

Around 46.5% of websites support HTTP/2.

HTTP2 statistics

It has many features like:

  • HTTP Header Compression
  • Push technologies
  • Load page elements in parallel over a single TCP connection
  • Binary protocol
  • Reduced latency

Demo by AKAMAI shows HTTP/2 is approximately two times faster than HTTP/1.1.

http2-demo

Did you know LiteSpeed was one of the first web servers to support HTTP/2?

If you use a CDN like Cloudflare, Incapsula, MaxCDN, you may not need to enable HTTP/2 in your web servers as you can do it from CDN network edge.

However, if you need to implement HTTP/2 in your web servers like Apache, Nginx, IIS, read on to learn how you can do it.

Note: All leading browsers (Chrome, Edge, Firefox, Safari, Opera) must use TLS 1.2 or newer for HTTP2 implementation.

But don’t worry if you are doing some experiments. You can get the TLS certificate for FREE.

Implement HTTP/2 in Apache HTTP

HTTP/2 can be deployed in Apache HTTP 2.4.17 or later version with the help of mod_http2 module. So if you have a 2.2 or lower version, you first have to upgrade to the compatible version.

Let’s compile Apache HTTP with the mod_http2, mod_ssl module.

  • Download the latest version (I’ll do 2.4.25 on Linux)
  • Extract the downloaded file, if you downloaded the .gz file, then
gunzip -c httpd-2.4.25.tar.gz | tar xvf -
  • You will see a new folder create. Go inside that
  • Build the Apache from source with configure command

Note: if you are trying this on a brand new server, you may encounter an error to install dependencies, and this guide should help you with that.

./configure --enable-ssl --enable-so --enable-http2
  • Ensure no error from above command once done, execute the following make command
make
make install

This concludes Apache HTTP is installed with HTTP/2 module and it’s time to do the necessary configuration.

  • Take a backup of httpd.conf file (default location /etc/httpd/conf/httpd.conf)
  • Open the httpd.conf file with vi and ensure the following exists; if not, then add it.
LoadModule http2_module modules/mod_http2.so
  • Next, you need to add the Protocols directive like below
Protocols h2 h2c http/1.1

If you are enabling HTTP/2 for an individual virtual host, you need to add Protocols under the respective VIrtualHost.

Note: there are three parameters in the above Protocols directive

  • h2 – instructing Apache to support HTTP/2 protocol over SSL/TLS
  • h2c – instructing Apache to support HTTP/2 over TCP
  • http/1.1 – if a client doesn’t accept HTTP/2 then serve the request over HTTP/1.1

Restart Apache HTTP server to reload the configuration.

Now, Apache HTTP instance is enabled to support HTTP/2 protocol.

Implement HTTP/2 in Nginx

Nginx 1.9.5 or higher version supports HTTP/2, so first, you have to ensure you have the compatible version installed.

Enabling HTTP/2 in Nginx is just a matter of adding the http2 parameter in listen directive.

  • As usual, take a backup of nginx.conf file (default location /etc/nginx/nginx.conf)
  • Modify using vi and add http2 under listen directive
server {
        listen       443 http2 ssl chandan.io;
        …....
}

As I mentioned above, HTTP/2 is supported only over HTTPS, so you got to add under server block which has SSL configuration.

If your SSL configuration is in a file other nginx.conf, you must update it there.

Once done, restart the Nginx for configuration to be active.

Verify if Website Supports HTTP/2

There are multiple ways to check if the website supports HTTP/2 or not.

Manually – open IE/Chrome/Firefox and press F12 >> go to the Network tab, and you should see Protocol as HTTP/2.

chrome-http2-verify

Online – you can use the HTTP/2 Test tool.

That’s all for today. I hope above helps you to enable HTTP/2 protocol in Apache and Nginx web servers for fast-loading web pages.