Back to Homepage
Tuesday 17 September 2024
74

How to configure http 2 in Nginx

HTTP/2 is an upgrade to the HTTP protocol that significantly enhances webpage loading speed, providing more efficient connections, reduced latency, and better request handling. Configuring it in Nginx is straightforward, as long as your server and clients support this protocol.

Steps to Configure HTTP/2 in Nginx

1. Ensure Nginx supports HTTP/2:

Make sure you're using Nginx version 1.9.5 or higher, as it's the first to support HTTP/2.

2. Edit your Nginx configuration file:

Open the server configuration file, typically located at /etc/nginx/nginx.conf or in site-specific files like /etc/nginx/sites-available/default.

Modify the server block that uses SSL to include the HTTP/2 protocol:

server {
       listen 443 ssl http2;
       server_name www.yourdomain.com;
       ssl_certificate /path/to/certificate.crt;
       ssl_certificate_key /path/to/key.key;
       ...
   }

3. Reload Nginx:

After making the changes, reload Nginx to apply the new configuration:

sudo systemctl reload nginx

4. Verify the configuration:

To confirm HTTP/2 is enabled, use tools like https://tools.keycdn.com/http2-test or check the network tab in your browser's developer tools.

HTTP/2 Configuration Examples in Nginx

  • Basic HTTPS server with HTTP/2 enabled:
server {
       listen 443 ssl http2;
       server_name example.com;
       ssl_certificate /etc/nginx/ssl/example.com.crt;
       ssl_certificate_key /etc/nginx/ssl/example.com.key;
       location / {
           root /var/www/html;
           index index.html;
       }
   }
  • Multiple sites using HTTP/2:
server {
       listen 443 ssl http2;
       server_name site1.com;
       ...
   }
   server {
       listen 443 ssl http2;
       server_name site2.com;
       ...
   }

Official Reference

For more details and advanced configurations, visit the official Nginx HTTP/2 module documentation.

Hashtags:
nginx
Share:
Created by:
Author photo

Jorge García

Fullstack developer