ruby on rails nginx 如何上傳大檔案?

來源:互聯網
上載者:User

標籤:ges   store   error   命名   local   啟動   use   sheet   header   

用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

 

這樣就完成了大檔案的上傳和處理

 

ruby on rails nginx 如何上傳大檔案?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.