ruby-on-rails - 使用 nginx 提供预编译 Assets

标签 ruby-on-rails ruby-on-rails-3 nginx

是否可以直接使用 nginx 提供预编译 Assets ?使用 Rails 动态提供 Assets 的速度要慢 20 倍(在我的 virtualbox 中为 4000 req/sec vs 200 req/sec)。

我想这可以通过 nginx.conf 中的一些重写规则来完成。然而,问题是这些文件名包含内容的 md5 哈希,所以我真的不明白可以用它做什么。

如果不可能的话,我不明白 Rails 3.1 Assets 管道的全部想法。以 x20 服务器负载为代价减少客户端带宽和页面加载时间?

有任何想法吗?

UPD:所以,我设法以某种方式设置了我的 nginx 和 Rails,当 一切在我的应用程序中以约 3500-4000 个请求/秒的速度提供服务。

首先,我添加了两个虚拟主机,其中一个用作另一个的缓存代理,发现 Assets 以我想要的速度(4k)提供服务。然后我将我的 Rails 应用程序与 memcached 连接起来(到目前为止没什么特别的,只有 application.rb 中的一行:ActionController::Base.cache_store = :mem_cache_store, "localhost")

然后我添加了 expires_in 1.hour, :public => true if !signed_in?; 之类的内容让我的 Controller 更改 Rails 内容的默认缓存策略,并为我的动态页面获得大约 500 个请求/秒的速度提升(在此之前它接近 200 个,而在我开始这一切之前大约是 50 个)。

现在,当我的 nginx 配置文件如下所示时:

nginx.conf:

...
proxy_cache_path  /tmp/blog keys_zone=one:8m max_size=1000m inactive=600m;
proxy_temp_path /tmp;
gzip  off;
include /opt/nginx/conf/sites-enabled/*;

启用网站/博客:
server {
        listen   8080;
        server_name  blindsight;

        root   /home/mike/rails/blog/public;
        rails_env production;

        # serve static content directly
        location ~* \.(ico|jpg|gif|png|swf|html)$ {
          if (-f $request_filename) {
            expires max;
            break;
          }
        }

        passenger_enabled on;

        location ~ /\.ht {
          deny  all;
        }
}

启用站点/主要:
server {

    listen   80;
    server_name  blindsight;

    location /authorize
    {
       proxy_pass_header Cookie;
       proxy_pass_header Set-Cookie;
       proxy_pass http://127.0.0.1:8080;
    }

    location /admin
    {
       proxy_pass_header Set-Cookie;
       proxy_pass_header Cookie;
       proxy_pass http://127.0.0.1:8080;
    }

    location / {
    root /home/mike/rails/blog/public;

        # All POST requests go directly
        if ($request_method = POST) {
          proxy_pass http://127.0.0.1:8080;
          break;
        }

    proxy_redirect off;
    proxy_pass_header Cookie;
    proxy_ignore_headers Set-Cookie;
    proxy_hide_header Set-Cookie;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cache one;
    proxy_cache_key blog$request_uri;
    proxy_cache_valid 200 302  5s;
    proxy_cache_valid 404      1m;
    proxy_pass http://127.0.0.1:8080;

    }

一切都像血腥的闪电一样快:)
谢谢你们。

最佳答案

从上面开始,我从互联网上收集了一些额外的信息:

对于 Rails 3.1:

location ~* ^/assets/ {
    # 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;
}

对于 Rails 3.0 使用
location ~* ^/(images|javascripts|stylesheets)/ {
    ... copy block from above ...
}

关于ruby-on-rails - 使用 nginx 提供预编译 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402278/

相关文章:

ruby-on-rails - 在 Ruby on Rails 中将 3 个数组组合成 Hash

javascript - Twitter Bootstrap 弹出窗口显示但看起来很奇怪 - Rails

WordPress nginx 无法创建目录 - 权限正确

Nginx proxy_pass 只能部分工作

ruby-on-rails - 使 Rails 表列属性只读

ruby-on-rails - 写入包含宏的现有 Excel .xls 文件

ruby-on-rails - PG::Result#clear 应该在你执行原始 SQL 之后被调用吗?

ruby-on-rails - :url, :action, 之间的 RoR 差异:form_for 中的方法

ruby-on-rails - 如何在rails 4.0中实现ajax进行简单的乘法运算?

redirect - 如何告诉nginx将网站的所有页面重定向到root?