httpd-2.4.18 mod_http2 适用于 curl 和 nghttp 但不适用于浏览器

httpd-2.4.18 mod_http2 works with curl and nghttp but doesn't work with the browser

我已经安装了带有 nghttp 1.6.0 和 curl 7.46 的 httpd-2.4.18 以使用 http2 服务器。当我使用 Curl 和 nghttp 命令测试它时,服务器似乎可以使用 http2(如下所示),但是当我使用浏览器(Google Chrome 47.0.2526.106)时,响应 headers 是 http1 而不是 http2,Spdy 指示灯是灰色的(应该是蓝色的)。有人知道为什么吗?

使用的命令

使用的 curl 命令告诉我 http2 工作正常:

eloy@eloy-OptiPlex-745:/usr/local/apache2/logs$ curl --http2 -I http://localhost
HTTP/1.1 101 Switching Protocols
Upgrade: h2c
Connection: Upgrade

HTTP/2.0 200
date:Thu, 07 Jan 2016 21:38:06 GMT
server:Apache/2.4.18 (Unix) OpenSSL/1.0.2e
last-modified:Mon, 11 Jun 2007 18:53:14 GMT
etag:"2d-432a5e4a73a80"
accept-ranges:bytes
content-length:45
content-type:text/html

与nghttp2相同,似乎http2服务器使用以下命令工作正常:

eloy@eloy-OptiPlex-745:/usr/local/apache2/logs$ nghttp -uv http://localhost
[  0.000] Connected
[  0.000] HTTP Upgrade request
GET / HTTP/1.1
host: localhost
connection: Upgrade, HTTP2-Settings
upgrade: h2c
http2-settings: AAMAAABkAAQAAP__
accept: */*
user-agent: nghttp2/1.6.0


[  0.001] HTTP Upgrade response
HTTP/1.1 101 Switching Protocols
Upgrade: h2c
Connection: Upgrade


[  0.001] HTTP Upgrade success
[  0.001] recv SETTINGS frame <length=6, flags=0x00, stream_id=0>

来自浏览器的响应 headers:

HTTP/1.1 304 Not Modified
Date: Thu, 07 Jan 2016 21:49:40 GMT
Server: Apache/2.4.18 (Unix) OpenSSL/1.0.2e
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=5, max=100
ETag: "2d-432a5e4a73a80"

从浏览器请求 Headers:

GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8
If-None-Match: "2d-432a5e4a73a80"
If-Modified-Since: Mon, 11 Jun 2007 18:53:14 GMT

浏览器不支持 HTTP/1.1 到 HTTP/2 升级请求。

从浏览器使用 HTTP/2 的唯一方法是通过 TLS 和 ALPN。

话虽如此,您上面的 "Request headers from browser" 实际上是 response headers 反之亦然,因此很难说出您实际上在做什么。请求 headers 缺少必要的升级位。

如果您从浏览器发出 clear-text 请求(即使用 http 方案),则浏览器不会尝试升级,您将停留在 HTTP/1.1模式。