获取NGINX来提供.gz压缩资产文件 [英] Get NGINX to serve .gz compressed asset files
问题描述
Rails 3.1有一个方便的系统,可以将文件压缩为.gz文件.但是,相反,我所做的是将所有使用asset: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 (以通过gzip标头提供后端响应)
2) Enable this option gzip on (to serve back-end response with gzip header)
3)使用 gzip_static打开来设置资产位置 (以直接投放 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和资产的最后修改计算
4) Prevent of etag generation and last-modify calculation for assets
5)打开右侧的缓存"控件以缓存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屋!