利用浏览器缓存不起作用 - Htaccess & mod_expires Active



我一直试图获得杠杆浏览器缓存相当一段时间,我不知道什么可能是问题。我尝试了几种方法来激活它,但都不起作用…

网站运行在Namecheap主机上。我已经联系了技术支持,询问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 ##

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

我已经解决了简而言之:我刚刚解决了这个问题,但你必须启用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中。您现在应该已经通过了杠杆浏览器缓存测试。

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

最新更新