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
型
1条答案
按热度按时间voase2hg1#
使用
--http2
标志和http://URL,curl发送一个HTTP/1.1消息,请求升级到HTTP/2(upgrade
HTTP头和HTTP2-Settings
HTTP头),然后如果站点声明它支持HTTP/2,则升级,并使用带有upgrade
HTTP头的103响应。这就是您在nghttp 2站点的第一个请求中看到的情况,并且只有当站点理解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(在HTTP/2规范中称为先验知识-因此命令行选项名称):
字符串