简体   繁体   English

HTTP / 2服务器推送不起作用,我在做什么错?

[英]HTTP/2 Server Push not working, what am I doing wrong?

This is my first question here, hope I'm doing it right. 这是我的第一个问题,希望我做对了。

I'm using a LiteSpeed webserver with HTTP/2 (shared hosting provider), but I can't get the push feature to work. 我正在使用带有HTTP / 2(共享主机提供程序)的LiteSpeed Web服务器,但无法使用推送功能。 I'm not quite sure how to check if it works, but at least as far as I can tell it isn't working properly. 我不太确定如何检查它是否有效,但至少据我所知它不能正常工作。

So I have looked up a ton of guides ( HTTP/2 Server Push Tutorial or A Comprehensive Guide To HTTP/2 Server Push as examples), and they all mention changing the headers by adding the following: 因此,我查阅了很多指南(例如HTTP / 2 Server Push教程HTTP / 2 Server Push 的综合指南 ),它们都提到通过添加以下内容来更改标头:

link: </my/theme/css/style.css>; rel=preload; as=style

I understand that this code tells the server to preload said resource, in this case style.css. 我知道这段代码告诉服务器预加载所述资源,在本例中为style.css。 However, if the server supports HTTP/2 push, then it should automatically push the resource instead (as long as you don't add nopush at the end of it). 但是,如果服务器支持HTTP / 2推送,则它应该自动推送资源(只要您不在其末尾添加nopush)。 The second link does mention that this is true for most, but not all servers. 第二个链接确实提到对于大多数服务器(但不是所有服务器)都适用。

So with this in mind, I created a simple test PHP page to see if I could get it working. 因此,考虑到这一点,我创建了一个简单的测试PHP页面,以查看是否可以正常运行。 The result was this: 结果是这样的:

Chrome DevTools showing Headers tab with link preload Chrome DevTools显示带有链接预加载的“标题”标签

Network tab in DevTools showing the result DevTools中的“网络”选项卡显示结果

So most of the guides I saw mentioned that the Initiator in the Network tab for the pushed resource would say Push / Other (or similar) instead of just Other , but as you can see from my result it just says Other. 因此,我看到的大多数指南都提到,“推送”资源的“网络”选项卡中的“启动器”将表示“ 推送/其他” (而不是“ Other”) ,但是从我的结果中可以看到,它只是说“ Other”。 Not to mention that the resource doesn't get loaded in the same request, it just looks like a simple preload to me. 更不用说该资源不会在同一请求中加载,对我而言,这就像一个简单的预加载。 I also tested my site with https://http2.pro/check , and it says no resource is pushed. 我还使用https://http2.pro/check测试了我的网站,它说没有资源被推送。

This was tested with Chrome 71.0.3578.98. 已使用Chrome 71.0.3578.98进行了测试。

  • Am I missing something important here? 我在这里错过了重要的事情吗? Could it be a problem with my server? 我的服务器可能有问题吗?
  • Another question. 另一个问题。 When defining multiple resources in the headers for preload and push, some guides mention creating multiple link-elements, while others mention comma separating them in a single link-element. 在标头中为预加载和推送定义多个资源时,一些指南提到创建多个链接元素,而另一些指南则提到用逗号将它们分隔为单个链接元素。 Are both of these correct, is one better than the other? 两者都是正确的,一个比另一个好吗?

Any help or enlightenment is appreciated, thanks! 感谢您的任何帮助或启发!

So there can be a few things after some testing with various configurations, browser versions and flags enabled/disabled within Chrome as well as investigating with the network event viewer. 因此,在通过各种配置,浏览器版本和Chrome中启用/禁用的标记进行测试以及通过网络事件查看器进行调查之后,可能会有一些事情。

If the LiteSpeed Web Server has QUIC enabled, Chrome v71 seems to be showing the request as a normal GET request, and won't indicate it being pushed, however looking in the network event viewer in Chrome, it reveals that it indeed is pushed (Look for QUIC_SESSION_PUSH_PROMISE_RECEIVED). 如果LiteSpeed Web服务器已启用QUIC,则Chrome v71似乎将请求显示为普通的GET请求,并且不会指示该请求已被推送,但是在Chrome的网络事件查看器中查看时,它表明确实已将其推送(查找QUIC_SESSION_PUSH_PROMISE_RECEIVED)。

Now, this will only be the case if QUIC in the browser is enabled as well, which it isn't by default currently in Chrome v71 as far as I can tell (they disable and enable things from time to time) - so you either have to have a flag set in Chrome to enable QUIC and you'll see the above behavior (You should also see http/2+quic/43 in "Protocol" under web developer tools). 现在,只有在启用浏览器中的QUIC的情况下,情况才会如此,据我所知,Chrome v71当前不是默认情况(它们会不时禁用和启用)-所以您要么必须在Chrome中设置一个标记才能启用QUIC,并且您会看到上述行为(您还应该在Web开发人员工具下的“协议”中看到http/2+quic/43 )。

If QUIC isn't enabled in the Browser or on the web server, you should see the file being pushed, and Chrome v71 would indicate this with the Initiator "Push / Other". 如果未在浏览器或Web服务器上启用QUIC,则应该看到正在推送的文件,Chrome v71会使用启动程序“推/其他”指示这一点。

Now, in Chrome v73 (Canary release), it will have the "Initiator" set to Other and the "Size" should be (from memory cache) , this indicates the file being pushed. 现在,在Chrome v73(Canary版本)中,它将“启动器”设置为“ Other ,“大小”应为(from memory cache) ,这表明文件已被推送。

In Google Chrome you can go to chrome://net-export/ and click "Start Logging to Disk", then refresh your browser window where you have the file that expects to be pushed - stop the measurement, and go to https://netlog-viewer.appspot.com/ - import the json file here, and go to "QUIC", find your domain name in the list, and click on the "Connection ID". 在Google Chrome浏览器中,您可以转到chrome:// net-export /,然后单击“开始将日志记录到磁盘”,然后刷新浏览器窗口,在该窗口中,您有要推送的文件-停止测量,然后转到https:/ /netlog-viewer.appspot.com/-在此处导入json文件,然后转到“ QUIC”,在列表中找到您的域名,然后单击“连接ID”。

Then click the "QUIC_SESSION" in the list, and search for QUIC_SESSION_PUSH_PROMISE_RECEIVED - if there's matches, you know for a fact that the files are pushed. 然后,单击列表中的“ QUIC_SESSION”,并搜索QUIC_SESSION_PUSH_PROMISE_RECEIVED如果存在匹配项,则知道文件已被推送。

It's a bit of digging to figure out whether it's pushed currently, and the confusion is mainly related to Chrome Dev Tools not really being consistent between protocols and versions. 要弄清楚是否当前已推送它有点麻烦,而这种困惑主要与Chrome Dev Tools协议和版本之间的不一致有关。

First of all you need to find out if your infrastructure uses Link headers to activate push. 首先,您需要确定您的基础架构是否使用Link标头来激活推送。 Litespeed documentation doesn't seem great here but let's assume that is how you activate push. Litespeed文档在这里似乎不太好,但是让我们假设这是激活推送的方式。

Next is to check if your server is sending the pushed request. 接下来是检查您的服务器是否正在发送推送的请求。 I find nghttp the best tool to do this so if you have access to this too, then run a command like this to see the HTTP/2 frames and if the PUSH_PROMISE frame is sent, indicating the style sheet is being pushed: 我发现nghttp是执行此操作的最佳工具,因此,如果您也可以访问此命令,请运行类似的命令以查看HTTP / 2框架,以及是否发送了PUSH_PROMISE框架,表明正在推送样式表:

nghttp -anv https://www.example.com

You can also see the frames using Chrome, though they've just made this a lot harder but insisting on logging to disk first and then opening in a viewer. 您也可以使用Chrome浏览器查看框架,尽管它们使难度增加很多,但坚持先记录到磁盘然后在查看器中打开。 Follow the instructions similar to @LucasRolff's answer, but for HTTP/2 or here . 请遵循与@LucasRolff的答案类似的说明,但要使用HTTP / 2或此处

Assuming the server is pushing the resource you're next left with why Chrome is not using that pushed resource. 假设服务器正在推送资源,那么您接下来要了解的是Chrome为什么不使用该推送资源。 The most common reason is if you are using a self-signed TLS certificate to provide HTTPS that the browser does not recognise. 最常见的原因是,如果您使用自签名TLS证书来提供浏览器无法识别的HTTPS。 Even if you click through the error and get a connection with a red padlock, Chrome will not use the cache for that connection, and so cannot use HTTP/2 push (which requires using the HTTP/2 push cache). 即使您点击错误并获得带有红色挂锁的连接,Chrome也不会对该连接使用缓存,因此无法使用HTTP / 2推送 (这需要使用HTTP / 2推送缓存)。 The nghttp or Chrome event log output should show if this is the case. 如果是这种情况,应该显示nghttp或Chrome事件日志输出。 Alternatively try using Firefox which DOES allow HTTP/2 push to be used if unrecognised certificates are clicked through. 或者,尝试使用Firefox,如果单击了无法识别的证书,则确实允许使用HTTP / 2推送。

Finally to your last question: 最后是您的最后一个问题:

When defining multiple resources in the headers for preload and push, some guides mention creating multiple link-elements, while others mention comma separating them in a single link-element. 在标头中为预加载和推送定义多个资源时,一些指南提到创建多个链接元素,而另一些指南则提到用逗号将它们分隔为单个链接元素。 Are both of these correct, is one better than the other? 两者都是正确的,一个比另一个好吗?

Both are correct. 两者都是正确的。 HTTP (whether HTTP/1.1 or HTTP/2) defines this: HTTP(无论是HTTP / 1.1还是HTTP / 2)定义了以下内容:

header1: value1
header1: value2

and this: 和这个:

header1: value1, value2

as syntactically identical : 语法上相同

A recipient MAY combine multiple header fields with the same field name into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma. 接收者可以通过将每个随后的字段值按顺序附加到合并的字段值上并用逗号分隔,将多个具有相同字段名的头字段组合成一对“字段名:字段值”,而无需更改消息的语义。逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM