用ruby on
rails開發(fā)的web剂陡,用了carrierwave和dropzone實(shí)現(xiàn)了上傳文件狈涮。但后來(lái)發(fā)現(xiàn),一旦文件大于200M時(shí)鹏倘,就不行了,特別慢顽爹,雖說(shuō)carrierwave有個(gè)move_to_cache纤泵、move_to_store的選項(xiàng),但好像起不了作用镜粤。于是又去研究其它的上傳方式捏题,之后發(fā)現(xiàn)nginx的upload
module比較靠普,但這樣做有一個(gè)問(wèn)題就是nginx必須是編譯安裝的肉渴,要把upload
module一塊編譯進(jìn)行才能用公荧。在這里記錄一下實(shí)現(xiàn)的具體流程。
一同规、編譯安裝nginx循狰,并把upload module模塊編譯進(jìn)去
如果沒有安裝編譯工具,要先安裝編譯工具:yum -y install gcc automake autoconf libtool make
下載nginx: http://nginx.org/download/nginx-1.10.2.tar.gz
下載upload module: www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
解壓:
tar -zxvf nginx-1.10.2.tar.gz
tar -zxvf nginx_upload_module-2.2.0.tar.gz
cd nginx-1.10.2
開始安裝:
./configure
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre-8.40 \
--with-zlib=../zlib-1.2.11 \
--add-module=../nginx_upload_module-2.2.0
make && make install
如果沒有權(quán)限就加上sudo
nginx -v
提示:nginx version: nginx/1.10.2券勺, 則安裝成功
二绪钥、修改nginx的配置文件
# HTTPS server configuration
# 這個(gè)使用的是puma啟動(dòng)地址
upstream localurl {
server localhost:3000;
}
server {
listen?????? 80;
try_files?????? $uri/index.html $uri.html $uri @app_server;
index? index.html index.htm;
client_max_body_size? 2000m; # 大文件上傳支持
client_body_buffer_size 1024k;
location ~* ^/(assets)/{
expires???????? max;
gzip_static on; # to serve pre-gzipped version
add_header Cache-Control public;
break;
}
location ~* ^/(imgs|images|javascripts|stylesheets|img|assets|favicon.ico|download|fonts)/{
access_log????? off;
log_not_found?? off;
expires???????? max;
break;
}
## send request back to apache ##
# 設(shè)置代理
location / {
proxy_pass? http://localurl;
#Proxy Settings
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout????? 200;
proxy_send_timeout???????? 1200;
proxy_read_timeout???????? 1200;
proxy_buffer_size????????? 4k;
proxy_buffers????????????? 4 128k;
proxy_busy_buffers_size??? 128k;
proxy_temp_file_write_size 128k;
}
# Upload form should be submitted to this location
location /uploads/file_upload {? # rails中要有相同可以上傳的地址,用來(lái)處理上傳成功后的邏輯
# autoindex on;
# Pass altered request body to this location
upload_pass?? @test;
# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /upload_path/uploads; # 文件存放的路徑
# Allow uploaded files to be read only by user
upload_store_access user:rw group:rw all:rw; # 使用的用戶及權(quán)限关炼,可以根據(jù)需要調(diào)整
upload_limit_rate 0;
upload_max_file_size 0;
# Set specified fields in request body
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
# Inform backend about hash and size of a file
upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_pass_form_field "authenticity_token|utf8";? #這里authenticity_token是rails的驗(yàn)證token程腹,要傳到后面rails里去,要不然rails會(huì)報(bào)錯(cuò)
upload_cleanup 400 404 499 500-505;
}
# Pass altered request body to a backend
location @test {
proxy_pass?? http://localurl;
}
}
三儒拂、rails處理文件
在相應(yīng)的controller里:
def file_upload
file = params["file.path"]? # 接收文件的存放路徑
FileUtils.mv file, "#{Rails.root}/public/uploads/filename.rar"? #把文件轉(zhuǎn)移需要的地方寸潦,nginx上傳后的文件名是一串字符并沒有擴(kuò)展名色鸳,所以這里要重命名一下。
# 這里寫邏輯代碼
end
這樣就完成了大文件的上傳和處理