ThisCalculator

Everyday

Nginx HTTP/2 Setup Guide (Current Config)

Updated · Standard formula, shown below

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

  1. Check your Nginx version – Run nginx -v in 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.
  2. 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.bak and similarly for any server block files.
  3. 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.
  4. Ensure SSL/TLS is configured – HTTP/2 requires HTTPS. Inside your server block, you should have lines like listen 443 ssl; and ssl_certificate / ssl_certificate_key pointing to your certificate files. If not, set up SSL first (e.g., using Certbot).
  5. Add the HTTP/2 directive – Change the listen line from listen 443 ssl; to listen 443 ssl http2;. If you have multiple listen lines (e.g., for IPv6), add http2 to each. For example: listen [::]:443 ssl http2;.
  6. 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.
  7. Test your configuration – Run sudo nginx -t to check for syntax errors. If you see “syntax is ok” and “test is successful”, proceed.
  8. Reload Nginx – Apply the changes with sudo systemctl reload nginx (or sudo service nginx reload on older systems). This does not drop connections.
  9. 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.com and check for HTTP/2 200 in the response.

Common Mistakes

  • Forgetting to add http2 to all listen directives – If you have both IPv4 and IPv6 listen lines, you must add http2 to 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 http2 parameter 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 -t and then reload.
  • Conflicting configuration files – If you have multiple server blocks for the same domain, the wrong one might be used. Check with nginx -T to 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; and ssl_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; and gzip_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_push to preload critical resources. However, consider using preload links instead for better browser compatibility.
  • Monitor with tools – Use nghttpd or 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. Run sudo nginx -t again 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:// not http://. Clear your browser cache, as some browsers may cache the protocol.
  • curl shows HTTP/1.1 – If you run curl -I https://yourdomain.com and see HTTP/1.1 200, it might be because curl is not using HTTP/2 by default. Use curl -I --http2 https://yourdomain.com to force it. If that still shows HTTP/1.1, your Nginx config may not have the http2 parameter 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/nginx then sudo 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.