Nginx的Upload上傳模組

來源:互聯網
上載者:User

標籤:des   http   ar   io   sp   for   on   檔案   div   

前段時間做一個項目,需要上傳檔案,差不多需要20M左右,普通用php處理會比較麻煩,經常逾時,而且大量佔用資源。於是搜尋了下,決定用nginx的upload上傳模組來處理。

你可以在這裡:http://www.grid.net.ru/nginx/upload.en.html 擷取源碼。下載以後需要重新編譯nginx

123 ./configure –add-module=/usr/local/nginx_upload_module-*makemake install

重啟nginx即可
以下是我的nginx設定檔

前端頁面提交的時候直接提交到 http://test.local/upload 即可

123456789101112131415161718192021222324252627282930313233343536 server{        listen 80;        server_name test.local;        index index.php index.shtml index.htm index.html;        root  /data/app/test.local/wwwroot;        access_log  off;         location /upload {                upload_pass     /index.php?c=uploader&a=upload_server;                upload_cleanup 400 404 499 500-505;                upload_store    /data/app/test.local/upload_tmp;                upload_store_access user:r;                upload_limit_rate 128k;                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;                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 "^.*$";        }         location ~ .*\.php?$        {                include fastcgi_params;        }         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {                expires      30d;        }         location ~ .*\.(js|css)?$ {                expires      1d;        } }

大概解釋一下每個參數

12345678910111213141516171819202122 upload_pass 指明了需要後續處理的php地址 upload_cleanup 如果php出現400 404 499 500-505之類的錯誤,則刪除上傳的檔案 upload_store 上傳檔案存放地址 upload_store_access 上傳檔案的存取權限,user:r是指使用者可讀 upload_limit_rate 上傳限速,如果設定為0則表示不限制 upload_set_form_field 設定額外的表單欄位。這裡有幾個可用的變數:$upload_file_name 檔案原始名字$upload_field_name 表單的name值$upload_content_type 檔案的類型$upload_tmp_path 檔案上傳後的地址 upload_aggregate_form_field 額外的變數,在上傳成功後產生$upload_file_md5 檔案的MD5校正值$upload_file_size 檔案大小 upload_pass_form_field 從表單原樣轉到後端的參數,可以Regex表示官方的例子是upload_pass_form_field "^submit$|^description$";意思是把submit,description這兩個欄位也原樣通過upload_pass傳遞到後端php處理。如果希望把所有的表單欄位都傳給後端可以用upload_pass_form_field "^.*$";

Nginx的Upload上傳模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.