普通網站在實現文件上傳功能的時候,通常是使用Python,Java等後端程序實現,比較麻煩。Nginx有一個Upload模塊,能夠很是簡單的實現文件上傳功能。此模塊的原理是先把用戶上傳的文件保存到臨時文件,而後在交由後臺頁面處理,而且把文件的原名,上傳後的名稱,文件類型,文件大小set到頁面。下面和你們具體介紹一下。php
爲了使用Nginx Upload Module,須要編譯安裝Nginx,將upload module編譯進去。upload module的代碼能夠去Github上下載: Upload Modulehtml
以後的編譯安裝Nginx這裏就不介紹,不瞭解的能夠參考: Ubuntu 14.10下源碼編譯安裝Nginx 1.8.0前端
Nginx upload module的簡單配置以下:python
server { listen *:80 default_server; server_name 192.168.1.251; client_max_body_size 20m; client_body_buffer_size 512k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE_ADD $remote_addr; location /upload { # 轉到後臺處理URL,表示Nginx接收完上傳的文件後,而後交給後端處理的地址 upload_pass @python; # 臨時保存路徑, 可使用散列 # 上傳模塊接收到的文件臨時存放的路徑, 1 表示方式,該方式是須要在/tmp/nginx_upload下建立以0到9爲目錄名稱的目錄,上傳時候會進行一個散列處理。 upload_store /tmp/nginx_upload; # 上傳文件的權限,rw表示讀寫 r只讀 upload_store_access user:rw group:rw all:rw; set $upload_field_name "file"; # upload_resumable on; # 這裏寫入http報頭,pass到後臺頁面後能獲取這裏set的報頭字段 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模塊自動生成的一些信息,如文件大小與文件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 "^.*$"; # upload_pass_form_field "^submit$|^description$"; # 每秒字節速度控制,0表示不受控制,默認0, 128K upload_limit_rate 0; # 若是pass頁面是如下狀態碼,就刪除這次上傳的臨時文件 upload_cleanup 400 404 499 500-505; # 打開開關,意思就是把前端腳本請求的參數會傳給後端的腳本語言,好比:http://192.168.1.251:9000/upload/?k=23,後臺能夠經過POST['k']來訪問。 upload_pass_args on; } location @python { proxy_pass http://localhost:9999; # return 200; # 若是不須要後端程序處理,直接返回200便可 } }
這裏咱們使用Django做爲後端處理程序,比較簡單,具體以下:nginx
首先建立Django項目:git
django-admin.py startproject uploadmodule
而後,建立views.py文件,代碼以下:github
# -*- coding: utf-8 -*- import os import json from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt UPLOAD_FILE_PATH = '/tmp/nginx_upload/' @csrf_exempt def upload(request): request_params = request.POST file_name = request_params['file_name'] file_content_type = request_params['file_content_type'] file_md5 = request_params['file_md5'] file_path = request_params['file_path'] file_size = request_params['file_size'] ip_address = request.META.get('HTTP_X_REAL_IP') or request.META.get('HTTP_REMOTE_ADD') # save file to tmp new_file_name = '%s_%s' % (file_md5, ip_address) new_file_path = ''.join([UPLOAD_FILE_PATH, new_file_name, os.path.splitext(file_name)[-1]]) with open(new_file_path, 'a') as new_file: with open(file_path, 'rb') as f: new_file.write(f.read()) content = json.dumps({ 'name': file_name, 'content_type': file_content_type, 'md5': file_md5, 'path': file_path, 'size': file_size, 'ip': ip_address, }) response = HttpResponse(content, content_type='application/json; charset=utf-8') return response
上面的代碼完成以後,咱們經過下面的命令啓動Django後端程序:django
cd uploadmodule/ python manage.py runserver 0.0.0.0:9999
而後,模擬POST請求:http://192.168.1.251/upload/,上傳一個jpg文件,返回結果以下:json
{ "name": "6125444419718417450.jpg", "ip": "192.168.1.121", "content_type": "image/jpeg", "path": "/tmp/nginx_upload/0000000002", "md5": "c3b1bd2e72694a8d5fc4548b9ecd9e18", "size": "37980" }
參考:後端
Ubuntu 14.10下源碼編譯安裝Nginx 1.8.0
HttpUploadModule - Nginx Community
vkholodkov/nginx-upload-module at 2.2
Uploading to nginx using the nginx upload module with php_handler