Rails Rails3.2 为什么 production 下不能使用 zip 压缩

return · 2018年05月23日 · 最后由 luikore 回复于 2018年05月26日 · 1485 次阅读

遇到一个很奇怪的问题,希望各位大牛帮解惑,非常感谢

我的 Rails 版本比较低 3.2,我想把一些静态 JS 通过 ZIP 进行压缩 application.rb 中配置如下

config.middleware.insert_before ActionDispatch::Static, Rack::Deflater

在 development 环境下 JS 可以 zip 压缩

Cache-Control:no-store, must-revalidate, private, max-age=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:286241
Content-Type:application/javascript
Date:Wed, 23 May 2018 07:34:38 GMT
Last-Modified:Tue, 22 May 2018 01:22:34 GMT
Server:WEBrick/1.3.1 (Ruby/1.9.3/2013-11-22)
Set-Cookie:__profilin=p%3Dt; path=/
Set-Cookie:__profilin=p%3Dt; path=/
Set-Cookie:__profilin=p%3Dt; path=/
Vary:Accept-Encoding

但是在 production 环境下始终没有效果:

Accept-Ranges:bytes
Connection:keep-alive
Content-Length:1006540
Content-Type:application/javascript; charset=utf-8
Date:Wed, 23 May 2018 07:37:28 GMT
ETag:"5afec9cd-f5bcc"
Last-Modified:Fri, 18 May 2018 12:40:45 GMT
Server:nginx/1.10.1

请问有好的解决方法吗?非常感谢!

不应该通过 nginx 来实现吗?

执行 rake assets:precompile 没有生成 .gz 的资源包么?

w7938940 回复

rails 在编译静态资源的时候会预生成 gzip 格式的资源文件,在 nginx 简单配置一下就可以直接使用,不需要 nginx 来压缩,节约 cpu 资源,加快响应速度

这是以前很老的项目,没有使用 asset,静态资源都是放在 public 下面,有好的处理方法吗?

nginx 如何实现?

首先这是 gzip 压缩,不是 zip 压缩。

其次 production 应该由 web server 负责静态文件。

nginx 如何实现?大多数发行版安装的 nginx 就已经配置成压缩了,具体可以读各大发行版 nginx 包里的默认配置。

因为静态文件在 production 下不通过 Rails 服务。你得配置你的 web server (apache? nginx?) 而不是 Rack middleware

需要 登录 后方可回复, 如果你还没有账号请 注册新账号