Nginx的Upload上傳模塊
睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接
1
2
3
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
前段時間做一個項目,需要上傳文件,差不多需要20M左右,普通用php處理會比較麻煩,經常超時,而且大量占用資源。于是搜索了下,決定用nginx的upload上傳模塊來處理。
你可以在這里:http://www.grid.net.ru/nginx/upload.en.html 獲取源碼。下載以后需要重新編譯nginx
.
/configure
–add-module=
/usr/local/nginx_upload_module-
*
make
make
install
重啟nginx即可
以下是我的nginx配置文件
前端頁面提交的時候直接提交到 http://test.local/upload 即可
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;
}
}
大概解釋一下每個參數
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
"^.*$"
;