优化Nginx的gzip_static配置让Rails的AssetPipeline的技持压缩gz传输

./configure –with-http_gzip_static_module

1
2
3
4
gzip  on;  
gzip_min_length  1024;  
gzip_buffers     4 8k;  
gzip_types       text/plain application/x-javascript text/css text/html application/xml; 

gzip_types:要gz的mine类型,据江湖传言text/html这个类型总会被压缩,不管指不指定!

1
2
3
4
5
location ~ ^/(assets)/  {  
  gzip_static on; # to serve pre-gzipped version  
  expires max;  
  add_header Cache-Control public;  
}  
1
2
3
4
5
6
7
8
9
10
[~] $ curl -I -H "Accept-Encoding: gzip,deflate,sdch" http://www.xxx.com/assets/application.css
HTTP/1.1 200 OK
Server: nginx
... ...
Content-Type: text/css
Content-Length: 17790
Connection: keep-alive
Content-Encoding: gzip #显示这个说明gz传递开启
... ...
Cache-Control: public

补充说明:

nginx会自动gz压缩!

  • 如一个在assets目标下的一个名为xxxx.css文件。当chrome请求的时候,nginx没有发现xxxx.css.gz那么nginx会先把xxxx.css压缩成xxxx.css.gz,然后发送给chrome!

  • Rails就提供了rake assets:precompile方案,把js,css,图片,都打开gz文件!让chrome请求的时候,nginx无须打压缩了,可以省占用CPU与内存了,也省带宽!

原文地址:http://dlj.bz/HRQmsD

Comments