2016-09-13 97 views
1

我一直在试图获得杠杆浏览器缓存很长一段时间,我不知道可能是什么问题。我尝试了几种方法来激活它,但没有任何工作...利用浏览器缓存无法正常工作 - 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 ##

这将是非常好的,如果任何人有一个想法可能是错我的代码;)

+0

我面临着同样的问题。无论我在'htaccess'中更改了什么,它都不起作用 –

回答

1

我已经解决了IT 简而言之: - 我只是解决了这个问题,但你必须启用expires_module模块。对于Linux,你可以轻松地做到这一点。

[email protected]: sudo a2enmod expires 
Enabling module expires. 
To activate the new configuration, you need to run: 
service apache2 restart 
[email protected]: 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" 

这似乎一切都很好,但然后他们回到自己的度量工具,重新分析,看看这个问题仍然不绝于耳..然后,他们在接下来的同时,试图找出为什么这不起作用,他们的网站仍然在d og house metrics明智。不用担心,这个问题不是一个可以这么说的网站,它是一个服务器。如果您使用的是Debian服务器,这是您一直在寻找的快速解决方案:登录到您的专用/ VPS并发出以下命令,该命令将检查您的服务器上加载了哪些模块,并且正在查找中的expires_module名单

[email protected]: 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的迹象,接下来你需要做的就是安装它

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

头回你的指标,并重新运行测试,一定要将上面指定的代码添加到您的htaccess中。您现在应该已通过杠杆浏览器缓存测试。

我的网站速度是85,我试图解决杠杆缓存,但最终我解决了这个问题。 屏幕截图: - http://prntscr.com/fcuutv