Traditional sites in the processing of file upload requests, the general use of back-end programming language processing, such as: Java, PHP, Python, Ruby and so on. Today to introduce a module of Nginx, Upload module upload module, the principle of this module is to first save the user upload files to temporary files, and then to the background page processing, and the original name of the file, after the upload, file type, file size set to the page .
github:https://github.com/vkholodkov/nginx-upload-module/tree/2.2
Site:http://wiki.nginx.org/httpuploadmodule
First, the installation module
Download source code from github to extract, enter the Nginx source directory, re./configure and add the following parameters:
Add this parameter--add-module=path/to/nginx_upload_module//if your upload module path is:/home/nginx_upload_module--add-module=/ Home/nginx_upload_module
If you want to know the previous Nginx installation parameters can be obtained as follows:
[Email protected] uploadtmp]#/usr/local/nginx/sbin/nginx-vnginx version:nginx/1.5.10built by gcc 4.4.7 20120313 (Red H At 4.4.7-3) (GCC) TLS SNI support enabledconfigure arguments:--user=www--group=www--prefix=/usr/local/nginx--with-htt P_stub_status_module--with-http_ssl_module--add-module=/home/nginx-upload-module-2.2
During the configuration process, be aware of any errors in the review process
Configuring additional modulesadding Module in/home/nginx-upload-module-2.2 + ngx_http_upload_module was configured
If there's nothing wrong, just do it. make && make install
Second, the configuration
Take a direct example:
# Upload size limit (including all content) client_max_body_size 100m;# upload path configuration location/upload { # go to background processing URL upload_pass / Uploadhandle; # Temporary save path # can use hash upload_store/tmp/nginx_upload; # Upload file permissions, rw means read/write R upload_store_access USER:RW; # here write the HTTP header, pass to the background page can get here Set header field 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 module automatically generates some information, such as file size and file MD5 value Upload_aggregate_form_field "${upload_field_name}_md5" $upload _file_md5; Upload_aggregate_form_field "${upload_field_name}_size" $upload _file_size; # allowed fields, allow all can "^.*$" Upload_pass_form_field "^submit$|^description$"; # Byte speed control per second, 0 means uncontrolled, default 0 upload_limit_rate 0; # If the pass page is the following status code, delete this uploaded temporary file upload_cleanup 400 404 499 500-505;}
In the above configuration, only some common configuration, more complete configuration please see the site of Nginx Upload module
Third, testing
I use the example.php in the example to test the results:
Even Chinese names are recognized set.
Iv. Some of the recommendations
efficiency comparison , this module is written in C language, efficiency is not a problem, the other is not too much to occupy the background language thread. By contrast, Nginx is more efficient at handling file uploads with the ability to balance debt.
Permissions Control , this really hurts, because the business code in the nginx.conf
write too much is not good maintenance, but if the permissions are not considered, and the file size control smaller, combined upload_cleanup
will not be too much pressure.
upload Process , can be combined with the use of Nginx nginx_uploadprogress_module
storage location , you can even save temporary files to Tmpfs (although there is a possibility of loss)
Nginx Upload Module Upload modules