Apache gzip 配置

Apache gzip 配置

我的服务器上安装了 apache 2.2.11,并且我一直在尝试根据 yslow firefox 插件的建议减少我的网站页面的加载时间。

我已经配置了 gzip 和 etag,并且它的一些其他建议都很好,但我注意到在我的 css 文件标题中 etag 被附加了文本 gzip。

有人知道为什么以及如何解决这个问题?

来自我的服务器的标头
日期:2009 年 6 月 7 日星期日 10:40:57 GMT
服务器 Apache/2.2.11 (Fedora)
最后修改时间:2009 年 5 月 31 日星期日 15:06:38 GMT
Etag“3b4-46b36a802bb80”-gzip
接受范围字节
缓存控制最大年龄=2592000
到期时间:2009 年 7 月 7 日星期二 10:40:57 GMT
改变接受编码
内容编码 gzip
内容长度 530
关闭连接
内容类型 text/css

我的托管包上的相同代码使用旧版本的 apache,没有出现相同问题。这可能是 apache 的一个错误吗?

来自我的托管包的标头
日期:2009 年 6 月 7 日星期日 10:48:26 GMT
服务器 Apache/2.0.63 (FreeBSD) mod_python/3.3.1 Python/2.5.1 PHP/5.2.6 带有 Suhosin-Patch mod_fastcgi/2.4.6 mod_ssl/2.0.63 OpenSSL/0.9.7e-p1 DAV/2 mod_perl/2.0.4 Perl/v5.8.8
最后修改时间:2009 年 2 月 21 日星期六 13:54:52 GMT
Etag“3b4-1d104300”
接受范围字节
缓存控制最大年龄=2592000
到期时间:2009 年 7 月 7 日星期二 10:48:26 GMT
改变接受编码
内容编码 gzip
内容长度 530
关闭连接
内容类型 text/css

答案1

这是一个理想的 .htaccess,它既可以压缩,又可以设置合适的过期标头。

# 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

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

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  <FilesMatch "\\.(css)$">
    Header set Cache-Control "max-age=604800, public"
  </FilesMatch>
  <FilesMatch "\\.(js)$">
    Header set Cache-Control "max-age=216000, private"
  </FilesMatch>
  <FilesMatch "\\.(x?html?|php)$">
    Header set Cache-Control "max-age=600, private, must-revalidate"
  </FilesMatch>
</IfModule>

<IfModule mod_headers.c>
  Header unset ETag
</IfModule>
FileETag None

<IfModule mod_headers.c>
  Header unset Last-Modified
</IfModule>

以下文章介绍了它的功能并讨论了压缩:

http://www.samaxes.com/2009/01/06/more-on-compressing-and-caching-your-site-with-htaccess/

希望有所帮助。

答案2

Apachemod_deflate为每个实体创建唯一的 Etag,因为它们标识了 URL 的特定实体变体。每个协商变体都需要有唯一的 ETag。因为mod_deflate它就像将编码添加到已计算的 ETag 一样简单。

一种解决方法是从 Etag 中删除编码:

<Location /js>
  RequestHeader  edit "If-None-Match" "^(.*)-gzip$" "$1"
  Header  edit "ETag" "^(.*[^g][^z][^i][^p])$" "$1-gzip"
</Location>

如果您使用带有mod_deflate模块的 Apache 2.5,则可以使用该指令DeflateAlterETag指定在压缩响应时应如何改变 ETag hader。

DeflateAlterETag AddSuffix|NoChange|Remove

来源:https://httpd.apache.org/docs/trunk/mod/mod_deflate.html#deflatealteretag

博客文章建议完全删除 Etags 并依赖标题Cache-Control

要做到这一点httpd.conf

<IfModule mod_headers.c>
    Header unset ETag
</IfModule>

FileETag None

请注意,如果被 gzip 压缩的实体mod_deflate仍然带有与普通实体相同的 ETag,这可能会导致 ETag 感知代理缓存不一致。

更多信息请点击这里:

答案3

您能详细说明一下您是如何配置 gzip 的吗?例如,您是否在使用 mod_deflate?您能发布 httpd.conf 中的片段吗,尤其是 AddOutputFilterByType 字段?我按照以下指示针对我的特定托管服务提供商进行了操作,但您的托管服务提供商的列表中可能没有 text/css:

http://kb.mediatemple.net/questions/1567/Compressing+web+pages+with+mod_deflate

相关内容