获取 NGINX 以提供 .gz 压缩资产文件 [英] Get NGINX to serve .gz compressed asset files

查看:41
本文介绍了获取 NGINX 以提供 .gz 压缩资产文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3.1 有一个方便的系统,可以将文件压缩成 .gz 文件.但是,我所做的是将使用 assets:precompile 创建的所有资产文件移动到静态 Web 服务器.这一切都有效,但如何让 nginx 正常提供 .gz 文件?

Rails 3.1 has a convenient system which can compress files into .gz files. However, instead what I've done is I've moved all the asset files that are created with assets:precompile to a static webserver. This all works, but how can I get nginx to serve the .gz files normally?

推荐答案

1) 确保您拥有 Nginx > 1.2.x(以正确修改头文件)并使用 --with-http_gzip_static_module 选项进行编译

1) ensure you have Nginx > 1.2.x (to proper headers modifications) and compile with --with-http_gzip_static_module option

2) 启用此选项 gzip on(使用 gzip 标头提供后端响应)

2) Enable this option gzip on (to serve back-end response with gzip header)

3) 使用 gzip_static on 设置资产位置(直接提供all.css.gz、all.js.gz文件)

3) Setup assets location with gzip_static on (to serve all.css.gz, all.js.gz files directly)

4) 防止资产的 etag 生成和 last-modify 计算

4) Prevent of etag generation and last-modify calculation for assets

5) 打开正确的 Cache-control 来缓存 SSL 服务的静态资产,除非它们会在浏览器关闭后过期

5) Turn on the right Cache-control to cache SSL served static assets, unless they will be expired once browser is closed

  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
      gzip_static on;
      expires     max;
      add_header  Cache-Control public;
      add_header  Last-Modified "";
      add_header  ETag "";
  }

如果您想获得完整的 Nginx 配置,您可以在 Github 上查看此要点.

if you would like to get full Nginx configuration, you can see this gist on Github.

open_file_cache 帮助您缓存:打开的文件描述符、它们的大小、修改时间和目录查找,这有助于文件系统的高负载.

open_file_cache helps you to cache: open file descriptors, their sizes, modification times and directory lookups, which is helpful for high load on the file system.

更新:如果您生活在边缘,请打开 SPDY 以增强 SSL 连接.

UPDATE: If you are living on the edge, turn on the SPDY to boost the SSL connection.

这篇关于获取 NGINX 以提供 .gz 压缩资产文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆