I am trying to optimise my rails 4 application by serving GZipped assets instead of regular compiled assets. GZip compression is described in section 4.1.2 of this Rails guide: http://guides.rubyonrails.org/asset_pipeline.html
我试图通过提供GZipped资产而不是常规编译资产来优化我的rails 4应用程序。 Gzip压缩在本Rails指南的4.1.2节中描述:http://guides.rubyonrails.org/asset_pipeline.html
The rails asset pipeline has indeed produced gzipped versions of my assets after pre-compiling them, and I can see these on my server's filesystem in my app's public folder.
rails资产管道确实在预编译之后生成了我的资产的gzip压缩版本,我可以在我的应用程序公共文件夹中的服务器文件系统上看到这些版本。
However, my webpages fall back to serving the un-compressed assets instead of the gzipped version when inspecting the network activity. This had lead me to think that my web server is not configured correctly to serve the gzipped assets. I'm using NGINX and the passenger module in-front of my rails app.
但是,在检查网络活动时,我的网页会回退到提供未压缩的资源而不是gzip压缩版本。这导致我认为我的Web服务器未正确配置以提供gzip压缩资产。我在我的rails应用程序前面使用NGINX和乘客模块。
I've firstly tried using the recommended NGINX configuration in the Rails Asset Pipeline guide by adding the following to my config file:
我首先尝试在Rails Asset Pipeline指南中使用推荐的NGINX配置,将以下内容添加到我的配置文件中:
location ~ ^/(assets)/ {
root /path/to/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
I then double checked that the http_gzip_static_module was indeed compiled with my NGINX installation:
然后,我仔细检查了http_gzip_static_module确实是用我的NGINX安装编译的:
/opt/nginx/sbin/nginx -V # --with-http_gzip_static_module
With the NGINX config updated, and confirmation that http_gzip_static_module is in my installation, I then played about with the config.serve_static_files option in my production.rb file:
随着NGINX配置更新,并确认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?
These three settings all resulted in my server falling back to the regular compressed assets.
这三个设置都导致我的服务器回退到常规压缩资产。
Lastly, I tried installing the rack-zippy gem (https://github.com/eliotsykes/rack-zippy) - which prioritises serving the static gzipped assets and falls back to the regular compressed assets otherwise. This also didn't work, which probably means my NGINX configuration needs amending.
最后,我尝试安装rack-zippy gem(https://github.com/eliotsykes/rack-zippy) - 它优先提供静态gzip资源,然后回退到常规压缩资产。这也行不通,这可能意味着我的NGINX配置需要修改。
Any help much appreciated!
任何帮助非常感谢!
2 个解决方案
#1
2
This answer solved it for me: https://*.com/a/40824720/667335
这个答案为我解决了这个问题:https://*.com/a/40824720/667335
I was missing this in config/production.rb
我在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)
#2
0
It looks like Sprockets no longer does this for you. It looks like this was answered in missing gzip version of css and js assets.
看起来Sprockets不再适合你了。看起来这是在缺少gzip版本的css和js资产中得到了回答。
#1
2
This answer solved it for me: https://*.com/a/40824720/667335
这个答案为我解决了这个问题:https://*.com/a/40824720/667335
I was missing this in config/production.rb
我在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)
#2
0
It looks like Sprockets no longer does this for you. It looks like this was answered in missing gzip version of css and js assets.
看起来Sprockets不再适合你了。看起来这是在缺少gzip版本的css和js资产中得到了回答。