nginx.ccWeb 服务器排障台
emerg

Nginx unknown directive 报错:http2、brotli、quic 等

快速答案

nginx -t 报 “unknown directive "http2"” 之类,说明当前 nginx 不认识这个指令:要么版本太旧(http2 on 需要 1.25.1+),要么所需模块没编译或没加载(brotli、headers-more 是第三方模块),要么是拼写错误、不可见字符。先看 nginx -V 的版本和模块,再改写法或装模块。

最常见的原因:发行版自带的 nginx 太旧:Debian 12 是 1.22,Ubuntu 24.04 是 1.24,没有 http2 on;。

先这样改:旧版本的 HTTP/2 写法

# nginx < 1.25.1
listen 443 ssl http2;
# nginx ≥ 1.25.1
listen 443 ssl;
http2 on;

最后核对: · 依据下方列出的官方文档 · 出处

典型日志

nginx: [emerg] unknown directive "http2" in /etc/nginx/sites-enabled/example.conf:4
nginx: [emerg] unknown directive "brotli" in /etc/nginx/nginx.conf:30
nginx: [emerg] unknown directive "more_set_headers" in /etc/nginx/conf.d/security.conf:2

常见原因

  1. 发行版自带的 nginx 太旧:Debian 12 是 1.22,Ubuntu 24.04 是 1.24,没有 http2 on;。
  2. brotli、more_set_headers 等第三方模块没安装,或 load_module 没写。
  3. listen … quic 需要带 --with-http_v3_module 编译的 nginx(1.25.0+)。
  4. 从网页复制配置带进了全角空格、零宽字符或中文标点。

排查命令

版本和编译进来的模块
#nginx -V 2>&1 | tr ' ' '\n' | grep -E 'version|http_v2|http_v3|brotli|headers-more'
可加载的动态模块
#ls /etc/nginx/modules-enabled/ /usr/lib/nginx/modules/ 2>/dev/null
找出非 ASCII 字符
#grep -nP '[^\x00-\x7F]' /etc/nginx/sites-enabled/example.conf

修复配置

旧版本的 HTTP/2 写法

# nginx < 1.25.1
listen 443 ssl http2;
# nginx ≥ 1.25.1
listen 443 ssl;
http2 on;

Debian 12+ 安装 brotli 模块

apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static
nginx -t && systemctl reload nginx

或者换成 nginx.org 官方源的新版本

curl -fsSL nginx.cc/install | sudo bash -s -- --dry-run

改完先 nginx -t,通过后 systemctl reload nginx。

关键事实与出处

  • http2 指令出现在 nginx 1.25.1。 [nginx.org]
  • HTTP/3 模块从 1.25.0 起提供,默认不编译,需要 --with-http_v3_module。 [nginx.org]

以上事实于 2026-11-14 对照原文核对;nginx 版本更新后请以官方文档为准。

常见问题

“directive is not allowed here” 是同一个问题吗?

不是。那表示指令存在但写错了层级(比如把 server_names_hash_bucket_size 写进了 server),看官方文档里的 Context 一栏。

nginx.org 官方包有 brotli 吗?

brotli 不是 nginx 自带模块,需要单独安装第三方模块包或自行编译;Debian 12 起官方仓库有 libnginx-mod-http-brotli-filter。

用工具解决

相关报错

推广位(待接入)
这里以后会放 VPS / CDN 推荐,并标明「推广链接」。目前没有任何返佣链接。