在rails 3.2中使用nginx而不是rails来提供static_assets

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

我可以通过将production.rb更改为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=#>, "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 `' /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 `' 

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

通过更改location线来解决此问题:

location ~ ^/(system|assets)/ {

对此:

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

这是因为资产位于“spree”目录内的公共根目录中。 “system”是典型Paperclip上传的地方,资产是Rails资产。