ALPN: how TLS chooses HTTP/2 or HTTP/1.1

11 min readUpdated

ALPN, Application-Layer Protocol Negotiation, is the TLS extension in which the client lists the application protocols it can speak and the server picks one, such as h2 for HTTP/2 or http/1.1 [6]. The choice is made inside the TLS handshake, so agreeing on a protocol adds no network round trip [1]. ALPN is defined in RFC 7301 for servers where one port, such as 443, carries several application protocols [1]; it is one of the extensions that shape how TLS works on such a port. HTTP/2 over TLS must use it [3], and QUIC, the transport under HTTP/3, requires it unless another mechanism is agreed [5].

Extension
application_layer_protocol_negotiation, type 16 [1] [6]
Client sends
a list of protocol names in the ClientHello, most preferred first [1]
Server answers
with exactly one name: in the ServerHello in TLS 1.2, in EncryptedExtensions in TLS 1.3 [1] [2]
Identifiers
http/1.1, h2 (HTTP/2), h3 (HTTP/3), acme-tls/1 [6]
No match
a fatal no_application_protocol alert, number 120 [1]
Check it
openssl s_client -alpn h2,http/1.1 or curl -v [7] [9]

What ALPN does

Port 443 can carry HTTP/1.1, HTTP/2 and other protocols, so the port number alone does not tell the server which one a client is about to speak. RFC 7301 notes that the protocol used to be known from the TCP or UDP port; ALPN replaces that with an explicit agreement on a protocol identifier inside the hello exchange [1]. The client sends its list in the ClientHello, the server returns its choice, and the application data that follows uses the chosen protocol [1].

The answer belongs to one connection. Unlike many TLS extensions, ALPN does not become a property of the session: when a session is resumed, the earlier answer is ignored and only the values in the new handshake count [1]. Early data in TLS 1.3 does depend on the earlier answer: the server accepts data sent on the first flight only if the ALPN protocol it selects is the same one associated with the pre-shared key [2].

ALPN is the IETF standard and replaces the earlier Next Protocol Negotiation (NPN) extension, as the OpenSSL manual puts it [7]. nginx dropped NPN for establishing HTTP/2 connections in version 1.21.4 [8].

How the negotiation works

  1. The client offers. Its ClientHello carries the ALPN extension with a list of protocols in descending order of preference, for example h2 and then http/1.1; each protocol identifier is an IANA-registered byte string [1]. The same message lists the cipher suites the client accepts.

  2. The server chooses. It ignores names it does not recognise and SHOULD select the protocol highest in its own preference order among those the client offered. Its answer contains exactly one name [1].

  3. Both sides keep to it. The selected protocol is definitive for the connection: the server must not answer with one protocol and then speak another [1].

The server's preference wins over the client's order. On our test machine, an OpenSSL 3.0.13 server set to -alpn h2,http/1.1 picked h2 even when openssl s_client listed http/1.1 first [13]. OpenSSL's helper for this step selects the first protocol in the server's list that the client also offered [7].

Where the server's answer travels

In TLS 1.2 the answer is an extension of the ServerHello, which is not encrypted. RFC 7301 kept it in plaintext on purpose, so network equipment can treat connections differently when the port does not identify the protocol [1]. TLS 1.3 moved it: RFC 9846, the TLS 1.3 specification, places ALPN in the ClientHello and in EncryptedExtensions, and every handshake message after the ServerHello is encrypted [2]. The keys for it come from the key exchange that has just run, and every public-key key exchange in TLS 1.3 provides forward secrecy [2]. The client's list still travels in the ClientHello, before encryption starts [2]. How the two versions differ otherwise is on TLS 1.2 versus TLS 1.3.

You can see this with -msg, which makes openssl s_client print each protocol message [7]. In our runs the bytes 00 10 00 05 00 03 02 68 32, extension 16 carrying one name, h2, arrived inside the ServerHello over TLS 1.2 and inside EncryptedExtensions over TLS 1.3 [13].

When no protocol matches

Over TLS on TCP, a server that supports none of the offered protocols SHALL end the handshake with a fatal no_application_protocol alert, number 120 [1] [2]. A server is not obliged to answer at all: one with no ALPN protocols configured leaves the extension out of its reply, and the handshake completes without a choice [1] [7] [13]. Our test server, OpenSSL 3.0.13 with openssl s_server -www, gave these results to openssl s_client [13]:

When no protocol matches
Server -alpnClient -alpnResult
h2,http/1.1h2,http/1.1ALPN protocol: h2
h2,http/1.1http/1.1,h2ALPN protocol: h2
http/1.1h2,http/1.1ALPN protocol: http/1.1
http/1.1h2SSL alert number 120, no connection
not seth2,http/1.1No ALPN negotiated, the handshake completes

A server with HTTP/2 turned off therefore does not lock out clients that offer both protocols: it picks http/1.1, and the connection goes ahead without HTTP/2 [13]. The handshake fails when the client offers nothing the server has, such as a client that insists on h2. From version 1.21.4, nginx also rejects a TLS connection when the client uses ALPN and no supported protocol can be negotiated [8].

Over QUIC

QUIC carries the TLS handshake in its own packets and is stricter about ALPN. RFC 9001 makes ALPN mandatory for QUIC unless another mechanism is agreed, and both endpoints MUST close the connection with no_application_protocol, QUIC error 0x0178, when no protocol is negotiated. RFC 7301 only asks this of servers. A QUIC server may also need to read the whole ClientHello, with its SNI and ALPN extensions, before it decides whether to accept the connection [5].

ALPN and HTTP/2, HTTP/3

HTTP/2

RFC 9113 identifies HTTP/2 over TLS as h2, serialized into an ALPN protocol identifier as the two bytes 0x68 0x32 [3]. A client that wants HTTP/2 for an https URI uses TLS with ALPN and offers h2 in its ClientHello, and HTTP/2 over TLS MUST use ALPN; starting HTTP/2 with prior knowledge, without negotiation, applies only to cleartext TCP [3]. The h2c identifier names HTTP/2 without TLS and must not be sent or selected in ALPN [3]. RFC 9113 also treats a completed handshake with the HTTP/2 identifier as sufficient protection against cross-protocol attacks [3]. On the server side, nginx needs ALPN support in the TLS library to accept HTTP/2 over TLS, available since OpenSSL 1.0.2, and turns HTTP/2 on with http2 on, a directive added in 1.25.1 whose default is off [8]; see installing on nginx.

HTTP/3

HTTP/3 runs over QUIC version 1, which uses TLS 1.3 or later as its handshake and carries the handshake data in its own CRYPTO frames [4] [5]. Support for HTTP/3 is indicated by selecting h3 in that handshake [4]. A client learns about it in advance from an Alt-Svc response header such as Alt-Svc: h3=":50781" [4], or from the alpn parameter of an HTTPS record in DNS (RFC 9460) [11]. If the QUIC connection fails, for example because UDP is blocked, RFC 9114 says clients SHOULD try a TCP-based version of HTTP [4].

ALPN and certificates

A server can pick its certificate by SNI, the hostname in the same ClientHello (RFC 6066) [14]. RFC 7301 adds a second option: a server may associate a different certificate with each application protocol chosen through ALPN [1]. ACME relies on it. In the TLS-ALPN-01 method of RFC 8737, the ACME server connects to TCP port 443 offering only the protocol identifier acme-tls/1, with the domain name in SNI; RFC 8737 introduces this method to validate domain control at the TLS layer alone, without changing how backends handle HTTP [10]. The site answers with a self-signed validation certificate that carries a critical acmeIdentifier extension; that certificate is set up to be returned only in a TLS handshake where acme-tls/1 has been negotiated and SNI carries the domain being validated [10]. The acme-tls/1 protocol carries no application data: once the handshake is complete, the ACME server closes the connection without sending anything more [10]. How the validation methods compare is on ACME.

How to check ALPN

Offer protocols with OpenSSL and read which one the server picked [7]:

$ openssl s_client -connect example.com:443 -servername example.com -alpn h2,http/1.1 </dev/null 2>&1 | grep ALPN
ALPN protocol: h2

In the example, example.com stands for your host, and the output line is from our test server. In our runs the line read ALPN protocol: http/1.1 when the server chose HTTP/1.1. No ALPN negotiated appeared whenever no protocol was agreed: after a server that sent no ALPN answer, and also after the alert 120 that ended the handshake [13].

curl enables ALPN by default when its TLS library supports it and negotiates HTTP/2 in the handshake for HTTPS; lines starting with * in its verbose output are curl's own notes [9]:

$ curl -v https://example.com/ -o /dev/null 2>&1 | grep ALPN
* ALPN: curl offers h2,http/1.1
* ALPN: server accepted http/1.1

These two lines are from curl 8.5.0 against our test server. Against a server that sent no answer, the second line read ALPN: server did not agree on a protocol. Uses default. [13]. In Chrome DevTools, the Protocol column of the Network panel shows the protocol of each request, for example h2 or h3 [12]. More certificate checks are on OpenSSL commands.

FAQ

What is ALPN?

Application-Layer Protocol Negotiation, a TLS extension in which the client lists the protocols it can speak and the server picks one during the handshake. It is how a client and a server agree on HTTP/2 or HTTP/1.1 without an extra round trip. [1]

Is ALPN required for HTTP/2?

For HTTP/2 over TLS, yes: RFC 9113 says such connections MUST use ALPN, with h2 as the identifier. A server that does not select h2 serves HTTP/1.1 to a client that also offered http/1.1. [3] [13]

What does "No ALPN negotiated" mean?

OpenSSL prints it whenever no protocol was agreed. If the server sent no ALPN answer, the handshake completes, and in our runs curl went on with its default, HTTP/1.x; HTTP/2 over TLS needs ALPN, so that connection runs without HTTP/2. If the server ended the handshake with alert 120, the line follows the alert and there is no connection. [13] [3]

What is the difference between ALPN and SNI?

Both are TLS extensions sent in the ClientHello. SNI names the website the client wants, so the server can pick the right certificate; ALPN names the protocol the client wants to speak inside the connection. [2] [14] [1]

What is acme-tls/1?

The ALPN identifier of the TLS-ALPN-01 validation method of ACME. The ACME server connects to port 443 offering only acme-tls/1, and the server under validation answers with a self-signed validation certificate prepared for that protocol. [10]

How do I check if a site uses HTTP/2?

Run openssl s_client with -alpn h2,http/1.1 and look for ALPN protocol: h2, or run curl -v and look for the line saying the server accepted h2. In Chrome DevTools, the Protocol column of the Network panel shows h2 for HTTP/2. [7] [9] [12]

Sources

  1. RFC 7301, TLS Application-Layer Protocol Negotiation Extension: §1, §3.1, §3.2, §4, §5.

  2. RFC 9846, TLS 1.3: §1.3, §2, §4.3, §4.3.10, §6.

  3. RFC 9113, HTTP/2: §3.1, §3.2, §3.3, §10.2.

  4. RFC 9114, HTTP/3: §3.1, §3.1.1, §3.2.

  5. RFC 9001, Using TLS to Secure QUIC: §4, §4.2, §4.3, §8.1.

  6. IANA, TLS ExtensionType Values and TLS ALPN Protocol IDs registries.

  7. OpenSSL 3.0 manual: openssl-s_client (-alpn, -msg), openssl-s_server (-alpn), SSL_CTX_set_alpn_select_cb.

  8. nginx: ngx_http_v2_module (http2); changes in nginx 1.21.4.

  9. curl man page: --http2, --no-alpn, -v.

  10. RFC 8737, ACME TLS ALPN Challenge Extension: §1, §3, §4.

  11. RFC 9460, SVCB and HTTPS Resource Records: §7.1.

  12. Chrome DevTools: Network features reference, Protocol column.

  13. Our runs: OpenSSL 3.0.13 and curl 8.5.0 on Linux, localhost, a www.example.com test certificate from our own lab root.

  14. RFC 6066, TLS Extensions: §3, Server Name Indication.