0
0
Nginxdevops~5 mins

HTTP/2 configuration in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
HTTP/2 is a newer version of the web communication protocol that makes websites load faster and more efficiently. Configuring HTTP/2 in nginx helps your server send data quicker and handle multiple requests smoothly.
When you want your website to load faster by allowing multiple files to be sent at once.
When you want to improve the performance of your secure website using HTTPS.
When you want to reduce the delay caused by older HTTP versions during data transfer.
When you want to support modern browsers that prefer HTTP/2 for better speed.
When you want to optimize resource usage on your web server for many users.
Config File - nginx.conf
nginx.conf
server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

This configuration enables HTTP/2 by adding http2 to the listen directive on port 443, which is the standard port for HTTPS.

The ssl_certificate and ssl_certificate_key lines specify the SSL/TLS certificates needed for secure connections, which HTTP/2 requires.

The location block defines where the website files are served from.

Commands
This command tests the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command checks if the server responds using HTTP/2 by requesting headers with curl using HTTP/2 protocol.
Terminal
curl -I --http2 https://example.com
Expected OutputExpected
HTTP/2 200 server: nginx content-type: text/html ...
-I - Fetch only HTTP headers
--http2 - Use HTTP/2 protocol for the request
Key Concept

If you remember nothing else from this pattern, remember: adding 'http2' to the listen directive on port 443 enables HTTP/2 in nginx.

Common Mistakes
Forgetting to include 'http2' in the listen directive.
Without 'http2', nginx will use HTTP/1.1 even on HTTPS, missing the performance benefits.
Always add 'http2' after the port number in the listen directive for HTTPS servers.
Trying to enable HTTP/2 on port 80 without SSL certificates.
HTTP/2 requires HTTPS in nginx, so it won't work on plain HTTP port 80.
Enable HTTP/2 only on port 443 with valid SSL certificates.
Not testing nginx configuration before reloading.
Syntax errors can cause nginx to fail to reload, leading to downtime.
Always run 'sudo nginx -t' to check configuration before reloading.
Summary
Add 'http2' to the listen directive on port 443 in nginx.conf to enable HTTP/2.
Test the nginx configuration with 'sudo nginx -t' before applying changes.
Reload nginx with 'sudo systemctl reload nginx' to apply the new HTTP/2 settings.
Verify HTTP/2 is active by using 'curl -I --http2 https://example.com'.