【问题标题】:Gzip Compression and leverage Browser Cache Not working for my storeGzip 压缩和利用浏览器缓存不适用于我的商店
【发布时间】:2017-07-28 09:00:36
【问题描述】:

我正在尝试优化我的商店,我在.htaccess 文件中添加了 Gzip 代码和利用浏览器缓存代码:

# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #

# Leverage browser caching using mod_headers #
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
        Header set Cache-Control "public"
    </FilesMatch>
</IfModule>
# End of Leverage browser caching using mod_headers #

【问题讨论】:

  • 您是否检查过这些模块实际上在 Apache 中已启用?你如何检查它是否被使用?
  • 你问的问题很好,请继续提问并在这里分享知识@Randhir

标签: php .htaccess optimization magento-1.9


【解决方案1】:

启用 Apache mod_headers 和 mod_expires 模块

请通过以下代码检查您的商店是否启用了 mod_expires 和 mod_headers。

<?php phpinfo();?>

如果两个扩展都无法访问您的服务器,请按照以下步骤操作:-

第 1 步:-

现在使用 SSH 控制台登录服务器并继续以下步骤:

启用 mod_headers:

sudo a2enmod headers

启用 mod_expires:

sudo a2enmod expires

第 2 步:-

服务器更新完成后,需要重启Apache服务器 使这些变化有效。在您的 SSH 控制台中输入以下行 重新启动 Apache。

service apache2 restart

或者通过 SSH 中的以下代码重新启动您的服务器:

reboot

在您的 .htaccess 文件中使用以下代码。

<IfModule mod_headers.c>
     # YEAR
     <FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
          Header set Cache-Control "max-age=29030400"
     </FilesMatch>
     # WEEK
     <FilesMatch "\.(js|css|swf|woff)$">
         Header set Cache-Control "max-age=604800"
     </FilesMatch>
     # 45 MIN
     <FilesMatch "\.(html|htm|txt)$">
        Header set Cache-Control "max-age=86400"
     </FilesMatch>

     Header set Connection keep-alive

</IfModule>

<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl|asp|html)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

清除您的商店缓存并重新加载您的网站 URL 后,重新检查 gtmetrix 工具或您正在使用的任何工具。

【讨论】:

  • 谢谢你如此详细的解释,你让我很开心。现在工作正常,我检查了 GTmetrix 工具,我的网站速度看起来不错。再次感谢@Umesh Kumar
  • 很好的解释,谢谢
【解决方案2】:

这是GZIP压缩的代码

<ifModule mod_deflate.c>

   SetOutputFilter DEFLATE
   AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   BrowserMatch ^Mozilla/4\.0[678] no-gzip
   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
   BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
   SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
   Header append Vary User-Agent env=!dont-vary
</ifModule>

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

为了利用浏览器缓存您的代码是正确的,但如果它不起作用,那么您必须检查 mod_expires 和 mod_headers 模块是否在您的服务器上启用。

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2014-03-23
    • 2013-09-26
    • 2011-10-15
    相关资源
    最近更新 更多