HTTP/2, HTTP/3 & QUIC
HTTP/2 and HTTP/3 explains the protocols: HTTP/2 multiplexes many requests over one TCP connection with binary framing and header compression; HTTP/3 does the same over QUIC, a transport on UDP with TLS 1.3 built in, which avoids TCP's head-of-line blocking and survives a client changing networks. This lesson is about switching them on, and about which version of which server supports what, because the directives changed recently.
Who supports what
| Server (version) | HTTP/2 | HTTP/3 |
|---|---|---|
| Nginx 1.24 (Ubuntu 24.04, this sandbox) | listen 8443 ssl http2; |
not built in (no ngx_http_v3_module) |
| Nginx 1.25.0+ | from 1.25.1: http2 on; (the http2 parameter of listen is deprecated) |
listen 443 quic reuseport; (experimental in 1.25, needs an OpenSSL with QUIC support or the compatibility layer) |
| HAProxy 2.8 (this sandbox) | bind :443 ssl crt … alpn h2,http/1.1 |
bind quic4@:443 ssl crt … alpn h3 (Ubuntu's build has USE_QUIC with the OpenSSL compatibility layer) |
| Caddy 2.11 | on by default with TLS | on by default with TLS |
| Traefik 3.7 | on by default with TLS | http3: {} on an entry point |
Browsers only use HTTP/2 and HTTP/3 over TLS. HTTP/2 without TLS (h2c) exists for proxy-to-backend hops, gRPC in particular.
HTTP/2 in Nginx 1.24
openssl req -x509 -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout ca.key -out ca.crt -days 365 -subj "/CN=TAD Lab Root CA" 2>/dev/null
openssl req -new -noenc -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout app.key -out app.csr -subj "/CN=app.example.test" 2>/dev/null
openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -set_serial 1 -days 90 -out app.crt -extfile <(printf 'subjectAltName=DNS:app.example.test\n') 2>/dev/null
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
access_log off;
server {
listen 127.0.0.1:8443 ssl;
http2 on; # the Nginx 1.25.1+ syntax
ssl_certificate app.crt; ssl_certificate_key app.key;
return 200 "proto=$server_protocol\n";
}
}
EOF
nginx -t -p "$PWD" -c nginx.conf -e stderr 2>&1 | grep emerg | sed 's/.*#[0-9]*: //'
sed -i -e '/http2 on;/d' -e 's/listen 127.0.0.1:8443 ssl;/listen 127.0.0.1:8443 ssl http2; # the 1.24 syntax/' nginx.conf
nginx -t -p "$PWD" -c nginx.conf -e stderr 2>&1 | tail -1
nginx -p "$PWD" -c nginx.conf -e stderr
C="curl -s --resolve app.example.test:8443:127.0.0.1 --cacert ca.crt https://app.example.test:8443/"
$C -w 'curl used HTTP/%{http_version}\n'
$C --http1.1 -w 'curl used HTTP/%{http_version}\n'
openssl s_client -connect 127.0.0.1:8443 -alpn h2,http/1.1 -CAfile ca.crt < /dev/null 2>/dev/null | grep ALPN
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/nullunknown directive "http2" in /home/learner/nginx.conf:8
nginx: configuration file /home/learner/nginx.conf test is successful
proto=HTTP/2.0
curl used HTTP/2
proto=HTTP/1.1
curl used HTTP/1.1
ALPN protocol: h2
The client and server agree on HTTP/2 during the TLS handshake through ALPN (Application-Layer Protocol Negotiation): the client offers h2,http/1.1, the server picks. $server_protocol shows what Nginx actually spoke. When you upgrade to Nginx 1.25.1 or later, move to http2 on; (the old form still works with a deprecation warning). Note that the http2 parameter in a listen line enables HTTP/2 for every server block on that address and port, which surprises people who add it to one site only.
HTTP/2 needs little tuning: http2_max_concurrent_streams (default 128) limits parallel requests per connection. HTTP/2 server push was removed from Nginx in 1.25.1 and from Chrome before that; 103 Early Hints (Nginx 1.29.0+ early_hints) replaced it.
HTTP/3: what it takes
HTTP/3 runs over UDP on the same port number as HTTPS. A browser always starts with HTTP/1.1 or HTTP/2 over TCP, sees an Alt-Svc: h3=":443" header in the response, and tries QUIC for the next requests. Practical consequences:
- The firewall must allow UDP 443 as well as TCP 443; many corporate networks block UDP, and browsers then silently stay on HTTP/2.
- Cloud load balancers must forward UDP (or terminate QUIC themselves).
reuseportmatters in Nginx so the kernel spreads QUIC packets over workers; connection migration across workers needsquic_bpf on;on Linux.
Caddy shows the mechanism with no configuration at all. tls internal makes Caddy issue a certificate from its own local CA (the next section covers it), and the response advertises HTTP/3:
cat > Caddyfile <<'EOF'
{
skip_install_trust
http_port 8080
https_port 8443
}
app.example.test:8443 {
tls internal
respond "hello from caddy"
}
EOF
caddy start --config Caddyfile > caddy.log 2>&1 && echo "caddy started"
for i in $(seq 50); do curl -sk https://127.0.0.1:8443/ -o /dev/null && break; sleep 0.1; done
ROOT=.local/share/caddy/pki/authorities/local/root.crt
curl -s -D - -o /dev/null --resolve app.example.test:8443:127.0.0.1 --cacert $ROOT https://app.example.test:8443/ | grep -iE '^(HTTP|alt-svc)' | tr -d '\r'
echo "listening:"; { ss -tlnH | awk '{print " tcp " $4}'; ss -ulnH | awk '{print " udp " $4}'; } | sort
curl --http3 -s https://127.0.0.1:8443/ 2>&1 | head -1
caddy stop > /dev/null 2>&1caddy started
HTTP/2 200
alt-svc: h3=":8443"; ma=2592000
listening:
tcp *:8080
tcp *:8443
tcp 127.0.0.1:2019
udp *:8443
curl: option --http3: the installed libcurl version doesn't support this
Caddy listens on TCP and UDP 8443 (plus 8080 for HTTP→HTTPS redirects and its admin API on 2019) and announces h3 in Alt-Svc (with ma, the number of seconds the client may remember it). The sandbox's curl 8.5 is built without HTTP/3 support, so the last line is its refusal; a curl built with HTTP/3 (or a browser's network panel, "Protocol" column h3) would show the switch. Sample session from a machine with an HTTP/3-capable curl:
# Sample session (curl 8.x built with ngtcp2/nghttp3 or OpenSSL QUIC)
$ curl -sI --http3-only https://www.example.com/ | head -1
HTTP/3 200
$ curl -s -o /dev/null -w '%{http_version}\n' --http3 https://www.example.com/
3HTTP/3 in Nginx 1.25+ (sample)
# Sample: Nginx 1.25.0+ built with --with-http_v3_module (e.g. the nginx.org packages)
server {
listen 443 quic reuseport; # UDP; "reuseport" only on one server block per port
listen 443 ssl; # TCP for HTTP/1.1 and HTTP/2
http2 on;
http3 on; # the default once a quic listener exists
ssl_certificate /etc/ssl/app.crt;
ssl_certificate_key /etc/ssl/app.key;
ssl_protocols TLSv1.3; # QUIC requires TLS 1.3 (TCP can keep 1.2 in another block)
add_header Alt-Svc 'h3=":443"; ma=86400';
}On Ubuntu 24.04 that means installing Nginx from nginx.org's own repository instead of Ubuntu's package. Whether HTTP/3 is worth it depends on your users: it helps most on lossy mobile networks and long distances, and does little on a fast LAN.
h2c and gRPC to backends
Between proxy and backend, gRPC needs HTTP/2. Nginx proxies it with grpc_pass (not proxy_pass, which speaks HTTP/1.x to backends):
server {
listen 443 ssl http2; # 1.24 syntax
location /linkly.v1.Links/ {
grpc_pass grpc://127.0.0.1:50051; # h2c to the backend; grpcs:// for TLS
}
}Test yourself
On Ubuntu 24.04's Nginx 1.24, how do you enable HTTP/2 for an HTTPS server?
http2 on;listen 443 ssl http2;http2_enable;- It is always on
How does a browser find out that a site supports HTTP/3?
- DNS A record
- From the
Alt-Svc: h3=":443"response header (or an HTTPS DNS record), then it tries QUIC on UDP - ALPN over TCP
- It always tries UDP first
HTTP/3 is configured, but users always stay on HTTP/2. What should you check first?
- The certificate
- That UDP on port 443 is allowed through firewalls and load balancers
- gzip
- The HTML