Carrierwave + NGINX轨道生产图像无法显示

时间:2016-04-29 17:20:42

标签: ruby-on-rails nginx file-upload carrierwave

我已经部署了一个rails应用程序,允许用户上传照片并将其显示在另一个页面上,相当简单。我在开发中测试了它,图像上传到公共文件夹并正确显示。在生产中并将图像上传到服务器但未在页面上呈现。

仅针对我上传的图片出现404错误,路径如下所示:

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

我读了另一个S.O.文章提到将生产配置serve_static_files设置为true。这并没有解决问题。

我认为这可能是一个服务器配置,NGINX没有选择上传路径,这是我的/sites-default/nginx.conf文件。

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;
}

我还认为图片可能无法正确上传到服务器,因此我将SSH连接到服务器并找到上传的图片:

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

还发现我认为是相同的图像:

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

我的上传器看起来像这样:

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
使用

Rails 4.2.6
Ruby 2.3.0
Carrierwave 0.11.0

修改

所有其他静态图像,CSS和JS正在正确显示和呈现。

重新上传图片时出现NGINX错误:

   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"

1 个答案:

答案 0 :(得分:1)

从您的日志中

用户正在请求:

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

Nginx正在寻找图片:

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

您确认图片位于:

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

Nginx正在查看public/assets/uploads,您的文件位于public/uploads