利用浏览器缓存无法正常工作 - Htaccess& mod_expires有效 [英] Leverage browser caching not working - Htaccess & mod_expires Active

查看:123
本文介绍了利用浏览器缓存无法正常工作 - Htaccess& mod_expires有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用杠杆浏览器缓存很长一段时间,我不知道可能是什么问题。我尝试了几种方法来激活它,但没有任何作用......

I´ve been trying to get The leverage browser cache for quite a while and I have no idea what could be the problem. I tried several methods to activate it, but nothing works...

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

The site is running on Namecheap Hosting. I already contacted the support and asked if the mod_expires module is active and according to the customer support it is...

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

This is the code I´ve been using:

# 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

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

I already tried some other methods like:

## 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 ##

如果有人知道我的代码可能有什么问题,那将是非常好的;)

It would be really nice if anyone has an idea what could be wrong with my code ;)

推荐答案

我已经解决了它
简而言之: -
我刚刚解决了这个问题,但你必须启用expires_module模块。对于linux,你可以这么简单。

I HAVE RESOLVED IT In short:- I just resolved this issue but you have to enable expires_module module. For linux you can do it easy like that.

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

In Deep: -

In Deep:-

人物他们正在看到他们需要利用浏览器缓存,所以他们做了他们认为的一站式修复,并将其添加到他们的.htaccess文件中:

People are seeing that among other things they need to Leverage Browser Caching, so they do what they think is the one stop fix and thats to add something like the following to their .htaccess file:

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 list

This seems all well and good but they then go back to their metrics tool, re-analyse and see that this issue is still prevalent.. They then spend the next while trying to figure out why this is not working and their website is still in the dog house metrics wise. well fear not the issue is not a website one so to speak, its a server one. If you are using a Debian server this is the quick fix you have been looking for: log into your Dedicated/VPS and issue the following command which will check to see what modules are loaded on your server, you are looking for the expires_module in the list

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的迹象,接下来您需要做的就是安装它

So there is no sign of the expires_module in this list, next up all you have to do is install it

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也。您现在应该通过杠杆浏览器缓存测试。

Head back to your metrics and rerun the test, be sure to add the code specified above to your htaccess also. You should now have passed the Leverage browser Caching test.

我的网站速度是85,我试图解决杠杆缓存但最后我用这个解决了它。
屏幕截图: - https://prnt.sc/iu3z2t

My website speed was 85, I was trying to resolve Leverage cache but finally i resolved it with this. Screenshot:- https://prnt.sc/iu3z2t

这篇关于利用浏览器缓存无法正常工作 - Htaccess&amp; mod_expires有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆