1. 程式人生 > >Nginx支援http檔案上傳的配置

Nginx支援http檔案上傳的配置

一. 相關模組安裝
檢視Nginx是否安裝了這兩個模組(nginx_upload_module和nginx_uploadprogress_module),
使用命令:
$ nginx -V (注意是大寫),可以
檢視Nginx當時編譯時候的引數,如果發現有上述兩個模組,說明Nginx已經安裝了這兩個模組。
如果沒有的話,就需要安裝這兩個Nginx模組。


1. 下載nginx_upload_modul
下載連結:
http://www.grid.net.ru/nginx/upload.en.html
$ wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
$ tar xvzf nginx_upload_module-2.2.0.tar.gz


2. 下載nginx_uploadprogress_module
網址:
http://wiki.nginx.org/HttpUploadProgressModule
下載連結:
https://github.com/masterzen/nginx-upload-progress-module/tree/master
$ unzip nginx-upload-progress-module-master.zip 


二、Nginx新增編譯選項
由於這兩個模組不在Nginx原始碼中,需要重新編譯Nginx,在編譯選項中加上
--add-module=/模組原始碼路徑/nginx_upload_module-2.2.0 
--add-module=/模組原始碼路徑/nginx-upload-progress-module-maste 。 


$ make
$ make install


1. 編譯時的錯誤提示
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c: In function 'ngx_http_read_upload_client_request_body':
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2628: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2687: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c: In function 'ngx_http_do_read_upload_client_request_body':
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2769: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2785: error: 'ngx_http_request_body_t' has no member named 'to_write'
/opt/nginx_http_rtmp/nginx_upload_module-2.2.0/ngx_http_upload_module.c:2877: error: 'ngx_http_request_body_t' has no member named 'to_write'
make[1]: *** [objs/addon/nginx_upload_module-2.2.0/ngx_http_upload_module.o] Error 1
make[1]: Leaving directory `/opt/nginx_http_rtmp/nginx-1.6.0'
make: *** [build] Error 2
解決辦法:




三、配置Nginx,實現上傳模組來接收頁面上傳的檔案。
把下面配置新增到Nginx的配置檔案中,注意是加在server的上下文中。
        location = /upload {
                upload_pass     /service.php?path=uploadfile&a=upload_server;//表示Nginx接收完上傳的檔案後,然後交給後端處理的地址
                upload_cleanup 400 404 499 500-505; //表示當發生這些http status程式碼的情況下,會把上傳的檔案刪除
                upload_store    /tmp/upload_tmp 1;//上傳模組接收到的檔案臨時存放的路徑, 1 表示方式,該方式是需要在/tmp/upload_tmp下建立以0到9為目錄名稱的目錄,上傳時候會進行一個雜湊處理。
                upload_store_access user:r; //指定訪問模式
                upload_limit_rate 128k; //設定上傳速度上限
                upload_set_form_field "${upload_field_name}_name" $upload_file_name; //設定後續指令碼語言訪問的變數,其中${upload_field_name}對照本例子就是addfile。比如後臺PHP就可以通過$_POST['addfile_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_store設定了臨時檔案存放根路徑,該路徑就是經過散裂後上傳檔案存在真實路徑,比如後續處理可以根據這值把上傳檔案拷貝或者移動到指定的目錄下。
                upload_pass_form_field "^.*$";//
                upload_pass_args on;// 開啟開關,意思就是把前端指令碼請求的引數會傳給後端的指令碼語言,比如:http://192.168.1.203:7100/upload/?k=23.PHP指令碼可以通過$_POST['k']來訪問。
        }


上述配置完了,就可以實現上傳的功能了。
但是,要獲取上傳的進度,那還是需要配置另外一個模組nginx_uploadprogress_module。
其實,獲取當前進度原理比較簡單,就是通過javascript以非同步方式定時給特定地址傳送請求,
這個模組會以json格式返回上傳的進度。配置比較簡單。
1)、首先開啟這個模組功能,在Nginx配置檔案中http上下文裡面,增加upload_progress proxied 5m;
     其中,proxied表示名稱(zone_name官方文件),5m表示每次連結存放跟蹤資訊的大小。
     另外,再設定返回格式為json,upload_progress_json_output;
2)、在上述的location = /upload中增加一個配置項track_uploads proxied 30s; 
     其中,proxied就是剛才在第一步設定的名字,30s表示每次連結處理完畢後,連結會保持30s。
3)、設定一個location來處理javascript傳送請求。
location ^~ /progress {
  report_uploads proxied;    #GET此地址得到上傳進度
}
4)、還有一個引數考慮設定upload_progress_header ,這個值預設是X-Progress-ID。
有點類似SessionID,主要用在前臺需要在上傳檔案的時候需要設定這個引數值,比如設定為uuid值。
這樣javascript每次傳送請求要獲取上傳進度時候,都需要帶上這個引數,
這樣上傳進度跟蹤模組才知道是返回那個連結的進度。
經過這三步驟,就把上傳進度跟蹤模組配置好了。