【问题标题】:Leverage browser caching not working - Htaccess & mod_expires Active利用浏览器缓存不起作用 - Htaccess & mod_expires Active
【发布时间】:2017-01-20 19:09:56
【问题描述】:

我一直在尝试获取杠杆浏览器缓存,但我不知道可能是什么问题。我尝试了几种方法来激活它,但没有任何效果......

该网站在 Namecheap Hosting 上运行。我已经联系了支持并询问 mod_expires 模块是否处于活动状态,并且根据客户支持它是...

这是我一直在使用的代码:

# START --- Browser Cache Control
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
 
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
 
# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
 
# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js|css)$">
ExpiresDefault A7200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
 
# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>
# END --- Browser Cache Control

我已经尝试了其他一些方法,例如:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

如果有人知道我的代码有什么问题,那就太好了;)

【问题讨论】:

  • 我面临同样的问题。无论我在htaccess 中进行了什么更改,它都无法正常工作

标签: wordpress .htaccess caching browser http-headers


【解决方案1】:

我已经解决了 简而言之:- 我刚刚解决了这个问题,但您必须启用 expires_module 模块。对于 linux,您可以像这样轻松完成。

azureuser@azure: sudo a2enmod expires
Enabling module expires.
To activate the new configuration, you need to run:
service apache2 restart
azureuser@azure: sudo service apache2 restart
[....] Restarting web server: 
. ok

在深处:-

人们发现他们需要利用浏览器缓存,因此他们采取了他们认为的一站式解决方案,并将以下内容添加到他们的 .htaccess 文件中:

ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"

这看起来一切都很好,但他们随后回到他们的指标工具,重新分析并发现这个问题仍然普遍存在。然后他们在接下来的时间里试图弄清楚为什么这不起作用以及他们的网站仍然在狗屋指标明智。不用担心,可以说问题不是网站,而是服务器。如果您使用的是 Debian 服务器,这是您一直在寻找的快速修复:登录到您的 Dedicated/VPS 并发出以下命令,该命令将检查您的服务器上加载了哪些模块,您正在寻找 expires_module 在列表

azureuser@azure: sudo apachectl -M
Loaded Modules:
core_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_default_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
mime_module (shared)
negotiation_module (shared)
php5_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
ssl_module (shared)
status_module (shared)
Syntax OK

所以这个列表中没有 expires_module 的迹象,接下来你要做的就是安装它

azureuser@azure: sudo a2enmod expires
Enabling module expires.
To activate the new configuration, you need to run:
service apache2 restart
azureuser@azure: sudo service apache2 restart
[....] Restarting web server: 
. ok

返回您的指标并重新运行测试,请务必将上面指定的代码也添加到您的 htaccess 中。您现在应该已经通过了 Leverage browser Caching 测试。

我的网站速度是 85,我试图解决 Leverage 缓存,但最后我解决了这个问题。 截图:-https://prnt.sc/iu3z2t

【讨论】:

  • 谢谢。从来没有人提到过这个。
  • 启用过期模式对我有用。我的设置是 LAMP + WordPress + W3 Total Cache 插件。 W3TC 使用 ExpiresByType 行创建 htaccess 文件。没有任何效果,因为我没有启用该模组。
猜你喜欢
  • 2020-07-21
  • 2014-12-24
  • 2013-01-21
  • 2016-09-18
  • 2014-11-03
  • 2018-05-05
  • 2017-01-09
相关资源
最近更新 更多