如何让Apache gzip压缩工作?

dai*_*hop 55 apache .htaccess gzip mod-deflate

我无法让我的网站使用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>
Run Code Online (Sandbox Code Playgroud)

Ous*_*ama 92

试试这个 :

####################
# 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
Run Code Online (Sandbox Code Playgroud)

  • 什么是Vary标题?有必要吗? (8认同)
  • @Oussama是否可以解释每条线的作用? (6认同)
  • @jerryb - 这是因为他从互联网上复制粘贴了他的答案... https://varvy.com/pagespeed/enable-compression.html (3认同)
  • "Header附加Vary User-Agent env =!dont-vary"对我来说看起来很粗略.除非您向不同的用户代理提供不同的内容,否则您不应该由用户代理"改变".否则它绝对会破坏您的缓存命中率.基本上,Vary头所做的是允许您指示Cache将"Vary"头中命名的请求头的值视为应该被视为可缓存请求键的一部分的值,有点像它的一部分查询args.关于这一行的另一个讨论:http://stackoverflow.com/questions/970798/what-does-this-configuration-in-apache-mean (2认同)

Umu*_* D. 37

最好像下面的代码段一样实现它.

只需将下面的内容粘贴到您的.htaccess文件中,然后使用以下方法检查效果:Google PageSpeed,Pingdom ToolsGTmetrics.

# 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>
Run Code Online (Sandbox Code Playgroud)


Pao*_*fan 11

你的.htaccess应该运行得很好; 它取决于四个不同的Apache模块(每个<IfModule>指令一个).我想其中一个:

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

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

  • 您正在尝试加载发送自己的标头的PHP文件(覆盖Apache的标头),从而"混淆"浏览器.

在任何情况下,您都应该仔细检查您的服务器配置错误日志,看看出了什么问题.只是可以肯定,尝试使用建议的最快方法这里在Apache的文档:

AddOutputFilterByType DEFLATE text/html text/plain text/xml
Run Code Online (Sandbox Code Playgroud)

然后尝试加载一个大文本文件(最好先清理你的缓存).

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

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

phpinfo()apache2handler部分


小智 7

首先转到apache/bin/conf/httpd.conf并确保启用了mod_deflate.so.

然后转到.htaccess文件并添加以下行:

SetOutputFilter DEFLATE

这应该输出所有作为gzip服务的内容,我已经尝试过它的工作原理.