0

有什么方法可以在 nginx 中支持 http1.1 升级到 h2c(不在 ssl 中)?

使用 curl 测试站点http://nghttp2.org/

$ curl --http2  http://nghttp2.org/ -s -o /dev/null -v

我得到以下结果:

*   Trying 139.162.123.134...
* TCP_NODELAY set
* Connected to nghttp2.org (139.162.123.134) port 80 (#0)
> GET / HTTP/1.1
> Host: nghttp2.org
> User-Agent: curl/7.54.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
>
< HTTP/1.1 101 Switching Protocols
< Connection: Upgrade
< Upgrade: h2c
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=33
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< date: Fri, 24 May 2019 08:58:43 GMT
< content-type: text/html
< last-modified: Thu, 18 Apr 2019 06:19:33 GMT
< etag: "5cb816f5-19d8"
< accept-ranges: bytes
< content-length: 6616
< x-backend-header-rtt: 0.009521
< server: nghttpx
< via: 2 nghttpx
< x-frame-options: SAMEORIGIN
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
<
{ [2159 bytes data]
* Connection #0 to host nghttp2.org left intact

但是当我访问一个 nginx 静态网站时,它会失败。

$ curl --http2 -v 10.10.5.89:9006
* Rebuilt URL to: 10.10.5.89:9006/
*   Trying 10.10.5.89...
* TCP_NODELAY set
* Connected to 10.10.5.89 (10.10.5.89) port 9006 (#0)
> GET / HTTP/1.1
> Host: 10.10.5.89:9006
> User-Agent: curl/7.54.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
>
* Connection #0 to host 10.10.5.89 left intact
$

这是我的 nginx 配置文件


server {
    listen 9006 http2 fastopen=3 reuseport;

    location / {
        autoindex_exact_size off;
        root /www/;
        autoindex on;
        }        
    }
}

nginx调试信息:

2019/05/24 16:49:53 [debug] 348#348: *1 invalid http2 connection preface "GET / HTTP/1.1
"
2019/05/24 16:49:53 [debug] 348#348: *1 http2 state connection error
2019/05/24 16:49:53 [debug] 348#348: *1 http2 send GOAWAY frame: last sid 0, error 1
2019/05/24 16:49:53 [debug] 348#348: *1 http2 frame out: 0000561508B48B08 sid:0 bl:0 len:8
2019/05/24 16:49:53 [debug] 348#348: *1 http2 frame out: 0000561508B48A58 sid:0 bl:0 len:4
2019/05/24 16:49:53 [debug] 348#348: *1 http2 frame out: 0000561508B489A0 sid:0 bl:0 len:18
4

1 回答 1

2

使用--http2标志和 http:// URL curl 发送一条 HTTP/1.1 消息,请求升级到 HTTP/2(upgradeHTTP 标头和HTTP2-SettingsHTTP 标头),然后如果站点声明它支持带有 103 响应的 HTTP/2,则升级带有upgradeHTTP 标头。

这就是您在对 nghttp2 站点的第一个请求中看到的情况,并且仅当站点同时理解 HTTP/1.1 和 HTTP/2 时才有效。

您的 nginx 配置仅支持 HTTP/2 而不是 HTTP/1.1,因此这不起作用,因为它不理解初始 HTTP/1.1 请求。不可能让 Nginx 在同一个端口上同时支持 HTTP/1.1 和 HTTP/2,所以你的配置是正确的 - 它只是不适用于该 curl 命令。您需要这样做以使 curl 从一开始就使用 HTTP/2(在 HTTP/2 规范中称为先验知识- 因此是命令行选项名称):

curl --http2-prior-knowledge -v 10.10.5.89:9006
于 2019-05-24T17:32:08.897 回答