keepcalculator

06 Numbers and Powers

Enable HTTP/2 in Nginx: Step-by-Step Guide

Card No. 06·86095 · Updated · Standard formula, shown below

To enable HTTP/2 in Nginx, you add the http2 directive to your server block’s listen line and reload Nginx. This works for HTTPS connections (the most common case) and requires Nginx 1.9.5 or newer. Below is a complete guide covering configuration, verification, common pitfalls, and performance considerations.

What HTTP/2 Actually Changes

HTTP/2 is a major revision of the HTTP protocol that retains the same request/response model but changes how data is transported. Instead of opening multiple TCP connections to load a page’s resources in parallel, HTTP/2 multiplexes many requests and responses over a single connection. This reduces latency and improves page load times, especially on high-latency networks. It also introduces header compression (HPACK), server push (though rarely used now), and binary framing, which makes parsing more efficient.

For Nginx, enabling HTTP/2 is straightforward because the implementation is built into the open-source version. You do not need to compile a separate module; the ngx_http_v2_module is included by default in most distribution packages since Nginx 1.9.5.

Prerequisites: What You Need Before You Start

  • Nginx version 1.9.5 or later. Check with nginx -v.
  • An SSL/TLS certificate. HTTP/2 in Nginx is only supported over HTTPS (via ALPN). There is no h2c (HTTP/2 cleartext) support in the official Nginx package.
  • Access to your Nginx configuration files (usually in /etc/nginx/ or /usr/local/nginx/conf/).
  • Root or sudo privileges to edit the configuration and reload Nginx.

If you are using a reverse proxy setup (e.g., Nginx in front of a Node.js or PHP-FPM backend), HTTP/2 works at the edge; the backend can still use HTTP/1.1.

Step-by-Step Configuration

Here is how to enable HTTP/2 on a typical HTTPS server block. The key change is adding http2 to the listen directive.

  1. Open your site’s configuration file. For example, sudo nano /etc/nginx/sites-available/example.com.
  2. Locate the server block that handles HTTPS. It will have a listen 443 ssl; directive.
  3. Change that line to listen 443 ssl http2;. If you have an IPv6 listener, change listen [::]:443 ssl; to listen [::]:443 ssl http2; as well.
  4. Save the file and test the configuration with sudo nginx -t. You should see syntax is ok and test is successful.
  5. Reload Nginx to apply the change: sudo systemctl reload nginx (or sudo nginx -s reload).

That’s it. The same http2 parameter works on any listen line that includes ssl. For example, listen 8443 ssl http2; for a custom port.

Verifying That HTTP/2 Is Active

After reloading, you can verify HTTP/2 is being served in several ways:

  • Use your browser’s developer tools (F12), go to the Network tab, reload the page, and look at the Protocol column. It should show h2 instead of http/1.1.
  • Use curl with the --http2 flag: curl -I --http2 https://example.com. Look for HTTP/2 200 in the response headers.
  • Use an online HTTP/2 checker (e.g., KeyCDN’s HTTP/2 test) by entering your domain. It will report whether HTTP/2 is supported.

If you see HTTP/1.1 in the response, HTTP/2 is not enabled. Double-check that you added http2 to the correct listen line and that you reloaded Nginx.

Common Problems and How to Fix Them

Despite the simplicity, a few issues can arise:

  • HTTP/2 not working after config change: Ensure you reloaded Nginx, not just restarted it. Sometimes a full restart is needed if the config syntax is cached. Also, check that the http2 parameter is on the listen line, not elsewhere.
  • Nginx fails to start with “unknown directive http2”: This means your Nginx version is older than 1.9.5, or the module is not compiled in. Update Nginx or recompile with --with-http_v2_module.
  • Browser still uses HTTP/1.1: HTTP/2 requires ALPN (Application-Layer Protocol Negotiation). Ensure your SSL configuration uses a modern TLS version (TLSv1.2 or higher) and that your cipher suite supports ALPN. If you have a very old OpenSSL version, ALPN may not be available.
  • Mixed content warnings: HTTP/2 does not cause this, but if you load resources over HTTP, they will be blocked. This is unrelated to enabling HTTP/2.
  • Performance degradation: If you see slower performance, it could be due to too many simultaneous connections being multiplexed, or a misconfigured worker_connections. Usually, HTTP/2 improves performance, but if you have a very low-end server, the extra CPU for header compression might be noticeable.

HTTP/2 vs. HTTP/3: Should You Skip Ahead?

HTTP/3 is the next major version, based on QUIC (a UDP-based transport). It offers even lower latency and better performance on lossy networks. However, HTTP/3 is not yet supported in the stable Nginx branch (as of Nginx 1.24, it is available in the njs and experimental builds). If you want HTTP/3, you need to use a third-party module or a different server like Caddy. For most sites, HTTP/2 is sufficient and widely supported. Enabling HTTP/2 does not prevent you from adding HTTP/3 later; they can coexist on the same port.

Performance Considerations and Best Practices

HTTP/2 shines when a page has many small resources (CSS, JS, images). It eliminates the need for domain sharding (using multiple subdomains to bypass the old 6-connections-per-host limit). However, there are a few things to keep in mind:

  • Server push is deprecated: Most browsers have disabled HTTP/2 server push because it can waste bandwidth. Do not rely on it.
  • Connection coalescing: With HTTP/2, browsers can reuse a single connection for multiple domains if they share the same IP and certificate (e.g., using a wildcard cert). This can reduce latency further.
  • Keepalive settings: HTTP/2 uses a single long-lived connection. Set keepalive_timeout to a reasonable value (e.g., 60 seconds) to avoid closing connections too early.
  • Check your worker_connections: Since each client uses one connection instead of many, you may be able to handle more concurrent users with the same worker settings.

Also, note that HTTP/2 requires TLS, so ensure your SSL certificate is valid and your SSL configuration is optimized (e.g., using ssl_protocols TLSv1.2 TLSv1.3;).

Frequently Asked Questions

Does HTTP/2 work without SSL?

In Nginx, HTTP/2 is only supported over TLS (via ALPN). The official Nginx does not support h2c (cleartext HTTP/2). If you need HTTP/2 without HTTPS, you would have to use a different server or a patch, but it is not recommended because most browsers only support HTTP/2 over HTTPS.

Can I enable HTTP/2 on port 80?

Technically, you can add http2 to a listen 80; line, but browsers will not use it because they only negotiate HTTP/2 over TLS. It is pointless and may cause errors. Keep HTTP/2 on 443 only.

How do I know if my Nginx supports HTTP/2?

Run nginx -V 2>& | grep http_v2. If you see --with-http_v2_module, it is compiled. If not, you need to install a newer version or compile with that module.

What is the difference between http2 and http2 on; in Nginx?

In older Nginx versions (before 1.25.1), you used the http2 parameter on the listen line. In newer versions, you can also use the http2 on; directive inside the server block. Both work, but the listen parameter is the classic method. If you have a very new Nginx, you can use either, but avoid mixing them.

Will HTTP/2 break my existing Nginx config?

No, enabling HTTP/2 is additive. It does not change the behavior of existing directives. However, if you have a very old Nginx version, the http2 parameter might not be recognized, so update first.

How do I disable HTTP/2 if I change my mind?

Simply remove the http2 parameter from the listen line, test the config, and reload. The server will revert to HTTP/1.1.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →