一. nginx upload module原理
官方文檔: http://www.grid.net.ru/nginx/upload.en.html
Nginx upload module通過nginx服務來接受用戶上傳的文件,自動解析請求體中存儲的所有文件上傳到upload_store指定的目錄下。這些文件信息從原始請求體中分離並根據nginx.conf中的配置重新組裝好上傳參數,交由upload_pass指定的段處理,從而允許處理任意上傳文件。每個上傳文件中的file字段值被一系列的upload_set_form_field指令值替換。每個上傳文件的內容可以從$upload_tmp_path變量讀取,或者可以將文件轉移到目的目錄下。上傳的文件移除可以通過upload_cleanup指令控制。如果請求的方法不是POST,模塊將返回405錯誤(405 Not Allowed),該錯誤提示可以通過error_page指令處理。
具體的過程如下:
1. 用戶訪問能夠選擇上傳文件的頁面
2. 用戶提交表單
3. 瀏覽器把文件和有關文件的信息作為請求的一部分發送給服務器
4. 服務器把文件保存到臨時存儲目錄下upload_store
5. upload_pass指定的處理表單提交的php頁面將文件從upload_store拷貝到持久存儲位置
P.S.
安裝編譯方法
1.下載
1
|
wget http:
//www
.grid.net.ru
/nginx/download/nginx_upload_module-2
.2.0.
tar
.gz
|
2.編譯(在NGINX編譯目錄執行以下命令, 其中 --add-module=你下載解壓的上傳插件目錄)
1
2
|
.
/configure
--user=www --group=www --prefix=
/usr/local/nginx
--with-http_stub_status_module --with-http_s
sl_module --with-http_gzip_static_module --add-module=
/data/downfile/nginx_upload_module-2
.2.0
|
二.nginx upload module配置參數
upload_pass 指明后續處理的php地址。文件中的字段將被分離和取代,包含必要的信息處理上傳文件。
upload_resumable 是否啟動可恢復上傳。
upload_store 指定上傳文件存放地址(目錄)。目錄可以散列,在這種情況下,在nginx啟動前,所有的子目錄必須存在。
upload_state_store 指定保存上傳文件可恢復上傳的文件狀態信息目錄。目錄可以散列,在這種情況下,在nginx啟動前,所有的子目錄必須存在。
upload_store_access 上傳文件的訪問權限,user:r是指用戶可讀
upload_pass_form_field 從表單原樣轉到后端的參數,可以正則表達式表示。:
$upload_field_name — 原始文件中的字段的名稱
upload_pass_form_field “^submit$|^description$”;
意思是把submit,description這兩個字段也原樣通過upload_pass傳遞到后端php處理。如果希望把所有的表單字段都傳給后端可以用upload_pass_form_field “^.*$”;
upload_set_form_field 名稱和值都可能包含以下特殊變量:
$upload_field_name 表單的name值
$upload_content_type 上傳文件的類型
$upload_file_name 客戶端上傳的原始文件名稱
$upload_tmp_path 文件上傳后保存在服務端的位置
upload_aggregate_form_field 可以多使用的幾個變量,文件接收完畢后生成的並傳遞到后端
$upload_file_md5 文件的MD5校驗值
$upload_file_md5_uc 大寫字母表示的MD5校驗值
$upload_file_sha1 文件的SHA1校驗值
$upload_file_sha1_uc 大寫字母表示的SHA1校驗值
$upload_file_crc32 16進制表示的文件CRC32值
$upload_file_size 文件大小
$upload_file_number 請求體中的文件序號
這些字段值是在文件成功上傳后計算的。
upload_cleanup 如果出現400 404 499 500-505之類的錯誤,則刪除上傳的文件
upload_buffer_size 上傳緩沖區大小
upload_max_part_header_len 指定頭部分最大長度字節。
upload_max_file_size 指定上傳文件最大大小,軟限制。client_max_body_size硬限制。
upload_limit_rate 上傳限速,如果設置為0則表示不限制。
upload_max_output_body_len 超過這個大小,將報403錯(Request entity too large)。
upload_tame_arrays 指定文件字段名的方括號是否刪除
upload_pass_args 是否轉發參數。
三. nginx配置
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#wget http: //www.nginx.org/download/nginx-1.2.2.tar.gz
#wget http: //www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
#tar zxvf nginx_upload_module - 2.2.0.tar.gz - c.. / software /
#tar zxvf nginx_upload_module - 2.2.0.tar.gz - C.. / software /
#. / configure–prefix = /usr/local / nginx–add - module = .. / nginx_upload_module - 2.2.0–with - http_secure_link_module
#make
#make install #vi nginx.conf
user www - data;
worker_processes 20;
error_log logs / error.log notice;
working_directory / usr /
local
/ nginx; events {
worker_connections 1024;
} http {
include mime.types;
default_type application / octet - stream;
root / www / web / upload;
server {
listen 80;
server_name 192.168.41.129;
error_page 405 = 200@405;
//
處理405錯誤
location / {
index index.html index.htm index.php;
}
location@405 {
root / www / web / upload;
} location~\.php$ {
try_files $uri / 404.html;
fastcgi_pass 127.0.0.1 : 9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include
/etc/nginx/fastcgi_params
;
} client_max_body_size 100m;
#上傳頁面提交到這個location location / upload {
#文件上傳以后轉交給后端的php代碼處理
upload_pass@
test
;
#上傳文件的臨時存儲位置,目錄是散列的,應該存在子目錄0 1 2 3 4 5 6 7 8 9
upload_store
/www/web/upload/tmp
1;
upload_store_access user: r;
#設置請求體的字段 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;
# 指示后端關於上傳文件的md5值和文件大小
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_args on;
}
#將請求轉到后端的地址處理
location@
test
{
rewrite ^ (. * ) $ /
test
.php last;
}
}
}
|
四. 上傳界面
# cat /www/web/upload/upload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
html
>
<
head
>
<
title
>Test upload</
title
>
</
head
>
<
body
>
<
h2
>Select files to upload</
h2
>
<
form
enctype=”multipart/form-data” action=”/upload” method=”post”>
<
input
type=”file” name=”file1″><
br
>
<
input
type=”file” name=”file2″><
br
>
<
input
type=”file” name=”file3″><
br
>
<
input
type=”file” name=”file4″><
br
>
<
input
type=”file” name=”file5″><
br
>
<
input
type=”file” name=”file6″><
br
>
<
input
type=”submit” name=”submit” value=”Upload”>
<
input
type=”hidden” name=”test” value=”value”>
</
form
>
</
body
>
</
html
>
|
五. upload_pass處理內容
# cat test.php //這里只是簡單的打印出來,便於先理解上傳原理。請對着輸出內容理解下nginx upload module配置參數。
<?php
print_r($_POST);
?>
對上傳文件的處理請參考:http://cn.php.net/manual/en/features.file-upload.php
六. 測試
http://192.168.41.129/upload.html
輸出內容如下所示:
Array
(
[file1_name] => Learning Perl, Sixth Edition.pdf
[file1_content_type] => application/pdf
[file1_path] => /www/web/upload/tmp/4/0000000014
[file1_md5] => 87032cc58109f5c6bb866d2684f9b48c
[file1_size] => 8927511
[file2_name] => Programming Perl, 4th Edition.pdf
[file2_content_type] => application/pdf
[file2_path] => /www/web/upload/tmp/5/0000000015
[file2_md5] => 82a52df177a8912c06af276581cfd5e4
[file2_size] => 21146356
[submit] => Upload
)
注意:需要修改php.ini以下參數
file_uploads on 是否允許通過http上傳
upload_max_filesize 8m 允許上傳文件的最大大小
post_max_size 8m 通過表單POST給php所能接收的最大值
另外nginx.conf中設置上傳文件大小
upload_max_file_size 軟限制
client_max_body_size 硬限制