我正在尝试优化我的Rails 4应用程序通过服务GZipped资产,而不是定期编译资产。 GZip压缩在本Rails指南的第4.1.2节中描述: http : //guides.rubyonrails.org/asset_pipeline.html
Rails资产pipe道确实在预编译之后生成了我的资源的gzip版本,我可以在我的应用程序公用文件夹中的服务器文件系统上看到这些。
但是,在检查networking活动时,我的网页会回退到提供未压缩的资源,而不是gzip版本。 这导致我认为我的Web服务器没有正确configuration来提供压缩资源。 我正在使用NGINX和我的导轨应用程序前面的乘客模块。
我首先尝试在Rails Asset Pipeline指南中使用推荐的NGINXconfiguration,将以下内容添加到我的configuration文件中:
location ~ ^/(assets)/ { root /path/to/public; gzip_static on; # to serve pre-gzipped version expires max; add_header Cache-Control public; }
然后我再次确认http_gzip_static_module确实是用我的NGINX安装编译的:
/opt/nginx/sbin/nginx -V # --with-http_gzip_static_module
随着NGINXconfiguration更新,并确认http_gzip_static_module是在我的安装,然后我玩了我的production.rb文件config.serve_static_files选项:
config.serve_static_files = true config.serve_static_files = false config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
这三个设置都导致我的服务器回落到常规的压缩资产。
最后,我尝试安装rack-zippy gem( https://github.com/eliotsykes/rack-zippy ),它优先考虑提供静态的压缩资源,否则就退回到常规的压缩资产。 这也没有工作,这可能意味着我的NGINXconfiguration需要修改。
任何帮助非常感谢!
这个答案解决了我的: https : //stackoverflow.com/a/40824720/667335
我在config / production.rb中错过了这个
config.middleware.insert_before(Rack::Sendfile, Rack::Deflater) # Compress JavaScripts and CSS. config.assets.compress = true config.assets.js_compressor = Uglifier.new(mangle: false)
它看起来像链轮不再为你做这个。 它看起来像是在缺少gzip版本的CSS和JS资产的答案。