Skip to content

Enabling HTTP/2 on Nginx — the one-line change and what it actually does

How to enable HTTP/2 (and HTTP/3 with QUIC) on Nginx, what changes in the wire protocol, and the practical performance behaviour to expect on a real-world site.

HTTP/2 has been a one-line change in Nginx for years — http2 on the listen directive and the server speaks both 1.1 and 2 over the same TLS port. The interesting part is what changes underneath: header compression, request multiplexing on a single TCP connection, and server-push (deprecated, but still in some configs). For a real-world site, expect ~5–20% page-load improvement and a measurable drop in connection count.

How to verify

Check what the server is speaking:

sudo nginx -V 2>&1 | tr ' ' '\n' | grep http_v2
curl --http2 -sI https://example.com/ | head -1
openssl s_client -alpn h2,http/1.1 -connect example.com:443 -servername example.com </dev/null \
  2>/dev/null | grep -E 'ALPN|Cipher'
nghttp -nv https://example.com/ 2>&1 | head

The curl output should show HTTP/2 200. The openssl ALPN line shows ALPN protocol: h2. nghttp (from nghttp2-client) gives a fuller dump including the frame log.

What’s happening

HTTP/2 is a binary wire protocol over TLS that solves three pain points of HTTP/1.1: it multiplexes many concurrent requests on a single TCP connection (no more six-connections-per-origin limit), it compresses headers with HPACK (good for cookie-heavy sites), and it lets the server prioritise streams. The protocol is negotiated via TLS ALPN — the client and server agree on h2 if both support it, falling back to http/1.1 otherwise.

Performance is workload-dependent. A site with many small assets benefits most from multiplexing. A site that already uses HTTP/1.1 keepalive and bundles assets sees a smaller win. Importantly, server-push (the spec lets the server send assets before they are requested) was removed from Chromium in 2022; the directive still exists in Nginx but does nothing useful.

HTTP/3 is the next protocol — same semantics as HTTP/2 but over QUIC (UDP) rather than TCP. Nginx mainline added QUIC/HTTP/3 in 1.25.x; on Ubuntu it requires the upstream mainline package and the udp port (443/UDP) open.

The procedure

  1. HTTP/2 on the listener:
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name example.com;
        # ssl_certificate ...
        # ...
    }
    http2 is positional on the listen line. On Nginx 1.25.1+, the http2 directive can also appear standalone inside the server {} block — more flexible but the listen-line form is fine.
  2. Reload and confirm with curl --http2 -sI. The first response line should be HTTP/2 200, not HTTP/1.1 200.
  3. (Optional) Disable HTTP/1.1 entirely. Almost no one does this — old crawlers and old Android still need it. The default of negotiating per-client via ALPN is correct.
  4. Tune the HTTP/2-specific knobs:
    http2_max_concurrent_streams 128;
    http2_max_field_size         4k;
    http2_max_header_size        16k;
    http2_idle_timeout           3m;
    These are the defaults plus or minus. Streams of 128 is plenty; 16 (the default in older versions) was the bottleneck on header-heavy sites.
  5. HTTP/3 (only if you really want it). Install nginx-mainline, open UDP/443 on the firewall, then:
    listen 443 quic reuseport;
    listen 443 ssl http2;
    add_header Alt-Svc 'h3=":443"; ma=86400';
    The Alt-Svc header tells HTTP/2 clients “you can also reach me over HTTP/3 on UDP 443” — first visit is HTTP/2, subsequent are HTTP/3.
  6. Confirm both — curl --http3 -sI https://example.com/ (needs a curl built with QUIC support; check curl --version | grep HTTP3).
  7. Watch the access log for protocol (if present in your log_format). Mix of h2 and http/1.1 is expected; mostly h2 from modern browsers, mostly http/1.1 from bots and old clients.

Common pitfalls

  • Enabling HTTP/2 without HTTPS. The spec allows h2c (cleartext), but no browser implements it — your http2 directive on a port-80 server is dead code.
  • Putting http2 on the listener of an HTTPS terminator that proxies to an upstream over HTTP/1.1. Fine — h2 only applies between browser and Nginx; the proxy hop can be 1.1.
  • Worker count tuned for an HTTP/1.1 connection model. With h2 multiplexing, you usually need fewer worker connections but each connection lives longer.
  • The “h2 server push” directives (http2_push, http2_push_preload) still appear in older Nginx configs. They do nothing in modern browsers. Remove to avoid future maintainer confusion.
  • Old SSL libraries: HTTP/2 requires ALPN support in OpenSSL, which means OpenSSL 1.0.2+. Anything older than ~2016 will not negotiate h2.

In the engagements we run, HTTP/2 is enabled by default in our Nginx template; HTTP/3 is opt-in per site (we use it where the user base is geographically spread and the latency/UDP-loss curve matters). Stack Harbor tracks the protocol distribution in managed operations dashboards alongside the rest of the request mix.