ruby-on-rails-3 - 如何从 nginx 而不是 unicorn 提供缓存的 ruby​​ 应用程序?

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

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

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

我正在研究这方面的问题,我不太了解服务器配置,所以我希望你们能给我一些建议,那就太好了!

谢谢!

最佳答案

我假设你的意思是我如何从 nginx 而不是 Unicorn 提供我的静态 Assets

我刚刚解决了这个问题,这是我的 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 进行不区分大小写的正则表达式比较。此外,您不需要像在其他语言中那样转义反斜杠。

这是有关 Nginx 的文档:http://wiki.nginx.org/HttpCoreModule#location

关于ruby-on-rails-3 - 如何从 nginx 而不是 unicorn 提供缓存的 ruby​​ 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080267/

相关文章:

php - mysqli_insert_id 是否有可能在高流量应用程序中返回错误的 id?

ruby-on-rails - Rails 市场支付处理

ruby-on-rails - 无法安装 mysql2 gem ...( Homebrew 软件上的附带内容也是如此)

java - 如果用户尚未安装 JRE,我如何提示用户安装 JRE?

java - 如何在tomcat docker容器中部署多个war?

ruby-on-rails - 如何在 Rails 3 事件记录中链接查询

ruby-on-rails - 进行渲染时,rails content_for 不会被执行?

python - pip推荐结构

java - 构建具有大量数据通信的系统的最佳方法是什么?