php - 在 apache php 上实现 http2 推送方法
问题描述
我已经在 http2 上配置了我的网站,但即使在阅读了这么多文章之后,我仍然无法弄清楚它的一些实现。
我已从common.css
我的网站的头文件中删除。并将这些行添加到.htaccess
文件中。
<IfModule http2_module>
#Check if there's a cookie saying the css has already been loaded:
SetEnvIf Cookie "cssloaded=1" cssloaded
#If no cookie, and it's an html file, then push the css file
#and set a session level cookie so next time it won't be pushed:
<filesMatch "\.([pP][hH][pP]?)">
Header add Link "</assets/css/common.css>;rel=preload;as=style" env=!cssloaded
Header add Set-Cookie "cssloaded=1; Path=/; Secure; HttpOnly" env=!cssloaded
</filesMatch>
</IfModule>
但我的网站根本没有加载common.css
。它坏了。我的网站位于 Apache 服务器之后,并且网站完全建立在 codeIgniter 之上。
我也确实将这些添加到我的common_head.php
文件中
<?php
header: header('Link: </assets/css/jquery-ui.css>; rel=preload; as=style,</assets/css/jquery.mCustomScrollbar.min.css>; rel=preload; as=style,</assets/css/slick.min.css>; rel=preload; as=style,</assets/css/slick-theme.min.css>; rel=preload; as=style,</assets/css/bootstrap.min.css>; rel=preload; as=style,</assets/css/common.css>; rel=preload; as=style,,</assets/css/jplayer.blue.monday.min.css>; rel=preload; as=style');
?>
现在我可以在检查元素中看到所有的 css 文件,也可以看到 initialtor Push/others 但它不适用于页面。页面已损坏。阿帕奇服务器:2.4.6
请让我知道我在哪里做错了?
解决方案
这段代码似曾相识!很高兴知道我的博客文章被证明是有用的:-)
我已经从我的网站的头文件中删除了 common.css。并将这些行添加到 .htaccess 文件中。
那就是你出错的地方。它需要在 HEAD 中正常引用并推送。
当浏览器看到common.css
引用时,它会去获取它并看到它已经被推送并使用推送的资源。
如果没有引用,服务器会推送它,但浏览器会忽略它。
注意 HTTP/2 Push 很复杂,有很多这样的地方你可能会出错。有关更多信息,请参阅此帖子:https ://jakearchibald.com/2017/h2-push-tougher-than-i-thought/ 。许多人说使用它不值得获得收益,而且收益无论如何都是值得怀疑的,因为您很容易过度推动并导致页面加载速度变慢而不是更快。
推荐阅读
- r - geom_point 颜色基于截止值
- spring - 如何使用jpa或hibernate防止删除表
- angular - “AppModule”类型上不存在属性“ngRedux”。?
- javascript - 如何在异步函数中抛出错误并仅在非立即调用者中处理?
- html - 将属性添加到动态创建的表
- c# - 如何在没有第一页 itext 7 的情况下添加页眉
- javascript - 如何使用 node js 中的 express 通过 server.js 文件将照片渲染到网页
- javascript - 使用 jQuery/JavaScript 在图像上插入/显示对象的属性
- elasticsearch - 仅从 ElasticSearch 中获取匹配的值和对应的字段
- python - python中的导入链接器引发错误