我想在 apache 上启用 h2c 模式,这样我就可以使用 HTTP2.0 协议。在我的虚拟主机配置中,我添加了以下行:
Protocols h2c http/1.1
我也关注了建议禁用 prefork但它没有按预期工作。
目前我在 Ubuntu 上使用 apache 2.4.29。
案例 1)curl
请求 http2 升级
$ curl -vs --http2 http://domain1.com
* Rebuilt URL to: http://domain1.com/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to domain1.com (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: domain1.com
> User-Agent: curl/7.58.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
>
< HTTP/1.1 101 Switching Protocols
< Upgrade: h2c
< Connection: Upgrade
* 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=28
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< date: Sun, 00 Jan 1900 00:00:00 GMT
< server: Apache/2.4.29 (Ubuntu)
< last-modified: Fri, 29 Mar 2019 13:52:29 GMT
< etag: W/"2aa6-5853bfb4c71ac"
< accept-ranges: bytes
< content-length: 10918
< vary: Accept-Encoding
< content-type: text/html
<
.... [snip website code] ....
情况 2)curl
直接使用 http2
$ curl -vs --http2-prior-knowledge http://domain1.com
* Rebuilt URL to: http://domain1.com/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to domain1.com (127.0.0.1) port 80 (#0)
* 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=0
* Using Stream ID: 1 (easy handle 0x5604f1cb1580)
> GET / HTTP/2
> Host: domain1.com
> User-Agent: curl/7.58.0
> Accept: */*
>
* http2 error: Remote peer returned unexpected data while we expected SETTINGS frame. Perhaps, peer does not support HTTP/2 properly.
如您所见,案例 1 工作正常,但案例 2 没有返回网站。为什么会发生这种情况?是因为 Apache 限制直接使用没有安全性的 HTTP2.0 吗?
希望您能回答我,因为我不知道为什么现在事情不起作用。
答案1
我认为我已经找到答案了,我认为这是最新版本的 Apache 中的一个错误。如果我仅在虚拟主机中启用 h2c,错误仍然存在,但如果我在默认虚拟主机 ( 000-default.conf
) 上启用它,一切似乎都运行正常。
我已经测试过并且有效的另一个潜在解决方案是通过修改文件在每个虚拟主机中启用协议 h2 和 h2c mods-enabled/http2.load
:
LoadModule http2_module /usr/lib/apache2/modules/mod_http2.so
<IfModule http2_module>
Protocols h2 h2c http/1.1
</IfModule>
上述任何一个选项似乎都能使系统通过协议协商和先验知识按预期工作。