gzip和deflate压缩都不会';不能使用mod_deflate或AddOutputFilterByType为Apache服务器工作

gzip和deflate压缩都不会';不能使用mod_deflate或AddOutputFilterByType为Apache服务器工作,apache,.htaccess,svg,compression,gzip,Apache,.htaccess,Svg,Compression,Gzip,我试图在我的服务器上压缩SVG文件,但不管我在.htaccess中输入什么代码,它都不起作用 这是我的.htaccess代码: # ---------------------------------------------------------------------- # Proper MIME type for all files # ---------------------------------------------------------------------- # Java

我试图在我的服务器上压缩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>
#----------------------------------------------------------------------
#所有文件的正确MIME类型
# ----------------------------------------------------------------------
#JavaScript
#标准化为标准类型(它在IE中被嗅探到)
#tools.ietf.org/html/rfc4329#第7.2节
AddType应用程序/javascript jsonp
AddType应用程序/json
#音频
AddType音频/ogg oga ogg
AddType音频/mp4 m4a f4a f4b
#录像带
AddType视频/ogg ogv
AddType视频/mp4 mp4 m4v f4v f4p
AddType视频/webm webm
AddType视频/x-flv flv
#SVG
#iPad上的svg webfonts需要
#twitter.com/FontSquirrel/status/14855840545
AddType image/svg+xml svg svgz
加法编码gzip svgz
#网络字体
AddType应用程序/vnd.ms-fontobject eot
AddType应用程序/x-font-ttf ttf ttc
AddType字体/opentype otf
AddType应用程序/x-font-woff woff
#各种类型
AddType图像/x图标图标图标
AddType图像/webp webp
AddType文本/缓存清单appcache清单
AddType文本/x组件htc
AddType应用程序/xml rss原子xml rdf
AddType应用程序/x-chrome-extension crx
AddType应用程序/x-opera-extension oex
AddType应用程序/x-xpinstall xpi
AddType应用程序/八进制流safariextz
AddType应用程序/x-web-app-manifest+json webapp
AddType text/x-vcard vcf
AddType应用程序/x-shockwave-flash swf
AddType文本/vtt vtt
# ----------------------------------------------------------------------
#Gzip压缩
# ----------------------------------------------------------------------
#插入过滤器
SetOutputFilter放气
#Netscape 4.x有一些问题。。。
浏览器匹配^Mozilla/4 gzip纯文本/html
#网景4.06-4.08还有一些问题
浏览器匹配^Mozilla/4\.0[678]无gzip
#MSIE伪装成网景,但它很好
BrowserMatch\bMSIE!没有gzip!仅限gzip文本/html
#不要压缩图像
SetEnvIfNoCase请求_URI\(?:gif | jpe?g | png)$no gzip不变
#确保代理不会提供错误的内容
标头附加变量用户代理env=!不要改变
# ----------------------------------------------------------------------
#Gzip压缩
# ----------------------------------------------------------------------
#针对破损的标题强制放气developer.yahoo.com/blogs/ydn/posts/2010/12/push-beyond-gzip/
SetEnvIfNoCase^((gzip | deflate)\s*,?\s*)+|[X~-]{4,13}$有接受编码
RequestHeader追加接受编码“gzip,deflate”env=HAVE_Accept-Encoding
#压缩使用以下MIME类型之一标记的所有输出
AddOutputFilterByType DEFLATE应用程序/atom+xml\
应用程序/javascript\
应用程序/json\
application/rss+xml\
application/xhtml+xml\
应用程序/xml\
应用程序/vnd.ms-fontobject\
应用程序/x-font-ttf\
字体/开放式\
图像/x图标\
image/svg+xml\
文本/css\
文本/html\
文本/纯文本\
文本/x组件\
文本/xml
当我查看SVG文件的响应头时,似乎没有GZip压缩。 当我通过Google PageSpeed Insights分析页面时,情况也是如此


有人能帮我吗?

使用
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>

标题集内容编码压缩
#标题集内容编码x压缩
是一个独立的项目

当使用编码进行响应时,Apache将使用客户端请求的任何形式(即x-foo或foo)。如果客户端没有特别请求特定的表单,Apache将使用AddEncoding指令给出的表单。长话短说,对于这两种特定的编码,您应该始终使用x-gzip和x-compress。更近期的编码,例如deflate,应该在不使用x-的情况下指定

参考资料


gzip适用于任何其他类型的文件类型吗?是的。我还注意到附加“Vary:Accept Encoding”(仅对svg文件)也会失败。我想知道为什么…我做了进一步的测试,发现每一个图像/任何文件都不是gzip。我还没有弄清楚到底是怎么回事。最后但并非最不重要的一点是,我的主机提供商告诉我没有什么有用的…@OpaleGR压缩一些类似jpg的文件是没有意义的。是的,压缩jpg文件是没有意义的,但是我真的需要gzip image/svg+xml和image/x-icon,因为我的网站在WebGetest和Google Pagespeed Insights上的分数受这种压缩的影响。我刚刚尝试了你的代码,但它不起作用。我会尽力给你更多我想要的