【问题标题】:Neither gzip nor deflate compression doesn't work for Apache server using mod_deflate or AddOutputFilterByTypegzip 和 deflate 压缩都不适用于使用 mod_deflate 或 AddOutputFilterByType 的 Apache 服务器
【发布时间】:2014-02-13 11:42:17
【问题描述】:

我正在尝试在我的服务器上压缩 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 分析页面时,也会发生同样的情况。

谁能帮帮我?

【问题讨论】:

  • gzip 是否适用于任何其他类型的文件类型?
  • 是的,确实如此。我还注意到附加“Vary:Accept-Encoding”(仅适用于 svg 文件)也失败了。我想知道为什么...
  • 我做了进一步的测试,我发现每个图像/任何文件都没有压缩。我还没有弄清楚出了什么问题。最后但并非最不重要的一点是,我的托管服务提供商没有告诉我任何有用的信息...
  • @OpaleGR 对 jpg 之类的文件进行 gzip 压缩毫无意义。
  • 是的,对 jpg 文件进行 gzip 压缩没有意义,但我确实需要同时 gzip image/svg+xml 和 image/x-icon,因为我的网站在 WebpageTest 和 Google Pagespeed Insights 上的得分是受这种压缩的影响。

标签: apache .htaccess svg compression gzip


【解决方案1】:

使用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。更新的编码,例如 deflate,应该在没有 x- 的情况下指定。

参考文献

【讨论】:

  • 我刚刚尝试了您的代码,但它不起作用。我会尽力为您提供更多信息:现在我正在使用 Apache + cPanel 和“优化网站”工具,它甚至可以成功压缩我的 gzip 文件,但是......它不起作用(仅适用于 image/svg+xml和图像/x 图标)。我认为这是服务器的错。
  • 现在注释掉&lt;IfModule&gt; 语句。将AddEncoding deflate .svgzAddEncoding x-gzip .svgz 也添加到文件的开头。
  • 我已将答案更新为 compressx-compress 而不是 x-deflate,如果其他方法都失败了,这应该可以工作。
  • 我刚刚尝试了compressx-compress 并且...没有结果。我还发现我的网站不是唯一受此问题影响的网站:我尝试在其他网站(由同一服务托管)中 gzip 压缩 svg 文件,但它们也有同样的问题。所以我决定联系我的(共享)托管服务提供商(六个月内第四次!)请他们解释我为什么会遇到这个问题。我认为这个问题是由某种我无法触及的配置引起的,因为我没有权限(共享主机)
  • 您的文件是否与您的 .htaccess 文件位于同一目录中?如果没有,请尝试将其复制到同一目录。
猜你喜欢
  • 2011-06-11
  • 2012-01-04
  • 2018-07-15
  • 1970-01-01
  • 2011-08-02
  • 2013-05-05
  • 2010-12-24
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多