安裝nginx_upload_module模組
| 代碼如下 |
複製代碼 |
./configure –add-module=/usr/local/nginx_upload_module-*
make
make install |
重啟nginx即可
以下是我的nginx設定檔
前端頁面提交的時候直接提交到 http://dev.local/upload 即可
| 代碼如下 |
複製代碼 |
|
server
{
listen 80;
server_name test.local;
index index.php index.shtml index.htm index.html;
root /data/app/dev.local/wwwroot;
access_log off;
location /upload {
upload_pass /upload;
upload_cleanup 400 404 499 500-505;
upload_store /data/app/dev.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 從表單原樣轉到後端的參數,可以Regex表示
官方的例子是upload_pass_form_field "^submit$|^description$";意思是把submit,description這兩個欄位也原樣通過upload_pass傳遞到後端php處理。如果希望把所有的表單欄位都傳給後端可以用upload_pass_form_field "^.*$";