2016-09-13 95 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