不能gzip一些JS和CSS文件

我正在优化一个magento商店,并且一些JS和CSS(在MEDIA文件夹中编译和缩小)没有被服务gziped,因为他们应该。

所有页面都是gziped(例如这个HTML页面 ),但是一些内容不是(例如这个JS文件 )。

通过gtmetrix.com和whatsmyip的HTTP压缩testing进行testing

我在.htaccess有这个configuration:

  <IfModule mod_deflate.c> # Insert filter on all content SetOutputFilter DEFLATE # Insert filter on selected content types only #AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # Don't compress images #SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </IfModule> # compress text, HTML, JavaScript, CSS, and XML AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/js # remove browser bugs BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent 

我可以错过什么?

你可以在你的.htaccess中使用它

 # BEGIN Compress text files <ifModule mod_deflate.c> <filesMatch "\.(css|js|x?html?|php)$"> SetOutputFilter DEFLATE </filesMatch> </ifModule> # END Compress text files 

这是一个.htaccessconfiguration示例的一部分,这个configuration会导致压缩这里列出的所有* .css,* .js,* .html,* .xhtml和* .php文件( http://www.samaxes.com / 2009/01 / more-on-compressing-and-caching-your-site-with-htaccess / )。 还列出了如何通过头文件和cachingtypes设置过期时间。 但对你来说不是那么重要。

另一种设置压缩的方法是使用apache mod_gzip外部扩展模块。 这也是非常受欢迎的解决scheme。 要使用这个解决scheme,你应该在你的.htaccess中写这个

 <ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule> 

有关更多详细信息,请参阅此文章( http://www.samaxes.com/2008/04/htaccess-gzip-and-cache-your-site-for-faster-loading-and-bandwidth-saving/ ) 。

我遇到了同样的问题,我正在使用分析服务器响应头的Chrome 38.0.2125.104(64位)Dev Tools进行testing。

我想你应该把这个指令添加到你的htaccess

 AddOutputFilterByType DEFLATE text/javascript