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.
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.
Here are my current nginx config files:
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/*;
}
Then my /etc/nginx/sites-enabled/[my application's name]:
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;
}
}
Also, for reference - here are the logs when an image is saved on the server:
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)
And the error when the server attempts to display an image:
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>'
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?
This problem was remedied by changing the
location
line from this:location ~ ^/(system|assets)/ {
To this:
location ~ ^/(spree|system|assets)/ {
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.