【问题标题】:Cache-Control + mod_expires setup at .htaccess but leverage browser cache still do not workCache-Control + mod_expires 设置在 .htaccess 但利用浏览器缓存仍然不起作用
【发布时间】:2020-07-21 15:51:25
【问题描述】:

我已经对 .htaccess 文件进行了微调,但无法使杠杆浏览器缓存正常工作。 GTMetrix 告诉我几个 .png 和 .svg 图像没有设置过期。 1. Expires_module 已启用并在 Apache 中运行 2. Set Cache-Control 和 Enable Browser Cache 在.htaccess 中设置。 .htaccess 代码如下。欢迎任何使浏览器缓存工作的建议。 :-)

#Set Cache-Control
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|svg|js|css|swf)$">
Header set Cache-Control "max-age=84600, public"
#End Cache-Control
</FilesMatch>
#Enable browser cache
<IfModule mod_expires.c>
  ExpiresActive On

  # Images
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"

  # Video
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType video/mpeg "access plus 1 year"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # Others
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>
#End browser cache
#Enable HSTS

【问题讨论】:

    标签: .htaccess caching browser controls mod-expires


    【解决方案1】:

    您需要确保在 CentOS 和 Ubuntu 上启用 mod_expires

    在您的 CentOS 系统上运行以下命令,查看是否启用了 Apache 模块 mod_expires,如果启用,它应该显示以下输出,否则您将看不到任何输出。

    httpd -M | grep expires
    

    验证它的另一种方法是通过 httpd.conf 文件,在您最喜欢的文本编辑器应用程序(Vi/ViM、Nano 等)中打开 apache 配置文件并搜索以下行。如果成功找到,则您的 Apache 已启用 mod_expires。

    LoadModule expires_module modules/mod_expires.so
    

    对于 Ubuntu 系统,我们需要使用 a2enmod 实用程序来启用 Apache 模块,只需运行以下命令即可在 Ubuntu 上启用 expires 模块。

    sudo a2enmod expires
    

    完成后,我们需要重新启动 apache 才能使更改生效。

    sudo /etc/init.d/apache2 restart
    

    sudo service apache2 restart
    

    然后转到 GTmetrics 或 Google PageSpeed 并测试您的“浏览器杠杆缓存”网址并检查缓存时间是否根据您在 .htaccess 中的条目更新

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2014-12-24
      • 2013-01-21
      • 2016-09-18
      • 2014-11-03
      • 2016-01-13
      • 2018-05-05
      相关资源
      最近更新 更多