apache - 如何配置 mod_deflate 以提供使用 Assets :precompile 准备的压缩 Assets

标签 apache ruby-on-rails-3.1 asset-pipeline mod-deflate

运行 assets:precompile rake 任务时,会创建应用程序资源的 gzip 版本。根据 Assets 管道的 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 guide 上的代码转换为 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>

任何想法如何正确设置?

最佳答案

首先,您不希望 mod_deflate 在这里操作。所以在你的 Assets .htaccess 文件中添加:

SetEnv no-gzip

这应该为您的 Assets 关闭 mod_deflate。

其次,我讨厌不同意 Rails 的人,但我认为他们的 Assets .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>

关于apache - 如何配置 mod_deflate 以提供使用 Assets :precompile 准备的压缩 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7509501/

相关文章:

java - 运行 gradle 项目时具有未经批准/未知许可证的文件

ruby-on-rails - Rails 3.2 以前在 words 方法中返回 html 内容

jquery-rails 生成旧的 jquery 版本

ruby-on-rails - Rails 3.1 使用 app/assets/images/subdirectory 下的图像

laravel - 如何将页面特定的 JavaScript 文件添加到 Laravel Assets 管道?

css - rails 应用程序 : pipeline asset not working as expected

php - .htaccess 将 php 重写为 php 文件

java - Apache POI XSLF 将图像替换为另一个图像

regex - 如何编写表达式以获取表达式后的所有内容,然后在 htaccess 中重写

ruby-on-rails - 设计异步,在 Rails 升级后抛出错误