首页 > 解决方案 > 在 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

请让我知道我在哪里做错了?

标签: phpapache.htaccesshttp2

解决方案


这段代码似曾相识!很高兴知道我的博客文章被证明是有用的:-)

我已经从我的网站的头文件中删除了 common.css。并将这些行添加到 .htaccess 文件中。

那就是你出错的地方。它需要在 HEAD 中正常引用并推送。

当浏览器看到common.css引用时,它会去获取它并看到它已经被推送并使用推送的资源。

如果没有引用,服务器会推送它,但浏览器会忽略它。

注意 HTTP/2 Push 很复杂,有很多这样的地方你可能会出错。有关更多信息,请参阅此帖子:https ://jakearchibald.com/2017/h2-push-tougher-than-i-thought/ 。许多人说使用它不值得获得收益,而且收益无论如何都是值得怀疑的,因为您很容易过度推动并导致页面加载速度变慢而不是更快。


推荐阅读