Nginx HTTP/2 Setup Guide (Current Config)
This guide walks you through enabling HTTP/2 on Nginx, which speeds up page loading by multiplexing multiple requests over a single connection and compressing headers. You’ll edit your Nginx server block to add the HTTP/2 protocol, then verify it works. Before starting, ensure you have Nginx installed (version 1.25.1 or later is recommended for full HTTP/2 support) and that you have a domain name with an SSL/TLS certificate, as HTTP/2 is required to be served over HTTPS in modern browsers.
What You’ll Need
- A Linux server (Ubuntu/Debian, CentOS, etc.) with Nginx installed and running.
- Root or sudo access to edit Nginx configuration files.
- A domain name pointing to your server’s IP address.
- An SSL/TLS certificate for that domain (can be from Let’s Encrypt, a commercial CA, or self-signed for testing).
- Basic familiarity with the command line and Nginx configuration syntax.
Step-by-Step
- Check your Nginx version – Run
nginx -vin your terminal. If it’s older than 1.9.5, you need to upgrade because HTTP/2 support was introduced in that version. For the best experience, use 1.25.1 or newer, which includes the improved HTTP/2 implementation. - Back up your current configuration – Before editing, make a backup of your Nginx config files. For example:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakand similarly for any server block files. - Locate your server block – Find the configuration file for your site, often in
/etc/nginx/sites-available/or/etc/nginx/conf.d/. Open it with a text editor, e.g.,sudo nano /etc/nginx/sites-available/example.com. - Ensure SSL/TLS is configured – HTTP/2 requires HTTPS. Inside your server block, you should have lines like
listen 443 ssl;andssl_certificate/ssl_certificate_keypointing to your certificate files. If not, set up SSL first (e.g., using Certbot). - Add the HTTP/2 directive – Change the listen line from
listen 443 ssl;tolisten 443 ssl http2;. If you have multiple listen lines (e.g., for IPv6), addhttp2to each. For example:listen [::]:443 ssl http2;. - Optional: Adjust server_tokens – To hide the Nginx version (not required for HTTP/2, but good security practice), set
server_tokens off;in the http block or server block. - Test your configuration – Run
sudo nginx -tto check for syntax errors. If you see “syntax is ok” and “test is successful”, proceed. - Reload Nginx – Apply the changes with
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). This does not drop connections. - Verify HTTP/2 is active – Use your browser’s developer tools (Network tab) and look for “h2” in the Protocol column. Alternatively, run
curl -I --http2 https://yourdomain.comand check forHTTP/2 200in the response.
Common Mistakes
- Forgetting to add http2 to all listen directives – If you have both IPv4 and IPv6 listen lines, you must add
http2to each, otherwise IPv6 connections may fall back to HTTP/1.1. - Using HTTP/2 without SSL – While Nginx technically allows
listen 80 http2;, browsers only support HTTP/2 over HTTPS. Always use it with SSL. - Outdated Nginx version – If you’re on an old version, the
http2parameter may not be recognized or may behave differently. Upgrade to a current stable release. - Not reloading after config changes – A common mistake is editing the config but not reloading Nginx, so changes don’t take effect. Always run
nginx -tand then reload. - Conflicting configuration files – If you have multiple server blocks for the same domain, the wrong one might be used. Check with
nginx -Tto see the full effective config.
Tips
- Use a modern SSL configuration – Enable HTTP/2 with TLS 1.3 if possible. Add
ssl_protocols TLSv1.2 TLSv1.3;andssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';for better performance and security. - Enable gzip or brotli compression – HTTP/2 works well with compression. Add
gzip on;andgzip_types text/plain text/css application/json application/javascript;to reduce transfer sizes. - Leverage server push (if needed) – While not widely used now, you can use
http2_pushto preload critical resources. However, consider usingpreloadlinks instead for better browser compatibility. - Monitor with tools – Use
nghttpdor online HTTP/2 testers to confirm your server is speaking HTTP/2 correctly. - Keep Nginx updated – Performance and security improvements are constantly added. Regularly update your Nginx package.
Troubleshooting
- After reload, the site fails to load – Check the Nginx error log:
sudo tail -f /var/log/nginx/error.log. Common issues include missing SSL certificates or a syntax error. Runsudo nginx -tagain to double-check. - HTTP/2 is not being used – Verify that your browser supports HTTP/2 (all modern ones do). Also ensure you are accessing via
https://nothttp://. Clear your browser cache, as some browsers may cache the protocol. - curl shows HTTP/1.1 – If you run
curl -I https://yourdomain.comand seeHTTP/1.1 200, it might be because curl is not using HTTP/2 by default. Usecurl -I --http2 https://yourdomain.comto force it. If that still shows HTTP/1.1, your Nginx config may not have thehttp2parameter applied correctly. - Error: “http2” directive is not allowed here – This usually means your Nginx version is too old. Upgrade Nginx (on Ubuntu:
sudo add-apt-repository ppa:ondrej/nginxthensudo apt update && sudo apt upgrade nginx). - SSL handshake fails – Ensure your certificate files are present and readable by Nginx. Check permissions:
sudo ls -l /etc/letsencrypt/live/yourdomain/and make sure the nginx user (often www-data) can read them.
Frequently Asked Questions
Do I need a specific Nginx version for HTTP/2?
Yes, HTTP/2 support was introduced in Nginx 1.9.5. However, for the most stable and performant experience, use a version from the 1.25.x series or newer, which includes the new HTTP/2 implementation that is more secure and efficient.
Can I enable HTTP/2 on port 80 (HTTP)?
Technically you can add http2 to a port 80 listen directive, but all major browsers only support HTTP/2 over TLS (HTTPS). So for practical purposes, HTTP/2 must be used with SSL.
Does HTTP/2 work with WebSockets?
Yes, HTTP/2 supports WebSockets (RFC 8441), but Nginx’s implementation may require additional configuration. In practice, many sites still use HTTP/1.1 for WebSockets because the overhead is minimal. If you need WebSockets over HTTP/2, you may need to use a reverse proxy like nginx with the http2 flag and ensure your application supports it.
Will HTTP/2 slow down my server?
No, HTTP/2 is generally faster than HTTP/1.1 because it reduces latency through multiplexing and header compression. However, it can increase CPU usage slightly due to header compression, but the performance gain usually outweighs this.
How do I disable HTTP/2 if I need to?
Simply remove the http2 parameter from your listen directives and reload Nginx. For example, change listen 443 ssl http2; back to listen 443 ssl;.
Can I use HTTP/2 with a CDN?
Yes, many CDNs support HTTP/2 to your origin server. You just need to ensure your origin Nginx is configured with HTTP/2 and the CDN is set to use it. This can improve performance for your users.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.