在我的应用程序中,我将文件上传与 Carrierwave 一起使用。我使用 MiniMagick 调整图像大小并将图像保存为大版本,如下所示:
version :large do
resize_to_limit(100, 100)
end
在视图中,我称之为“大”版本:
<%= image_tag @user.avatar.url(:large) %>
在开发环境中,图像显示并且路径正确:
<img src="/uploads/user/....">
但是在生产环境中,没有图像被显示,因为它呈现了错误的路径(它预先添加了应用程序名):
<img src="appname/uploads/user/....">
我使用带有 Nginx、Unicorn、Capistrano、Ruby 2.0.0p353 和 Rails 4.0.2 的 Ubuntu 服务器
nginx.conf:
upstream unicorn {
server unix:/tmp/unicorn.appname.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name appname.domain.com;
root /home/deployer/apps/appname/current/public;
location ~ ^/(assets)/ {
root /home/deployer/apps/appname/current/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
载波上传器:
class UserUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :large do
resize_to_limit(100, 100)
end
end