Nginx的Upload上傳模組

double2li發表於2014-12-17

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

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

1
2
3
./configure –add-module=/usr/local/nginx_upload_module-*
make
make install

重啟nginx即可
以下是我的nginx配置檔案

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
        }
 
}

大概解釋一下每個引數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 從表單原樣轉到後端的引數,可以正規表示式表示
官方的例子是upload_pass_form_field "^submit$|^description$";意思是把submit,description這兩個欄位也原樣通過upload_pass傳遞到後端php處理。如果希望把所有的表單欄位都傳給後端可以用upload_pass_form_field "^.*$";


相關文章