如何使Apache gzip压缩工作?

如何使Apache gzip压缩工作?,apache,.htaccess,gzip,mod-deflate,Apache,.htaccess,Gzip,Mod Deflate,我无法让我的站点使用gzip压缩 我最近在css-tricks.com上看了Chris Coyier的这篇文章。在视频中,他谈到了启用gzip压缩使网站运行更快 按照他的指示,我通过html5boilerplate.com链接到github,从他们的.htaccess文件中复制了gzip压缩代码,粘贴到我自己的文件中,并上传到我的网站 我已经通过gzipwtf.com对它进行了测试,但它似乎不起作用。有人能帮我吗 我的.htaccess文件如下所示: # --------------------

我无法让我的站点使用gzip压缩

我最近在css-tricks.com上看了Chris Coyier的这篇文章。在视频中,他谈到了启用gzip压缩使网站运行更快

按照他的指示,我通过html5boilerplate.com链接到github,从他们的.htaccess文件中复制了gzip压缩代码,粘贴到我自己的文件中,并上传到我的网站

我已经通过gzipwtf.com对它进行了测试,但它似乎不起作用。有人能帮我吗

我的.htaccess文件如下所示:

# ----------------------------------------------------------------------
# Trim www
# ----------------------------------------------------------------------

RewriteEngine On
RewriteCond %{HTTP_HOST} !^orbitprint.com$ [NC]
RewriteRule ^(.*)$ http://orbitprint.com/$1 [L,R=301]

# ----------------------------------------------------------------------
# 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/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  application/xhtml+xml \
                                  application/xml \
                                  font/opentype \
                                  image/svg+xml \
                                  image/x-icon \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
  </IfModule>

</IfModule>
#----------------------------------------------------------------------
#修剪www
# ----------------------------------------------------------------------
重新启动发动机
重写cond%{HTTP_HOST}^orbitprint.com$[NC]
重写规则^(.*)$http://orbitprint.com/$1[L,R=301]
# ----------------------------------------------------------------------
#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\
应用程序/vnd.ms-fontobject\
应用程序/x-font-ttf\
application/xhtml+xml\
应用程序/xml\
字体/开放式\
image/svg+xml\
图像/x图标\
文本/css\
文本/html\
文本/纯文本\
文本/x组件\
文本/xml
试试这个:

####################
# GZIP COMPRESSION #
####################
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip

您的.htaccess应该运行正常;它取决于四个不同的Apache模块(每个指令一个)。我猜是以下情况之一:

  • 您的Apache服务器没有安装和运行mod_filter、mod_deflate、mod_头和/或mod_setenif模块。如果您可以访问服务器配置,请检查
    /etc/apache2/httpd.conf
    (以及相关的Apache配置文件);否则,您可以通过apache2handler部分下的
    phpinfo()
    查看加载了哪些模块(请参见附图);(EDIT)或者,您可以打开一个终端窗口并发出命令
    sudo apachectl-M
    ,该命令将列出加载的模块

  • 如果出现http 500内部服务器错误,则可能不允许您的服务器使用.htaccess文件

  • 您试图加载一个PHP文件,该文件发送自己的头(覆盖Apache的头),从而“混淆”浏览器

在任何情况下,您都应该仔细检查服务器配置错误日志,看看哪里出了问题。为了确保这一点,请尝试使用Apache文档中建议的最快方式:

AddOutputFilterByType DEFLATE text/html text/plain text/xml
然后尝试加载一个大的文本文件(最好先清理缓存)

编辑)如果需要的模块存在(在Apache模块目录中),但未加载, 只需编辑/etc/apache2/httpd.conf并为它们中的每一个添加一个指令

如果没有所需的模块(既没有加载,也没有在apachemodules目录中),我担心唯一的选择是重新安装Apache(完整版本)


最好按照下面的代码片段实现它

只需将下面的内容粘贴到
.htaccess
文件中,然后使用:,和检查性能

#启用GZIP
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
浏览器匹配^Mozilla/4 gzip纯文本/html
浏览器匹配^Mozilla/4\.0[678]无gzip
BrowserMatch\bMSIE!没有gzip!仅限gzip文本/html
#过期标头-2678400s=31天
过期于
ExpiresDefault“访问加1秒”
ExpiresByType text/html“访问加7200秒”
ExpiresByType image/gif“访问时间加2678400秒”
ExpiresByType图像/jpeg“访问时间加2678400秒”
ExpiresByType image/png“访问时间加2678400秒”
ExpiresByType文本/css“访问加518400秒”
ExpiresByType文本/javascript“访问时间加2678400秒”
ExpiresByType应用程序/x-javascript“访问时间加2678400秒”
#缓存头
#将指定的文件缓存31天
标题集缓存控制“最大年龄=2678400,公共”
#将HTML文件缓存几个小时
标题集缓存控制“最大年龄=7200,专用,必须重新验证”
#缓存PDF一天
标题集缓存控制“最大年龄=86400,公共”
#缓存Javascripts 31天
标题集缓存控制“最大年龄=2678400,专用”

使用相同的.htaccess配置遇到此问题。我意识到我的服务器以
text/javascript
的形式提供javascript文件,而不是
application/javascript
。一旦我将
text/javascript
添加到
AddOutputFilterByType
声明中,gzip就开始工作了


至于为什么javascript被用作
text/javascript
:在我的root.htaccess文件的顶部有一个
AddType'text/javascript'js
声明。在删除它(它是错误添加的)之后,javascript开始充当
应用程序/javascript

在我的例子中,只有这一行起作用

SetOutputFilt
# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>

# Expires Headers - 2678400s = 31 days
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7200 seconds"
  ExpiresByType image/gif "access plus 2678400 seconds"
  ExpiresByType image/jpeg "access plus 2678400 seconds"
  ExpiresByType image/png "access plus 2678400 seconds"
  ExpiresByType text/css "access plus 518400 seconds"
  ExpiresByType text/javascript "access plus 2678400 seconds"
  ExpiresByType application/x-javascript "access plus 2678400 seconds"
</ifmodule>

# Cache Headers
<ifmodule mod_headers.c>
  # Cache specified files for 31 days
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
  Header set Cache-Control "max-age=2678400, public"
  </filesmatch>
  # Cache HTML files for a couple hours
  <filesmatch "\.(html|htm)$">
  Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  # Cache PDFs for a day
  <filesmatch "\.(pdf)$">
  Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  # Cache Javascripts for 31 days
  <filesmatch "\.(js)$">
  Header set Cache-Control "max-age=2678400, private"
  </filesmatch>
</ifmodule>
  # Compress HTML File, CSS File, JavaScript File, Text File, XML File and Fonts
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/x-httpd-php
    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 font/otf
    AddOutputFilterByType DEFLATE font/ttf
<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>
<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>

<IfModule mod_deflate.c>
# Insert filters
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/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml

# Drop problematic browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>