Nginx HTTP/2 Setup and Configuration
Enable HTTP/2 in Nginx for faster page loads through request multiplexing, HPACK header compression, and stream prioritization. Requires TLS setup.
Detailed Explanation
HTTP/2 significantly improves web performance through multiplexing, header compression, and stream prioritization. Enabling it in Nginx is straightforward and provides immediate measurable benefits for most websites.
Enabling HTTP/2
Add the http2 parameter to your listen directive. In practice, HTTP/2 requires SSL/TLS because all major browsers enforce encrypted connections for the protocol:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
}
Key HTTP/2 Benefits
Multiplexing allows multiple requests and responses to be sent simultaneously over a single TCP connection, eliminating the head-of-line blocking problem that plagued HTTP/1.1. Header compression using the HPACK algorithm significantly reduces overhead for repeated headers like cookies, user agents, and authorization tokens that are identical across requests. Stream prioritization enables the browser to signal which resources are most critical, allowing the server to optimize delivery order.
Server Push
HTTP/2 Server Push allows Nginx to proactively send resources to the client before the browser requests them:
location / {
http2_push /styles/main.css;
http2_push /scripts/app.js;
proxy_pass http://backend;
}
However, server push has largely fallen out of favor in modern web development because it can waste bandwidth by pushing resources the browser already has cached locally. Most production deployments now prefer using 103 Early Hints responses or <link rel="preload"> headers for resource prioritization instead.
Tuning Parameters
Adjust HTTP/2-specific settings for optimal performance based on your traffic patterns:
http2_max_concurrent_streams 128;
http2_recv_buffer_size 256k;
http2_chunk_size 8k;
The http2_max_concurrent_streams setting controls how many simultaneous streams each client connection can maintain, with 128 being a reasonable default for most applications.
TLS Configuration Requirements
HTTP/2 mandates TLS 1.2 or higher and requires specific AEAD cipher suites. Ensure your SSL configuration meets these requirements:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
Verifying HTTP/2
Confirm that HTTP/2 is active by checking the response with curl:
curl -I --http2 https://example.com
Look for HTTP/2 200 in the response line. Browser developer tools also display the protocol version in the Network tab for each request.
Backward Compatibility
Nginx automatically negotiates the protocol version with each client via ALPN (Application-Layer Protocol Negotiation). Clients that do not support HTTP/2 fall back to HTTP/1.1 seamlessly without any configuration changes or user impact.
Use Case
You want to improve your website's loading performance by enabling HTTP/2 multiplexing so browsers can load all page resources over a single connection instead of multiple TCP connections.