在Apache PHP上实现http2 push方法

时间:2019-07-11 13:50:20

标签: php apache .htaccess 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');
?>

现在,我可以在inspect元素中看到所有的css文件,还可以看到Initialtor Push / others,但是它不适用于页面。页面已损坏。 Apache服务器:2.4.6

请让我知道我做错了什么地方

1 个答案:

答案 0 :(得分:0)

该代码看起来很熟悉!很高兴知道my blog post很有用:-)

  

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

这就是您出问题的地方。需要在HEAD中正常引用它,并进行推送。

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

没有参考,服务器将推送它,但浏览器将忽略它。

请注意,HTTP / 2推送非常复杂,在类似这样的情况下,您可能会出错。有关更多信息,请参见此帖子:https://jakearchibald.com/2017/h2-push-tougher-than-i-thought/。许多人说使用它是不值得的,而且获得的收益还是值得怀疑的,因为您可以轻易地过度推送并导致页面加载较慢而不是更快。