How to Enable HTTP/2 in Apache & Nginx for Improved Web Performance

HTTP/2 is the second major version of the HTTP network protocol, designed to enhance website speed, improve performance, and provide a better user experience. HTTP/2 can reduce page load times through multiplexing, header compression, and better resource prioritization. Enabling HTTP/2 on your Apache or Nginx server is a straightforward way to upgrade your website’s performance. Here’s a step-by-step guide to enable HTTP/2 on both Apache and Nginx servers.  
 


 

Benefits of HTTP/2

  • Faster Load Times: HTTP/2 supports multiplexing, allowing multiple requests over a single connection, which significantly reduces latency.
  • Improved Security: HTTP/2 requires HTTPS, offering a secure connection by default.
  • Better Compression: With header compression, HTTP/2 reduces data redundancy and lowers page load times.
  • Resource Prioritization: HTTP/2 allows the server to prioritize resources based on importance, ensuring critical assets load first.  
     

 

Prerequisites

  • SSL Certificate: HTTP/2 requires HTTPS, so an SSL certificate must be installed and configured.
  • Updated Web Server: Ensure you have an updated version of Apache or Nginx that supports HTTP/2.  
     

 

Enabling HTTP/2 in Apache

Update Apache: Ensure Apache is version 2.4.17 or newer. You can check your version by running:  

bash

apache2 -v  
 

Enable HTTP/2 Module: Run the following command to enable the HTTP/2 module:  

bash

sudo a2enmod http2  
 

Configure Virtual Hosts: Edit your virtual host configuration file to enable HTTP/2. Open the configuration file for your site:  

bash

sudo nano /etc/apache2/sites-available/your-site.conf  
 

In the virtual host file, add the following line to enable HTTP/2:  

bash

Protocols h2 http/1.1  
 

Restart Apache: Save and close the file, then restart Apache to apply changes:  

bash

sudo systemctl restart apache2  
 

Verify HTTP/2 Activation: Use an online tool like KeyCDN's HTTP/2 Test to confirm that HTTP/2 is active on your site.  
 


 

Enabling HTTP/2 in Nginx

Update Nginx: Ensure you have Nginx version 1.9.5 or later. Check your version by running:  

bash

nginx -v  
 

Edit Configuration File: Open your Nginx configuration file:  

bash

sudo nano /etc/nginx/sites-available/your-site.conf  
 

Update your server block to include HTTP/2 in the listen directive:  

bash

listen 443 ssl http2;  
 

Enable SSL Certificate: Ensure your SSL configuration is correct. Include the following lines for SSL:  

bash

ssl_certificate /path/to/your/certificate.crt; ssl_certificate_key /path/to/your/private.key;  
 

Restart Nginx: Save the changes and restart Nginx:  

bash

sudo systemctl restart nginx  
 

Verify HTTP/2 Activation: Use an online tool like KeyCDN's HTTP/2 Test to confirm that HTTP/2 is active.  
 


 

Conclusion

Enabling HTTP/2 on Apache or Nginx servers can significantly improve site speed, enhance security, and provide a better user experience. With this protocol, your website can handle multiple requests efficiently and load faster, ultimately benefiting your visitors.

 

How to Fix

HTTP/2 can be deployed and enabled in Apache with the help of mod_http2 module. 

Learn more about specific steps to take in Apache to enable HTTP/2.

loading...