Nginx是否支持以明文形式从http / 1.1进行http / 2升级(h2c)

时间:2019-05-24 09:13:12

标签: nginx http2 nghttp2

在nginx中是否有任何方法可以支持将http1.1升级到h2c(不在ssl中)?

使用卷曲测试站点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 conf


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

1 个答案:

答案 0 :(得分:0)

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

这就是您在首次请求nghttp2网站时看到的情况,并且只有在该网站同时理解HTTP / 1.1和HTTP / 2的情况下,该方法才有效。

您的nginx配置仅支持HTTP / 2,而不支持HTTP / 1.1,因此无法正常使用,因为它无法理解初始的HTTP / 1.1请求。 Its not possible to make Nginx support both HTTP/1.1 and HTTP/2 on the same port,因此您的配置正确无误-它将无法使用该curl命令。您需要执行此操作以使curl从一开始就使用HTTP / 2(called prior knowledge in the HTTP/2 spec-因此是命令行选项名称):

curl --http2-prior-knowledge -v 10.10.5.89:9006