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.
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.
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;
}
}
server {
listen 443 ssl http2;
server_name site1.com;
...
}
server {
listen 443 ssl http2;
server_name site2.com;
...
}
For more details and advanced configurations, visit the official Nginx HTTP/2 module documentation.
Jorge García
Fullstack developer