2013-11-20 68 views

回答

0

如果您從單個Apache服務器(或任何服務器,據我所知)提供所有資源,GZip和緩存並不能真正地協同工作。這是複雜的,但你可以在這裏讀到它:

https://issues.apache.org/bugzilla/show_bug.cgi?id=39727

跳轉到這種做法是徒勞這裏的Roy Fielding的簡介:

https://issues.apache.org/bugzilla/show_bug.cgi?id=39727#c31

基本上,你破壞了一些HTTP的動詞(PUT和有條件的GET),這樣當你處理一系列服務器時,管理變得非常困難。

簡而言之,只需啓用客戶端緩存以獲得最大的單一性能提升。如果您可以提供來自不同服務器的不同文件(例如,staticfiles.mydomain.com和dynamicfiles.mydomain.com),則可以在動態內容上啓用GZip,前提是您不緩存它。

這裏是在.htaccess靜態文件緩存(推薦)進行基本設置:

# Apache Cache Setting 
<IfModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault "access plus 1 hours" 
    ExpiresByType text/html "modification plus 1 days" 
    ExpiresByType text/css "modification plus 1 days" 
    ExpiresByType application/x-javascript "modification plus 1 days" 
    ExpiresByType application/javascript "modification plus 1 days" 
    ExpiresByType image/gif "access plus 1 years" 
    ExpiresByType image/jpeg "access plus 1 years" 
    ExpiresByType image/jpg "access plus 1 years" 
    ExpiresByType image/png "access plus 1 years" 
    EXpiresByType application/x-shockwave-flash "access plus 1 years" 
    ExpiresByType video/x-flv "access plus 1 years" 
</IfModule> 

在你不關心上面的參數情況下,有一個變通 如果您真的要同時啓用緩存和gzip(但不推薦):

SetOutputFilter DEFLATE 

<IfModule mod_deflate.so> 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/xml 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE text/javascript 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/atom_xml 
AddOutputFilterByType DEFLATE application/x-javascript 
AddOutputFilterByType DEFLATE application/x-httpd-php 
AddOutputFilterByType DEFLATE image/svg+xml 
</IfModule> 

<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresDefault "access plus 1 hours" 
ExpiresByType text/html "modification plus 1 days" 
ExpiresByType text/css "modification plus 1 days" 
ExpiresByType application/x-javascript "modification plus 1 days" 
ExpiresByType application/javascript "modification plus 1 days" 
ExpiresByType image/gif "access plus 1 years" 
ExpiresByType image/jpeg "access plus 1 years" 
ExpiresByType image/jpg "access plus 1 years" 
ExpiresByType image/png "access plus 1 years" 
EXpiresByType application/x-shockwave-flash "access plus 1 years" 
ExpiresByType video/x-flv "access plus 1 years" 
</IfModule> 

# remove the ETag (which circumvents the problem but invalidates 
# an important part of the caching (i.e. uniqueness). 
# This is not a good thing!! 
FileETag None 

祝你好運! :)