HTTP/2
What is HTTP/2?
HTTP/2 is the second major version of the Hypertext Transfer Protocol (HTTP), designed to improve web performance by addressing limitations of HTTP/1.1. Released in 2015, HTTP/2 introduces several key optimizations including multiplexing, header compression, and server push, resulting in faster page loads and more efficient network utilization.
HTTP/2 maintains backward compatibility with HTTP/1.1 while introducing a binary protocol that reduces latency and improves security. It's widely adopted across modern web browsers and servers, forming the foundation of today's high-performance web applications.
Key Features
Multiplexing
HTTP/2 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. This means browsers no longer need to open multiple connections to fetch resources in parallel.
Header Compression (HPACK)
HTTP/2 uses HPACK compression to reduce the size of HTTP headers, which can become quite large with cookies and other metadata. This reduces bandwidth usage and improves performance, especially for header-heavy applications.
Server Push
Servers can proactively push resources to clients before they're explicitly requested. This eliminates round-trip delays for critical resources like CSS and JavaScript files, improving page load times.
Stream Prioritization
Clients can assign priority levels to different streams, allowing critical resources (like CSS and JavaScript) to be delivered first. This improves rendering performance and user experience.
Binary Protocol
HTTP/2 uses a binary protocol instead of HTTP/1.1's text-based format, making it more efficient to parse and less prone to errors.
Security Benefits
HTTP/2 offers several security advantages:
- Encourages HTTPS adoption: Most HTTP/2 implementations require TLS encryption
- Reduced attack surface: Single connection reduces exposure to connection hijacking
- Improved DoS protection: Better resource isolation and flow control
- Header compression security: HPACK is designed to resist compression-based attacks
Performance Improvements
HTTP/2 provides significant performance benefits:
- Faster page loads: Reduced latency through multiplexing and server push
- Better mobile performance: More efficient use of limited bandwidth
- Lower server resource usage: Fewer TCP connections to manage
- Improved user experience: Faster time to first paint and smoother rendering
Implementation Examples
Nginx Configuration:
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
http2_push /styles.css;
http2_push /script.js;
}
Node.js with Express:
const express = require('express');
const spdy = require('spdy');
const fs = require('fs');
const app = express();
spdy.createServer({
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem')
}, app).listen(443);
Browser Support
HTTP/2 is supported in all modern browsers:
- Chrome 41+
- Firefox 36+
- Edge 12+
- Safari 9+
- Opera 28+
Best Practices
- Use HTTPS - HTTP/2 works best with TLS encryption
- Enable server push for critical resources
- Optimize stream prioritization for better rendering
- Monitor performance to identify optimization opportunities
- Combine with other security headers:
Common Issues and Solutions
- Connection coalescing issues:
- Problem: Multiple domains sharing same IP may cause connection issues
- Solution: Use proper SNI configuration and ensure certificates cover all domains
- Server push overuse:
- Problem: Pushing too many resources can waste bandwidth
- Solution: Only push critical resources and monitor effectiveness
- Compatibility problems:
- Problem: Some proxies or firewalls may not support HTTP/2
- Solution: Implement fallback to HTTP/1.1
Related Concepts
- HTTP/3 - The next generation of HTTP based on QUIC
- TLS 1.3 - Modern encryption protocol
- Content Delivery Networks (CDN)
- Web Performance Optimization
- HTTP Security Headers
Heartbleed (CVE-2014-0160)
Heartbleed is a critical security vulnerability in OpenSSL that allowed attackers to read sensitive memory contents, exposing private keys, passwords, and other confidential data.
HTTP Header Injection
HTTP Header Injection is a web security vulnerability that allows attackers to inject malicious headers into HTTP responses, enabling various attacks including XSS, session fixation, and cache poisoning.
