Nginx Upload Module test example

Source: Internet
Author: User
Tags file size file upload md5 regular expression sha1

Traditional websites generally use backend programming languages to process file upload requests, such as Java, PHP, Python, and Ruby. Today, we will introduce a Module of Nginx, the Upload Module. The principle of this Module is to first save the files uploaded by the user to a temporary file and then process them on the background page, set the file name, uploaded name, file type, and file size to the page.

GitHub: https://github.com/vkholodkov/nginx-upload-module/tree/2.2

Site: http://wiki.nginx.org/HttpUploadModule

I. Installation module

Download and decompress the source code from GitHub, go to the nginx Source Code Directory, re-./configure, and add the following parameters:

The code is as follows:
// 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

You can obtain nginx installation parameters as follows:

The code is as follows:

[Root @ lee uploadtmp] #/usr/local/nginx/sbin/nginx-V
Nginx version: nginx/1.5.10
Built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
Tls sni support enabled www.111cn.net
Configure arguments: -- user = www -- group = www -- prefix =/usr/local/nginx -- with-http_stub_status_module -- with-http_ssl_module -- add-module =/home/nginx-upload-module-2.2

Check whether errors occur during configuration.

The code is as follows:

ING additional modules
Adding module in/home/nginx-upload-module-2.2
+ Ngx_http_upload_module was configured

Make & make install

II. Configuration

Example:

The code is as follows:

# Upload size limit (including all content)
Client_max_body_size 100 m;
 
# Upload path configuration
Location/upload {
# Go to the background to process the URL
Upload_pass/uploadHandle;
 
# Temporary Save path
# Use hash
Upload_store/tmp/nginx_upload;
     
# Permission to upload files. rw indicates read/write r read-only
Upload_store_access user: rw;
 
# Write the http header here. After passing to the background page, you can get the header field of the set here.
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;
 
# Information automatically generated by the Upload module, 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;
 
# All allowed fields can be "^. * $"
Upload_pass_form_field "^ submit $ | ^ description $ ";
 
# Byte speed control per second. 0 indicates no control. The default value is 0.
Upload_limit_rate 0;
 
# If the pass page contains the following status codes, delete the temporary files uploaded this time.
Upload_cleanup 400 404 499 500-505;
}

To

In the above configuration, it is only some common configuration. For more comprehensive configuration, see The Nginx Upload Module site

III. Test

Test results using example. php in the instance:


Even Chinese names are identified.

4. Some suggestions

Efficiency Comparison: This module is written in C language, and the efficiency is naturally not a problem. The other one is that it does not occupy too many background languages. In contrast, Nginx uses the balance of liability to process file uploads, which is more efficient.

Permission control, this is indeed a pain point, because the business code in nginx. it is difficult to maintain a large number of files in the conf file, but if you do not consider the permissions, and the file size control is a little smaller, combined with upload_cleanup will not put too much pressure.

The upload process can be used in combination with Nginx's nginx_uploadprogress_module.

Storage location, or even saving temporary files to tmpfs (but this may be lost)


5. nginx upload module configuration parameters

Upload_pass indicates the php address to be processed later. Fields in the file will be separated and replaced, including necessary information for processing uploaded files.

Whether upload_resumable starts resumable Upload.

Upload_store specifies the address (directory) for storing the uploaded files ). Directories can be hashed. In this case, all subdirectories must exist before nginx is started.

Upload_state_store specifies the directory where the Upload file state information can be restored after being saved. Directories can be hashed. In this case, all subdirectories must exist before nginx is started.

Upload_store_access: the access permission for uploading files. user: r indicates that the user is readable.

Upload_pass_form_field is a parameter that is transferred from the original form to the backend. It can be expressed in a regular expression. :

$ Upload_field_name-name of the field in the original file

Upload_pass_form_field "^ submit $ | ^ description $ ";

This means that the submit and description fields are passed to the backend php for processing through upload_pass. If you want to pass all form fields to the backend, you can use upload_pass_form_field "^. * $ ";

Upload_set_form_field names and values may all contain the following special variables:

$ Upload_field_name name value of the form

$ Upload_content_type type of the file to be uploaded

$ Upload_file_name name of the original file uploaded by the client

$ Upload_tmp_path: the location on the server after the file is uploaded

Upload_aggregate_form_field can use multiple variables. After the file is received, it is generated and passed to the backend.

$ Upload_file_md5 file MD5 check value

$ Upload_file_md5_uc indicates the MD5 check value in uppercase letters.

$ Upload_file_sha1 file SHA1 check value

$ Upload_file_sha1_uc SHA1 check value in uppercase letters

$ Upload_file_crc32 file CRC32 value in hexadecimal notation

$ Upload_file_size file size

$ Upload_file_number indicates the object serial number in the request body.

These field values are calculated after the file is successfully uploaded.

If an error such as 400 404 499-occurs during upload_cleanup, delete the uploaded file.

Upload_buffer_size size of the upload buffer

Upload_max_part_header_len specifies the maximum length of the header.

Upload_max_file_size specifies the maximum size of the uploaded file. Client_max_body_size.

Upload_limit_rate: the upload speed limit. If it is set to 0, no limit is imposed.

If the size of upload_max_output_body_len exceeds this value, 403 error will be reported (Request entity too large ).

Upload_tame_arrays whether to delete square brackets of the specified file field name

Whether the upload_pass_args is a forwarding parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.