【问题标题】:Apache settings to send gzipped CSS/JS files to browser将 gzip 压缩的 CSS/JS 文件发送到浏览器的 Apache 设置
【发布时间】:2015-06-17 09:05:06
【问题描述】:

我使用预先组合的 gzip 压缩 CSS 和 JS 文件,因此服务器不会即时执行此操作。

所以在同一个文件夹中,我有 file.css(gzip 压缩版)、file.nozip.css(nogzip 压缩版)。然后根据浏览器是否接受压缩文件,发送正确的版本。

所以我在 .htaccess 中有以下内容:

RewriteEngine On

RewriteRule ^(.*)\.[0-9]+\.css$ $1.css [L]

#redirect Konqueror and "old browsers"
RewriteCond %{REQUEST_FILENAME} !\.nogzip\.css$
RewriteCond %{HTTP:Accept-encoding} !gzip [OR]
RewriteCond %{HTTP_USER_AGENT} Konqueror
RewriteRule ^(.*)\.css$ $1.nogzip.css [L]

<IfModule mod_headers.c>
    Header set Vary User-Agent
#set Content-Encoding for all css files
    <FilesMatch .*\.css$>
    Header set Content-Encoding: gzip
    Header set Cache-control: private
    </FilesMatch>
#drop Content-Encoding in case we send not gzipped file
    <FilesMatch .*\.nogzip\.css$>
    Header unset Content-Encoding
    </FilesMatch>
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault A36000000
</IfModule>

这种方法我以前在 Windows 和 Linux 服务器上都使用过很多次。一直运行良好。

但是,最近在开发另一个网站时,我遇到了浏览器无法将 gzipped 文件识别为 gzipped 的问题。

在本地主机上工作,这里是响应头:

Accept-Ranges:bytes
Cache-control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:39115
Content-Type:text/css
Date:Wed, 17 Jun 2015 11:27:28 GMT
ETag:"98cb-517998d9e690c"
Keep-Alive:timeout=5, max=100
Last-Modified:Wed, 03 Jun 2015 09:19:16 GMT
Server:Apache/2.4.12 (Win64) OpenSSL/1.0.1m PHP/5.6.9
Vary:User-Agent
X-Distributed-by:AHC

这是从生产服务器收到的标头(不工作 - 在浏览器中显示为压缩的 css 文件):

Accept-Ranges:bytes
Cache-control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:39099
Content-Type:text/css
Date:Wed, 17 Jun 2015 11:30:08 GMT
ETag:"98cb-517998d8fcd00-gzip"
Keep-Alive:timeout=5, max=99
Last-Modified:Wed, 03 Jun 2015 09:19:16 GMT
Server:Apache/2.4.10 (Debian)
Vary:User-Agent

唯一的区别是“X-Distributed-by:AHC”,但这几乎不是问题的原因。

还有什么要检查的想法吗?

【问题讨论】:

  • 我也有同样的问题。你解决了吗?

标签: apache http-headers debian gzip response-headers


【解决方案1】:

在情况 2 中,它强烈暗示 mod_deflate 由于 etag 对其进行了动态压缩:

ETag:"98cb-517998d8fcd00-gzip"

也许最好为例如设置 no-gzip征服者案?

【讨论】:

  • 所以它会压缩已经压缩过的内容?这也是我的想法,我通过以下方式进行了检查 - 我保存了内容并解压缩了文件并获得了无压缩版本,所以看起来压缩只应用了一次,没关系。我能够通过添加“Header set Content-Encoding: gzip”来解决问题,这解决了将 gzip 压缩版本发送到浏览器的问题,但是“Header unset Content-Encoding”不能解决无压缩文件时的情况要发送,您能否详细说明您建议的解决方案?
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多