在 rails 3.2 中使用 nginx 而不是 rails 提供 static_assets [英] Serving static_assets with nginx instead of rails in rails 3.2

查看:49
本文介绍了在 rails 3.2 中使用 nginx 而不是 rails 提供 static_assets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近部署了一个基于 SpreeCommerce 平台的网站.我正在使用 nginx 并希望 nginx 为我的静态资产提供服务.我遇到的问题是,当用户在网站上上传产品的新照片时,图像似乎正确保存(文件位于服务器上),但它们没有提供给浏览器.

I recently deployed a site based on the SpreeCommerce platform. I'm using nginx and would like nginx to serve my static assets. The problem I am having is that when a user uploads new photos for a product on the website the images appear to save correctly (the files are located on the server) but they do not get served to the browser.

我可以通过将我的 production.rb 更改为读取 config.serve_static_assets = true 而不是 false 并将图片重新上传到现有产品来临时解决此问题.但这有轨道服务资产而不是 nginx - 这不是我想要发生的.

I can resolve this issue temporarily by changing my production.rb to read config.serve_static_assets = true instead of false and re-uploading pictures to existing products. But this has rails serving the assets instead of nginx - not what I want to have happen.

这是我当前的 nginx 配置文件:

user spree;

worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    multi_accept on;
}

http {
    types_hash_bucket_size 512;
    types_hash_max_size 2048;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;


    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    gzip_proxied any;
    gzip_min_length 500;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

然后是我的/etc/nginx/sites-enabled/[我的应用程序名称]:

upstream 50.57.75.156 {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  # for UNIX domain socket setups:
  server unix:/tmp/angel_wood.socket fail_timeout=0;
}

server {
    # if you're running multiple servers, instead of "default" you should
    # put your main domain name here
    listen 80 default;

    # you could put a list of other domain names this application answers
    server_name 50.57.75.156;

    root /home/spree/angel_wood/current/public;
    access_log /var/log/nginx/50.57.75.156_access.log;
    rewrite_log on;

    location / {
        #all requests are sent to the UNIX socket
        proxy_pass  http://50.57.75.156;
        proxy_redirect     off;

        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_set_header   X-Forwarded-Proto  $scheme;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }

    # if the request is for a static resource, nginx should serve it directly
    # and add a far future expires header to it, making the browser
    # cache the resource and navigate faster over the website
    # this probably needs some work with Rails 3.1's asset pipeline
    location ~ ^/(system|assets)/  {
      root /home/spree/angel_wood/current/public;
      expires max;
      break;
    }
}

另外,供参考 - 以下是在服务器上保存图像时的日志:

Started GET "/admin/products/basket/images/new?authenticity_token=y5rw7G9ihZ6aFQa8tzC1q07DFyTW0f9iJ7MTxGxVZ5k%3D" for 24.155.210.182 at 2013-02-17 01:00:26 +0000
Processing by Spree::Admin::ImagesController#new as */*
  Parameters: {"authenticity_token"=>"y5rw7G9ihZ6aFQa8tzC1q07DFyTW0f9iJ7MTxGxVZ5k=", "product_id"=>"basket"}
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/images/_form.html.erb (2.7ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/images/new.html.erb (6.3ms)
Completed 200 OK in 19ms (Views: 7.0ms | ActiveRecord: 2.2ms)
Started POST "/admin/products/basket/images" for 24.155.210.182 at 2013-02-17 01:00:43 +0000
Processing by Spree::Admin::ImagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"y5rw7G9ihZ6aFQa8tzC1q07DFyTW0f9iJ7MTxGxVZ5k=", "image"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x00000007b822f0 @original_filename="basket3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[attachment]\"; filename=\"basket3.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130217-965-39199t>>, "viewable_id"=>"1", "alt"=>"basket 3"}, "button"=>"", "product_id"=>"basket"}
Command :: identify -format %wx%h '/tmp/stream20130217-965-1fn7d3c.jpg[0]'
Command :: convert '/tmp/stream20130217-965-1fn7d3c.jpg[0]' -resize "48x48>" -strip -auto-orient '/tmp/stream20130217-965-1fn7d3c20130217-965-wmlyai'
Command :: identify -format %wx%h '/tmp/stream20130217-965-1fn7d3c.jpg[0]'
Command :: convert '/tmp/stream20130217-965-1fn7d3c.jpg[0]' -resize "x190" -crop "190x190+31+0" +repage -strip -auto-orient '/tmp/stream20130217-965-1fn7d3c20130217-965-13z6pr7'
Command :: identify -format %wx%h '/tmp/stream20130217-965-1fn7d3c.jpg[0]'
Command :: convert '/tmp/stream20130217-965-1fn7d3c.jpg[0]' -resize "575x575>" -strip -auto-orient '/tmp/stream20130217-965-1fn7d3c20130217-965-znyfd4'
Command :: identify -format %wx%h '/tmp/stream20130217-965-1fn7d3c.jpg[0]'
Command :: convert '/tmp/stream20130217-965-1fn7d3c.jpg[0]' -resize "600x600>" -strip -auto-orient '/tmp/stream20130217-965-1fn7d3c20130217-965-pyrv7a'
Command :: identify -format %wx%h '/tmp/stream20130217-965-1fn7d3c.jpg[0]'
[paperclip] Saving attachments.
[paperclip] saving /home/spree/angel_wood/releases/20130216235942/public/spree/products/5/original/basket3.jpg
[paperclip] saving /home/spree/angel_wood/releases/20130216235942/public/spree/products/5/mini/basket3.jpg
[paperclip] saving /home/spree/angel_wood/releases/20130216235942/public/spree/products/5/small/basket3.jpg
[paperclip] saving /home/spree/angel_wood/releases/20130216235942/public/spree/products/5/product/basket3.jpg
[paperclip] saving /home/spree/angel_wood/releases/20130216235942/public/spree/products/5/large/basket3.jpg
Redirected to http://50.57.75.156/admin/products/basket/images
Completed 302 Found in 4334ms (ActiveRecord: 5.1ms)
Started GET "/admin/products/basket/images" for 24.155.210.182 at 2013-02-17 01:00:47 +0000
Processing by Spree::Admin::ImagesController#index as HTML
  Parameters: {"product_id"=>"basket"}
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_product_sub_menu.html.erb (2.6ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_product_tabs.html.erb (2.8ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/images/index.html.erb within spree/layouts/admin (38.1ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_translations.html.erb (1.8ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_routes.html.erb (1.2ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_head.html.erb (5.1ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_alert.html.erb (0.0ms)
  Rendered /home/spree/angel_wood/shared/bundle/ruby/1.9.1/gems/spree_core-1.3.2/app/views/spree/admin/shared/_tabs.html.erb (2.1ms)
Completed 200 OK in 62ms (Views: 43.2ms | ActiveRecord: 9.5ms)

以及服务器尝试显示图像时的错误:

ActionController::RoutingError (No route matches [GET] "/spree/products/5/mini/basket3.jpg"):
  actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.4) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.4) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.4) lib/rack/lock.rb:15:in `call'
  rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
  rack-cache (1.2) lib/rack/cache/context.rb:245:in `fetch'
  rack-cache (1.2) lib/rack/cache/context.rb:185:in `lookup'
  rack-cache (1.2) lib/rack/cache/context.rb:66:in `call!'
  rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
  railties (3.2.11) lib/rails/engine.rb:479:in `call'
  railties (3.2.11) lib/rails/application.rb:223:in `call'
  railties (3.2.11) lib/rails/railtie/configurable.rb:30:in `method_missing'
  unicorn (4.6.0) lib/unicorn/http_server.rb:552:in `process_client'
  unicorn (4.6.0) lib/unicorn/http_server.rb:632:in `worker_loop'
  unicorn (4.6.0) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
  unicorn (4.6.0) lib/unicorn/http_server.rb:142:in `start'
  unicorn (4.6.0) bin/unicorn_rails:209:in `<top (required)>'
  /home/spree/angel_wood/shared/bundle/ruby/1.9.1/bin/unicorn_rails:23:in `load'
  /home/spree/angel_wood/shared/bundle/ruby/1.9.1/bin/unicorn_rails:23:in `<main>'

关于向 nginx 发出指令以使其为我的资产提供服务,我有些不明白.有没有人看到我在这里做错了什么?

There is something I don't understand about giving instructions to nginx to have it serve my assets. Does anyone see what I'm doing wrong here?

推荐答案

此问题已通过从以下内容更改 location 行来解决:

This problem was remedied by changing the location line from this:

location ~ ^/(system|assets)/{

为此:

location ~ ^/(spree|system|assets)/{

这是因为资产位于spree"目录内的公共根目录下.系统"是典型的回形针上传的地方,而资产则是用于 Rails 资产.

This is because the assets are located at the public root inside the "spree" directory. "system" is where typical Paperclip uploads go, and assets are for the Rails assets.

这篇关于在 rails 3.2 中使用 nginx 而不是 rails 提供 static_assets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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