如何从nginx而不是独角兽提供缓存的ruby应用程序?

时间:2012-01-31 13:24:46

标签: ruby-on-rails-3 deployment high-traffic

我有一个rails 3.0应用程序,它有很多流量,它通过Nginx和Unicorn的组合运行的应用程序。问题是独角兽和它的工作人员消耗了大量资源,并且由于我的应用程序的性质,从数据库中提取了大量记录,然后就像提供使用这些数据库记录生成的几乎静态文件一样

我想知道你是否可以生成这种静态文件,缓存它们,通过nginx而不是应用程序通过unicorn为它们提供服务,以便在1000次请求后使用更少的资源和重新加载缓存

我正在开始研究这个,我不知道很多服务器配置,所以我希望你们有任何建议,这会很棒!

谢谢!

1 个答案:

答案 0 :(得分:3)

我认为你的意思是如何从nginx而不是Unicorn

提供我的静态资产?

我刚解决了这个问题,这是我的nginx.conf

的片段
# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri @app;

# Set Far Future Cache on Static Assets
# All requests starting with /xyz/ where xyz is 
# one of the options below (~* == case insensitive)
location ~* ^/(images|javascripts|stylesheets)/ {
    # Per RFC2616 - 1 year maximum expiry
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
}

location @app { ... }

我正在使用Rails 3.0.10,所以我需要^/assets/之类的东西。 ~*指令告诉nginx进行一个区分敏感的reg-ex比较。此外,您不需要像在其他语言中那样转义反斜杠。

以下是关于该问题的Nginx文档:http://wiki.nginx.org/HttpCoreModule#location