Boost the loading speed of your website using HTTP/2
Hypertext Transfer Protocol Version 2 (HTTP/2) is the latest version of the HTTP protocol, published as an IETF standard in RFC 7540 in 2015.
Why HTTP/2 need to update?
- Faster load times of web pages
- Security: encryption is enabled by default
- Mobile Friendly thanks to header compression
- Multiplexing makes hacking more difficult
But HTTP/2 also has some drawbacks.
- Older browsers and servers do not allow to save time when one of them does not support HTTP/2
- HTTPS is required, which means an SSL certificate is required as well. But this should not be a major issue, You can use Let's Encrypt or a self-signed certificate.
HTTP/2 shows significant improvements over the previous HTTP/1.1 protocol:
Multiplexing, Header optimization & Server Push.
- Multiplexing : With version 1.1, the protocol is only able to request files one by one on a single connection. On the other hand, HTTP/2 is able to send multiple files at once via multiplexing over one single connection.
HTTP/2 is able to send multiple files at once via multiplexing over one single connection, which helps websites load faster.
- Header optimization : Every single HTTP request contains header information. With HTTP/1.1, many of those headers were repeated in a single session. HTTP/2 eliminates unnecessary headers and the remaining headers are compressed (and transmitted in binary format instead of plain text).
- Server Push: With HTTP/1.1, the server needed to wait until the client initiated a connection. With HTTP/2, server resources can be proactively pushed to the client.
Enable HTTP/2 in Nginx :
Requirements
- Must need Nginx version 1.9.5 or greater. You can check your Nginx version by running (nginx -v) command.
- OpenSSL version 1.0.2 or greater. You can check your OpenSSL version by running (OpenSSL version) command.
- SSL/TLS certificate. TLS 1.2 or higher protocol enabled. Otherwise, you will not be able to use HTTP/2. Implementations of HTTP/2 must use TLS version 1.2 or higher for HTTP/2 over TLS.
Enable HTTP/2 in Nginx
To enable HTTP/2 in Nginx, we have to add the http2 parameter to the listen directive in our virtual host:
listen 443 ssl http2;
And reload your Nginx configuration: sudo systemctl reload nginx.service
Here is the minimal virtual server configuration that can be used to enable HTTP/2 in some virtual host:
Enable HTTP/2 in Apache:
Requirements
- Must need Apache 2.4.17 or greater. You can check your Apache version by running (apache2 -v) command.
- OpenSSL version 1.0.2 or greater. You can check your OpenSSL version by running (OpenSSL version) command.
- SSL/TLS certificate. TLS 1.2 or higher protocol enabled. Otherwise, you will not be able to use HTTP/2. Implementations of HTTP/2 must use TLS version 1.2 or higher for HTTP/2 over TLS.
- Disable the mod_php module. We will have to disable the old mod_php mode and replace it with the more modern PHP-FPM mode.
sudo apt-get install php7.X-fpm
sudo a2dismod php7.X
sudo a2enconf php7.X-fpm
sudo a2enmod proxy_fcgi
Enable an Apache MPM that is compatible with HTTP/2:
- First, we disable mpm_prefork module: sudo a2dismod mpm_prefork
- Then we enable the mpm_event module: sudo a2enmod mpm_event
Enable HTTP/2 in Apache
- Enable SSL module : sudo a2enmod ssl
- Enable HTTP/2 module : sudo a2enmod http2
- To activate these new modules, restart apache2 : sudo systemctl restart apache2
To enable HTTP/2 on your Apache web server add one of the following to your global Apache configuration or inside of a particular virtual host : Protocols h2 http/1.1
To check if your server supports HTTP/2, you can use your browser dev tools or Nginx log files. The below is a screenshot from Google Chrome browser that shows HTTP/2 in action on https://example.com domain.
And that's all there is to enabling HTTP/2 web servers