如何配置mod_deflate以提供使用assets准备的gzip压缩资源:precompile

jro*_*osw 11 apache mod-deflate ruby-on-rails-3.1 asset-pipeline

运行资产时:预编译rake任务,会创建应用程序资产的gzip压缩版本.根据资产管道的Rails指南,您可以配置Web服务器(在我的情况下是Apache 2.2)来提供这些预压缩文件,而不是让Web服务器完成工作.

我无法弄清楚的是如何配置mod_deflate以便提供这些文件而不是双重压缩然后提供?

我通过httpd.conf启用了mod_deflate:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Run Code Online (Sandbox Code Playgroud)

我已经将rails指南上的代码转换为公共/资产中的.htaccess:

# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.

Header unset Last-Modified
Header unset ETag
FileETag None

# RFC says only cache for 1 year

ExpiresActive On
ExpiresDefault "access plus 1 year"

# Serve gzipped versions instead of requiring Apache to do the work

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+) $1.gz [L]

# without it, Content-Type will be "application/x-gzip"

<FilesMatch .*\.css.gz>
    ForceType text/css
</FilesMatch>

<FilesMatch .*\.js.gz>
    ForceType text/javascript
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

任何想法如何正确设置?

sbu*_*ler 24

首先,您不希望mod_deflate在此处运行.所以在你的资产.htaccess文件中添加:

SetEnv no-gzip
Run Code Online (Sandbox Code Playgroud)

这应该关闭资产的mod_deflate.

其次,我讨厌不同意铁路人员,但我认为他们的资产存在一些不足.htaccess配方.顶部是好的,但对于RewriteEngine而言,我有:

RewriteEngine on
# Make sure the browser supports gzip encoding before we send it
RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b
RewriteCond %{REQUEST_URI} .*\.(css|js)
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+) $1.gz [L]

# without it, Content-Type will be "application/x-gzip"
# also add a content-encoding header to tell the browser to decompress

<FilesMatch \.css\.gz$>
    ForceType text/css
    Header set Content-Encoding gzip
</FilesMatch>

<FilesMatch \.js\.gz$>
    ForceType application/javascript
    Header set Content-Encoding gzip
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)

  • 此外,您可能应该建议使用`application/javascript`而不是`text/javascript`.请参阅*脚本媒体类型*上的[RFC4329](https://tools.ietf.org/html/rfc4329). (3认同)