gzip和deflate压缩对于使用mod_deflate或AddOutputFilterByType的Apache服务器都不起作用

时间:2014-02-13 11:42:18

标签: apache .htaccess svg compression gzip

我正在尝试在我的服务器上压缩SVG文件,但无论我放入什么代码.htaccess它都不起作用!

这是我的.htaccess代码:

# ----------------------------------------------------------------------
# Proper MIME type for all files
# ----------------------------------------------------------------------

# JavaScript
#   Normalize to standard type (it's sniffed in IE anyways)
#   tools.ietf.org/html/rfc4329#section-7.2
AddType application/javascript         js jsonp
AddType application/json               json

# Audio
AddType audio/ogg                      oga ogg
AddType audio/mp4                      m4a f4a f4b

# Video
AddType video/ogg                      ogv
AddType video/mp4                      mp4 m4v f4v f4p
AddType video/webm                     webm
AddType video/x-flv                    flv

# SVG
#   Required for svg webfonts on iPad
#   twitter.com/FontSquirrel/status/14855840545
AddType     image/svg+xml              svg svgz
AddEncoding gzip                       svgz

# Webfonts
AddType application/vnd.ms-fontobject  eot
AddType application/x-font-ttf         ttf ttc
AddType font/opentype                  otf
AddType application/x-font-woff        woff

# Assorted types
AddType image/x-icon                        ico
AddType image/webp                          webp
AddType text/cache-manifest                 appcache manifest
AddType text/x-component                    htc
AddType application/xml                     rss atom xml rdf
AddType application/x-chrome-extension      crx
AddType application/x-opera-extension       oex
AddType application/x-xpinstall             xpi
AddType application/octet-stream            safariextz
AddType application/x-web-app-manifest+json webapp
AddType text/x-vcard                        vcf
AddType application/x-shockwave-flash       swf
AddType text/vtt                            vtt


# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<Location />
  # Insert filter
  SetOutputFilter DEFLATE

  # 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
</Location>


# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>

  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>

  # Compress all output labeled with one of the following MIME-types
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                  application/javascript \
                                  application/json \
                                  application/rss+xml \
                                  application/xhtml+xml \
                                  application/xml \
                                  application/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  font/opentype \
                                  image/x-icon \
                                  image/svg+xml \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
  </IfModule>

</IfModule>

当我查看SVG文件的响应头时,似乎没有GZip压缩。 当我通过Google PageSpeed Insights分析页面时,情况也是如此。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

使用deflate模块,因为gzip模块不是标准Apache安装的一部分。

添加以下内容以使用mod_deflate:

<filesMatch "\.(svg|gif|jpg|jpeg|png|ico|swf|js|css|html)$">
 Header set Content-Encoding compress
 # Header set Content-Encoding x-compress 
</filesMatch>

mod_gzip是一个独立的项目。

  

当使用编码进行响应时,Apache将使用客户端请求的任何形式(即x-foo或foo)。如果客户没有专门请求特定表单,Apache将使用AddEncoding指令给出的表单。简而言之,您应该始终使用x-gzip和x-compress进行这两种特定编码。应该在没有x - 。

的情况下指定更新的编码,例如deflate

<强>参考