簡體   English   中英

在Apache PHP上實現http2 push方法

[英]Implementing http2 push method on apache php

我已經在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

請讓我知道我在哪里做錯了?

該代碼看起來很熟悉! 很高興知道我的博客文章非常有用:-)

我已經從我的網站的頭文件中刪除了common.css。 並將這些行添加到.htaccess文件中。

那就是你錯了。 需要在HEAD中正常引用它,並進行推送。

當瀏覽器看到common.css引用時,它將去獲取它並看到它已經被推送,而只是使用被推送的資源。

沒有參考,服務器將推送它,但瀏覽器將忽略它。

注意HTTP / 2推送很復雜,並且有很多類似的地方可能會出錯。 請參閱此帖子以獲取更多信息: https : //jakearchibald.com/2017/h2-push-tougher-than-i-thought/ 許多人說使用它是不值得的,而且獲得的收益還是值得懷疑的,因為您很容易過度推送並導致頁面加載較慢而不是更快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM