11

assets:precompile 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

そして、Rails ガイドのコードを public/assets の .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>

これを適切に設定する方法はありますか?

4

1 に答える 1

25

まず、ここで mod_deflate を動作させたくありません。したがって、アセットの .htaccess ファイルに次を追加します。

SetEnv no-gzip

これにより、アセットの mod_deflate がオフになります。

2 つ目は、Rails 関係者の意見に反対するのは嫌いですが、Rails のアセットの .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>
于 2011-09-22T06:58:15.477 に答える