普通网站在实现文件上传功能的时候,一般是使用Python,Java等后端程序实现,比较麻烦。Nginx有一个Upload模块,可以非常简单的实现文件上传功能。此模块的原理是先把用户上传的文件保存到临时文件,然后在交由后台页面处理,并且把文件的原名,上传后的名称,文件类型,文件大小set到页面。下面和大家具体介绍一下。<br/>一、编译安装Nginx<br/><br/>为了使用Nginx Upload Module,需要编译安装Nginx,将upload module编译进去。upload module的代码可以去Github上下载: Upload Module<br/><br/>之后的编译安装Nginx这里就不介绍,不了解的可以参考: Ubuntu 14.10下源码编译安装Nginx 1.8.0<br/>二、Nginx配置<br/><br/>Nginx upload module的简单配置如下:<br/><br/>server {<br/>&nbsp;&nbsp;&nbsp; listen *:80 default_server;<br/>&nbsp;&nbsp;&nbsp; server_name 192.168.1.251;<br/>&nbsp;&nbsp;&nbsp; client_max_body_size 20m;<br/>&nbsp;&nbsp;&nbsp; client_body_buffer_size 512k;<br/>&nbsp;&nbsp;&nbsp; proxy_set_header Host $host;<br/>&nbsp;&nbsp;&nbsp; proxy_set_header X-Real-IP $remote_addr;<br/>&nbsp;&nbsp;&nbsp; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br/>&nbsp;&nbsp;&nbsp; proxy_set_header REMOTE_ADD $remote_addr;<br/>&nbsp;&nbsp;&nbsp; location /upload {<br/>&nbsp;&nbsp; &nbsp;# 转到后台处理URL,表示Nginx接收完上传的文件后,然后交给后端处理的地址<br/>&nbsp;&nbsp; &nbsp;upload_pass @python;<br/>&nbsp;&nbsp; &nbsp;# 临时保存路径, 可以使用散列<br/>&nbsp;&nbsp; &nbsp;# 上传模块接收到的文件临时存放的路径, 1 表示方式,该方式是需要在/tmp/nginx_upload下创建以0到9为目录名称的目录,上传时候会进行一个散列处理。<br/>&nbsp;&nbsp; &nbsp;upload_store /tmp/nginx_upload;<br/>&nbsp;&nbsp; &nbsp;# 上传文件的权限,rw表示读写 r只读<br/>&nbsp;&nbsp; &nbsp;upload_store_access user:rw group:rw all:rw;<br/>&nbsp;&nbsp; &nbsp;set $upload_field_name &quot;file&quot;;<br/>&nbsp;&nbsp; &nbsp;# upload_resumable on;<br/>&nbsp;&nbsp; &nbsp;# 这里写入http报头,pass到后台页面后能获取这里set的报头字段<br/>&nbsp;&nbsp; &nbsp;upload_set_form_field &quot;${upload_field_name}_name&quot; $upload_file_name;<br/>&nbsp;&nbsp; &nbsp;upload_set_form_field &quot;${upload_field_name}_content_type&quot; $upload_content_type;<br/>&nbsp;&nbsp; &nbsp;upload_set_form_field &quot;${upload_field_name}_path&quot; $upload_tmp_path;<br/>&nbsp;&nbsp; &nbsp;# Upload模块自动生成的一些信息,如文件大小与文件md5值<br/>&nbsp;&nbsp; &nbsp;upload_aggregate_form_field &quot;${upload_field_name}_md5&quot; $upload_file_md5;<br/>&nbsp;&nbsp; &nbsp;upload_aggregate_form_field &quot;${upload_field_name}_size&quot; $upload_file_size;<br/>&nbsp;&nbsp; &nbsp;# 允许的字段,允许全部可以 &quot;^.*$&quot;<br/>&nbsp;&nbsp; &nbsp;upload_pass_form_field &quot;^.*$&quot;;<br/>&nbsp;&nbsp; &nbsp;# upload_pass_form_field &quot;^submit$|^description$&quot;;<br/>&nbsp;&nbsp; &nbsp;# 每秒字节速度控制,0表示不受控制,默认0, 128K<br/>&nbsp;&nbsp; &nbsp;upload_limit_rate 0;<br/>&nbsp;&nbsp; &nbsp;# 如果pass页面是以下状态码,就删除此次上传的临时文件<br/>&nbsp;&nbsp; &nbsp;upload_cleanup 400 404 499 500-505;<br/>&nbsp;&nbsp; &nbsp;# 打开开关,意思就是把前端脚本请求的参数会传给后端的脚本语言,比如:http://192.168.1.251:9000/upload/?k=23,后台可以通过POST[&#39;k&#39;]来访问。<br/>&nbsp;&nbsp; &nbsp;upload_pass_args on; &nbsp;<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp; location @python {<br/>&nbsp;&nbsp; &nbsp;proxy_pass http://localhost:9999;<br/>&nbsp;&nbsp; &nbsp;# return 200;&nbsp; # 如果不需要后端程序处理,直接返回200即可<br/>&nbsp;&nbsp;&nbsp; }<br/>}<br/><br/>三、后端处理程序<br/><br/>这里我们使用Django作为后端处理程序,比较简单,具体如下:<br/><br/>首先创建Django项目:<br/><br/>django-admin.py startproject uploadmodule<br/><br/>然后,创建views.py文件,代码如下:<br/><br/># -*- coding: utf-8 -*-<br/>import os<br/>import json<br/>from django.http import HttpResponse<br/>from django.views.decorators.csrf import csrf_exempt<br/>UPLOAD_FILE_PATH = &#39;/tmp/nginx_upload/&#39;<br/>@csrf_exempt<br/>def upload(request):<br/>&nbsp;&nbsp; &nbsp;request_params = request.POST<br/>&nbsp;&nbsp; &nbsp;file_name = request_params[&#39;file_name&#39;]<br/>&nbsp;&nbsp; &nbsp;file_content_type = request_params[&#39;file_content_type&#39;]<br/>&nbsp;&nbsp; &nbsp;file_md5 = request_params[&#39;file_md5&#39;]<br/>&nbsp;&nbsp; &nbsp;file_path = request_params[&#39;file_path&#39;]<br/>&nbsp;&nbsp; &nbsp;file_size = request_params[&#39;file_size&#39;]<br/>&nbsp;&nbsp; &nbsp;ip_address = request.META.get(&#39;HTTP_X_REAL_IP&#39;) or request.META.get(&#39;HTTP_REMOTE_ADD&#39;)<br/>&nbsp;&nbsp; &nbsp;# save file to tmp<br/>&nbsp;&nbsp; &nbsp;new_file_name = &#39;%s_%s&#39; % (file_md5, ip_address)<br/>&nbsp;&nbsp; &nbsp;new_file_path = &#39;&#39;.join([UPLOAD_FILE_PATH, new_file_name, os.path.splitext(file_name)[-1]])<br/>&nbsp;&nbsp; &nbsp;with open(new_file_path, &#39;a&#39;) as new_file:<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;with open(file_path, &#39;rb&#39;) as f:<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;new_file.write(f.read())<br/>&nbsp;&nbsp; &nbsp;content = json.dumps({<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;name&#39;: file_name,<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;content_type&#39;: file_content_type,<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;md5&#39;: file_md5,<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;path&#39;: file_path,<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;size&#39;: file_size,<br/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#39;ip&#39;: ip_address,<br/>&nbsp;&nbsp; &nbsp;})<br/>&nbsp;&nbsp; &nbsp;response = HttpResponse(content, content_type=&#39;application/json; charset=utf-8&#39;)<br/>&nbsp;&nbsp; &nbsp;return response<br/><br/>四、示例<br/><br/>上面的代码完成之后,我们通过下面的命令启动Django后端程序:<br/><br/>cd uploadmodule/<br/>python manage.py runserver 0.0.0.0:9999<br/><br/>然后,模拟POST请求:http://192.168.1.251/upload/,上传一个jpg文件,返回结果如下:<br/><br/>{<br/>&nbsp;&nbsp; &nbsp;&quot;name&quot;: &quot;6125444419718417450.jpg&quot;,<br/>&nbsp;&nbsp; &nbsp;&quot;ip&quot;: &quot;192.168.1.121&quot;,<br/>&nbsp;&nbsp; &nbsp;&quot;content_type&quot;: &quot;image/jpeg&quot;,<br/>&nbsp;&nbsp; &nbsp;&quot;path&quot;: &quot;/tmp/nginx_upload/0000000002&quot;,<br/>&nbsp;&nbsp; &nbsp;&quot;md5&quot;: &quot;c3b1bd2e72694a8d5fc4548b9ecd9e18&quot;,<br/>&nbsp;&nbsp; &nbsp;&quot;size&quot;: &quot;37980&quot;<br/>}<br/><br/>