JoeyB
JoeyB

Reputation: 49

Carrierwave + NGINX rails production images not displaying

I've deployed a rails app that allows a user to upload a photo and have it display on another page, fairly simple. I tested it in development, the image uploads to the public folder and displays properly. In production and deployed the image uploads to the server but isn't being rendered on the page.

404 errors for only the images I've uploaded, path looks like this:

 http://IP-OF-APP/uploads/blog/name-of-image.jpg

I read in another S.O. article mentioning setting a production config serve_static_files as true. This didn't fix the problem.

I thought it might be a server configuration with NGINX not picking up the uploads path here is my /sites-default/nginx.conf file.

upstream app {
  server unix: /home/deploy/MYAPPNAME/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name IP_ADDRESS_OF_SERVER;

  root /home/deploy/MYAPPNAME/public;

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://app;
    proxy_redirect off;
  }

  location ~ ^/(assets)/ {
    root /home/deploy/MYAPPNAME/shared/public
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location ~ ^/uploads/ {
    root /home/deploy/MYAPPNAME/shared/public;
    expires 24h;
    add_header Cache-Control public;
    break;
  }

  location ~ ^/(fonts|system)/favicon.ico/robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

I also thought it might be that the image isn't uploading to the server correctly so I SSH'd into the server and found the image uploaded living in:

home/deploy/MYAPPNAME/shared/public/uploads/blog/name-of-image.jpg

Also found what I assume to be the same image in:

home/deploy/MYAPPNAME/current/public/uploads/blog/name-of-image.jpg

My uploader looks like this:

class BlogUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  def store_dir
     'uploads/blog'
  end

  def extension_whitelist
     %w(jpg jpeg gif png)
  end

  process :resize_to_fit => [825, 825]

  #titles are validated to be unique
  def filename
     "#{model.title}"+".#{file.extension}" if original_filename.present?
  end
end

using

Rails 4.2.6
Ruby 2.3.0
Carrierwave 0.11.0

EDIT

All of the other static images, CSS and JS are displaying and rendering properly.

The NGINX error when rending the upload image produces this:

   2016/04/29 17:32:34 [error] 4993#0: *23 open()  "/home/deploy/MYAPPNAME/shared/public/assets/uploads/blog/name-of-image.jpg" fails (2: no such file or directory), client: *******, server: SERVER_IP, request: "GET /assets/uploads/blog/name-of-image.jpg HTTP/1.1", host: "SERVER_IP"

Upvotes: 1

Views: 1583

Answers (1)

Uzbekjon
Uzbekjon

Reputation: 11813

From your log:

User is requesting:

/assets/uploads/blog/name-of-image.jpg

Nginx is looking for the image in:

/home/deploy/MYAPPNAME/shared/public/assets/uploads/blog/name-of-image.jpg

You confirmed that the image is in:

home/deploy/MYAPPNAME/shared/public/uploads/blog/name-of-image.jpg

Nginx is looking in public/assets/uploads, your file is in public/uploads.

Upvotes: 3

Related Questions