Fix nginx: the "listen ... http2" directive is deprecated
nginx 1.25.1+ warns "listen ... http2" is deprecated. Drop http2 from each listen line and add http2 on; per server. On 1.24 (Ubuntu 24.04) http2 on fails.
nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead in /etc/nginx/conf.d/default.conf:2Since nginx 1.25.1 the http2 parameter of listen 443 ssl http2; is deprecated. Remove http2 from the listen line and add http2 on; to the same server block, and the warning goes away. It is only a warning: nginx -t still succeeds and HTTP/2 keeps working.
[1] Check your nginx version first. The fix depends on it.
$ nginx -v| Version | listen ... http2 | http2 on; |
|---|---|---|
| 1.25.0 and older (including 1.24.0 on Ubuntu 24.04) | no warning | **unknown directive "http2" error** |
| 1.25.1 and newer (official stable 1.30.6, mainline 1.31.8, 1.28.3 on Ubuntu 26.04) | warning | works |
The nginx changelog for 1.25.1 (2023-06-13) says it added the http2 directive, which enables HTTP/2 per server, and that the http2 parameter of listen is now deprecated. On Ubuntu 24.04 you first see this warning after switching to the nginx.org packages or the official Docker image, or after upgrading to 26.04.
[2] Find every line the warning points to. You get one warning per listen line with http2, so an IPv4 plus an IPv6 line gives two. Use nginx -T to include all included files.
$ sudo nginx -T 2>/dev/null | grep -nE "^# configuration file|listen .*http2"[3] Remove http2 from listen and add http2 on;. Back up first (sudo cp -a /etc/nginx /etc/nginx.bak).
server { listen 443 ssl; listen [::]:443 ssl; http2 on; server_name example.com; ssl_certificate /etc/nginx/certs/cert.pem; ssl_certificate_key /etc/nginx/certs/key.pem;}The http2 directive is valid in http, server context. To enable it for every site, one line in the http block also works.
[4] Change **every other server block on the same port** too. Fix only one and the leftover block keeps the warning alive. Here the second block still has listen 443 ssl http2;:
nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead in /etc/nginx/conf.d/default.conf:10nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulThe opposite mistake is quieter. If you strip http2 from listen but forget http2 on; in some block, that site drops to HTTP/1.1. The old parameter turned HTTP/2 on for the whole address and port; http2 on; is per server. Two sites on 443, with http2 on; only in example.com:
$ curl -sk --http2 -o /dev/null -w '%{http_version}\n' --resolve example.com:443:127.0.0.1 https://example.com/2$ curl -sk --http2 -o /dev/null -w '%{http_version}\n' --resolve www.example.com:443:127.0.0.1 https://www.example.com/1.1The warning is gone, yet one site lost HTTP/2. Check each name with step [6].
[5] Test and reload. The fixed config passes with no warning.
$ sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulThe same fixed config on Ubuntu 24.04's 1.24.0 fails. There, remove http2 on; and go back to listen 443 ssl http2; — that is the correct syntax on 1.24 and it does not warn.
nginx: configuration file /etc/nginx/nginx.conf test failed[6] Confirm that the site really answers over HTTP/2. Repeat for each server name.
$ curl -sk --http2 -I https://example.com/HTTP/2 200 server: nginx/1.30.6A first line of HTTP/2 means it works. HTTP/1.1 means that server block has no http2 on;, or a proxy or CDN in front terminates the connection itself.
For testing HTTPS with a self-signed certificate see nginx self-signed certificate; for checking the Ubuntu package version see Install nginx on Ubuntu 24.04.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.