0

最近、 SpreeCommerceプラットフォームに基づくサイトを展開しました。nginx を使用していますが、nginx で静的アセットを提供したいと考えています。私が抱えている問題は、ユーザーが製品の新しい写真を Web サイトにアップロードすると、画像は正しく保存されているように見えますが (ファイルはサーバー上にあります)、ブラウザーには提供されません。

production.rb を read に変更し、既存の製品に写真を再アップロードするconfig.serve_static_assets = trueことで、この問題を一時的に解決できます。falseしかし、これにはnginxの代わりにアセットを提供するレールがあります-私が望んでいることではありません。

これが私の現在の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 にアセットを提供するように指示することについて、理解できないことがあります。ここで私が間違っていることを誰かが見ていますか?

4

1 に答える 1

7

locationこの問題は、次の行を変更することで解決されました。

location ~ ^/(system|assets)/ {

これに:

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

これは、アセットが「spree」ディレクトリ内のパブリック ルートにあるためです。"system" は典型的な Paperclip のアップロード先で、assets は Rails アセット用です。

于 2013-02-17T23:50:21.623 に答える