ruby on rails nginx 如何上传大文件?

2017-07-19  本文已影响0人  limx

用ruby on

rails开发的web,用了carrierwave和dropzone实现了上传文件。但后来发现,一旦文件大于200M时,就不行了,特别慢,虽说carrierwave有个move_to_cache、move_to_store的选项,但好像起不了作用。于是又去研究其它的上传方式,之后发现nginx的upload

module比较靠普,但这样做有一个问题就是nginx必须是编译安装的,要把upload

module一块编译进行才能用。在这里记录一下实现的具体流程。

一、编译安装nginx,并把upload module模块编译进去

如果没有安装编译工具,要先安装编译工具: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

如果没有权限就加上sudo

nginx -v

提示:nginx version: nginx/1.10.2, 则安装成功

二、修改nginx的配置文件

# HTTPS server configuration

# 这个使用的是puma启动地址

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 ##

# 设置代理

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中要有相同可以上传的地址,用来处理上传成功后的逻辑

# 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; # 使用的用户及权限,可以根据需要调整

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的验证token,要传到后面rails里去,要不然rails会报错

upload_cleanup 400 404 499 500-505;

}

# Pass altered request body to a backend

location @test {

proxy_pass   http://localurl;

}

}

三、rails处理文件

在相应的controller里:

def file_upload

file = params["file.path"]  # 接收文件的存放路径

FileUtils.mv file, "#{Rails.root}/public/uploads/filename.rar"  #把文件转移需要的地方,nginx上传后的文件名是一串字符并没有扩展名,所以这里要重命名一下。

# 这里写逻辑代码

end

这样就完成了大文件的上传和处理

上一篇下一篇

猜你喜欢

热点阅读