Ruby on rails 压缩rails资产和nginx gzip
如果我使用Ruby on rails 压缩rails资产和nginx gzip,ruby-on-rails,nginx,asset-pipeline,Ruby On Rails,Nginx,Asset Pipeline,如果我使用rake assets:precompile压缩了rails资产,那么我是否必须将nginx配置为压缩资产(gzip设置为on)?我是说这有意义吗?性能是更好还是更差?谢谢大家! 不,你没有。它们不是同一种压缩。当您运行rakeassets:precompile时,您真正要做的就是将一组文件合并到一个文件中,并将其转储到磁盘上。实际上,根据文件,它是两个文件: 预编译文件时,链轮还会创建gzip(.gz) 您的资产版本。Web服务器通常配置为使用 中等压缩比作为折衷方案,但由于预编译
rake assets:precompile
压缩了rails资产,那么我是否必须将nginx配置为压缩资产(gzip设置为on)?我是说这有意义吗?性能是更好还是更差?谢谢大家! 不,你没有。它们不是同一种压缩。当您运行rakeassets:precompile
时,您真正要做的就是将一组文件合并到一个文件中,并将其转储到磁盘上。实际上,根据文件,它是两个文件:
预编译文件时,链轮还会创建gzip(.gz)
您的资产版本。Web服务器通常配置为使用
中等压缩比作为折衷方案,但由于预编译
发生一次,链轮使用最大压缩比,因此
将数据传输的大小减少到最小。另一方面
另一方面,可以将web服务器配置为提供压缩内容
直接从磁盘,而不是压缩非压缩文件
他们自己
这对您来说很重要,因为如果您愿意,它允许您使用gzip,但并不强迫您这么做,这是真正的压缩(不仅仅是连接文件)减少了必须传输的数据量,而且以牺牲处理器能力(压缩和解压缩)为代价。根据页面大小和您(以及您的用户)的硬件,它可能会极大地改进您的站点。执行rake资产:预编译,并且您必须为send gzip版本的文件配置nginx,我使用此配置
user www-data www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events{
worker_connections 2048;
use epoll;
}
http{
include mime.types;
default_type application/octet-stream;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
server_tokens off;
server_name_in_redirect off;
ignore_invalid_headers on;
gzip off;
sendfile on;
upstream reverse-proxy{
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}
server{
listen 80;
server_name _;
root /home/www-data/my_website/public;
client_max_body_size 10M;
client_body_buffer_size 512k;
location ~ ^/assets/ {
gzip_static on;
add_header Cache-Control public;
expires 4w;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";
gzip_comp_level 6;
gzip_types application/x-javascript text/css text/html image/x-icon image/png image/jpeg image/gif;
}
location / {
try_files $uri @ruby;
}
location @ruby {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://reverse-proxy;
}
}
}
这是一个完整的配置(我正在为我的站点使用它): 一般配置 /opt/nginx/conf/nginx_host.conf的内容; 主机配置 对于服务资产:
是的,如果您想提高性能,您应该这样做 只需将以下块添加到站点配置中:
location ~ ^/(assets)/ {
root /path/to/public; # CHANGE THIS
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
更改配置中的根路径。这就是全部
文件中的建议™: 对我有效的是配置Nginx:
location ~ ^/(assets)/ {
gzip_static on;
}
然后在application.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)
nginx文档说我们不需要添加text/html,因为这是默认情况下完成的(在gzip_类型下)。使用基于位置的gzipping有一点很明显,这在443土地上是行不通的,对吗?我把它移到了http块,这样所有的东西都可以压缩。顺便说一句,你的过期时间也太棒了。我从未在https上测试过(因为我设置了侦听端口80,所以无法工作),我的过期时间是因为google page speed建议将其设置为4周。当运行
rake assets:precompile
时,不再创建Gzip版本:(
location ~ ^/(assets)/ {
root /path/to/public; # CHANGE THIS
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
location ~ ^/(assets)/ {
gzip_static on;
}
config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)
# Compress JavaScripts and CSS.
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(mangle: false)