I. nginx_upload_module background
The nginx upload module Nginx_upload_module can be used to more effectively implement resumable upload of large files, and nginx-upload-progress-module can be installed to display the file upload progress.
II. Working principle of nginx_upload_module
Nginx_upload_module uses the nginx service to accept files uploaded by users. It automatically parses all files stored in the request body and uploads them to the directory specified by upload_store. The file information is separated from the original request body and re-assembled with the upload parameters according to the configuration in nginx. conf, which is handled by the specified upload_pass segment, so as to allow arbitrary file upload. The file field value in each uploaded file is replaced by a series of upload_set_form_field commands. The content of each uploaded file can be read from the $ upload_tmp_path variable or transferred to the target directory. You can use the upload_cleanup command to control the removal of uploaded files. If the requested method is Not POST, the module will return a 405 error (405 Not Allowed). The error prompt can be handled using the error_page command.
III. nginx_upload_module installation process
(1). Check whether the nginx_upload_module is installed.
/Usr/local/nginx/sbin/nginx-V
(2) Download nginx_upload_module
Wget https://github.com/hongzhidao/nginx-upload-module/archive/master.zip
Unzip master.zip
(3) enter the nginx Source Code Directory, recompile and install nginx, and add the nginx_upload_module compilation option.
-- Add-module =/module source code path/nginx_upload_module
(4) configure the nginx Upload module
Add configuration information in the server context:
Location/upload {
Upload_pass/upload. php; # the uploaded file is processed by the backend.
Upload_store/data/uptmp; # upload a folder
Upload_limit_rate 1024 k; # limit the upload speed
Upload_set_form_field "$ {upload_field_name} _ name" $ upload_file_name; # file name
Upload_set_form_field "$ {upload_field_name} _ content_type" $ upload_content_type; # file type
Upload_set_form_field "$ {upload_field_name} _ path" $ upload_tmp_path; # Upload path
Upload_aggregate_form_field "$ {upload_field_name} _ md5" $ upload_file_md5; # md5
Upload_aggregate_form_field "$ {upload_field_name} _ size" $ upload_file_size; # file size
Upload_pass_form_field "^ submit $ | ^ description $ ";
# If you want to pass all form fields to the backend, you can use upload_pass_form_field "^. * $ ";
}
IV. nginx_upload_module configuration parameters (for details, see the official documentation)
Syntax: upload_pass <location>
Default: none
Severity: mandatory
Context: server, location
Specify the server to which the request is sent. File fields will be reorganized, including necessary information for processing files.
Syntax: upload_resumable <on/off>
Default: off
Severity: mandatory
Context: main, server, location
Enable resumable Upload
Syntax: upload_store <directory> [<level 1> [<level 2>]...]
Default: none
Severity: mandatory
Context: server, location
Specifies the directory to store the uploaded files. Directories can be hashed. All subdirectories must exist before nginx is started.
Syntax: upload_state_store <directory> [<level 1> [<level 2>]...]
Default: none
Severity: optional
Context: server, location
Specifies the Directory of the state file contained in the resumable Upload file. The directory can be hashed. All subdirectories must exist before nginx is started.
Syntax: upload_store_access <mode>
Default: user: rw
Severity: optional
Context: server, location
Specifies the file upload permission mode.
Syntax: upload_set_form_field <name> <value>
Default: none
Severity: optional
Context: server, location
In the request body, specify the form fields of each Upload file to be generated. The field names and field values can be included in the following variables.
$ Upload_field_name -- Name of the source file field
$ Upload_content_type -- Content-Type value of the uploaded file
$ Upload_file_name -- Name of the uploaded source file. The path in the file name will be processed, for example, path "D: \ Documents And Settings \ My Dcouments \ My Pictures \ Picture.jpg "will be converted to" Picture.jpg ", or the path"/etc/passwd "will be converted to" passwd ".
$ Upload_tmp_path: The storage path of the source file. The output file name consists of 10 numbers. The algorithm used is the same as the path algorithm specified by proxy_temp_path.
These variables will only be verified when processing part of the request body, for example:
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 ";
Syntax: upload_aggregate_form_field <name> <value>
Default: none
Severity: optional
Context: server, location
Specify the field name and field value of the form. The field name and field value can contain nginx standard variables specified by upload_set_form_field or use the following variables:
$ Upload_file_md5 -- MD5 checksum of the file
$ Upload_file_md5_uc -- MD5 checksum of the file in uppercase letters
$ Upload_file_sha1 -- SHA1 checksum of the file
$ Upload_file_sha1_uc -- SHA1 checksum of the file in uppercase letters
$ Upload_file_crc32 -- hexdecimal value of CRC32 of the file
$ Upload_file_size -- size of the file in bytes
$ Upload_file_number -- ordinal number of file in request body
The field value specified above is calculated only after the file is successfully uploaded. Therefore, these variables are verified only after the last request body is uploaded.
Warning variables $ upload_file_md5, $ upload_file_md5_uc, $ upload_file_sha1 and $ upload_file_sha1_uc consume more resources to calculate MD5 and SHA1 verification.
Example:
Upload_aggregate_form_field $ upload_field_name.md5 "$ upload_file_md5 ";
Upload_aggregate_form_field $ upload_field_name.size "$ upload_file_size ";
Syntax: upload_pass_form_field <regex>
Default: none
Severity: optional
Context: server, location
Specify a regular expression for the field name, which will be forwarded to the backend server. Multiple matching modes can be set for each command. Once the matching mode is met, the command will be immediately forwarded to the backend server. If the mode is unrecognizable by PCRE, the field name will be precisely matched and the matching mode will be forwarded to the backend server, if the command is ignored, it is not forwarded to the backend server.
For example:
Upload_pass_form_field "^ submit $ | ^ description $ ";
PCRE-unrecognized:
Upload_pass_form_field "submit ";
Upload_pass_form_field "description ";
Syntax: upload_cleanup <HTTP status/range> [<HTTP status/range>...]
Default: none
Severity: optional
Context: server, location
Specifies the HTTP status after each file is successfully uploaded, used for cleaning after the backend server fails. When the backend server does not need to upload files for some reason, the backend server may also directly return the error code. The HTTP status must be a number ranging from to. You can specify the status of a broken number range.
For example:
Upload_cleanup 400 404 499 500-505;
Syntax: upload_buffer_size <size>
Default: size of memory page in bytes
Severity: optional
Context: server, location
Set the size of the write buffer, used to accumulate file data, and then write data to the hard disk at one time. This value is used to improve memory usage efficiency and optimize the number of system calls.
Syntax: upload_max_part_header_len <size>
Default: 512
Severity: optional
Context: server, location
The maximum length of the specified header, in bytes. This value is used to accumulate the buffer size of the stored header.
Syntax: upload_max_file_size <size>
Default: off
Severity: optional
Context: main, server, location
Specifies the maximum size of a file. If the file length exceeds this value, it will be ignored. This command is a soft limit. When the file length exceeds the specified maximum value, nginx will continue to receive the request body. If you want this value to take effect, you must use the client_max_body_size hard limit to set it. This value is set to 0, which means there is no limit.
Syntax: upload_limit_rate <rate>
Default: 0
Severity: optional
Context: main, server, location
Specify the upload speed. The unit is bytes/second. 0 indicates that the upload speed is unlimited.
Syntax: upload_max_output_body_len <size>
Default: 100 k
Severity: optional
Context: main, server, location
Specifies the maximum length of the output body. This prevents non-file form data from occupying the memory. If the value is exceeded, error 413 is returned, indicating that the request body is too large. If the value is 0, the request body is not limited.
Syntax: upload_tame_arrays <on/off>
Default: off
Severity: optional
Context: main, server, location
Specifies whether the square sign in the field name is discarded (requires a PHP array )..
Syntax: upload_pass_args <on/off>
Default: off
Severity: optional
Context: main, server, location
Request parameters are allowed to be forwarded. This parameter is specified by upload_pass as an invalid naming position. For example:
<Form action = "/upload? Id = 5 ">
...
Location/upload {
Upload_pass/internal_upload;
Upload_pass_args on;
}
...
Location/internal_upload {
...
Proxy_pass http: // backend;
}
In this example, the request address obtained by The backend server is "/upload? Id = 5 ". In this example, if upload_pass_args is set to off, the request address obtained by The backend server is"/upload ".
5. nginx_upload_module implementation code:
Html code:
<Form method = "post" enctype = "multipart/form-data" action = "/upload">
<Input type = "file" name = "myfile"/>
<Input type = "submit"/>
</Form>
PHP code:
Print_r ($ _ POST );
If (rename ($ _ POST ['myfile _ path'], $ _ POST ['myfile _ name']) {
Echo 'OK ';
}
/*
Array
(
[Myfile_name] => psb (22.16.jpg
[Myfile_content_type] => image/jpeg
[Myfile_path] =>/data/uptmp/0000000012
[Myfile_md5] => 091fa303622a020dba0e57a5d36ca276
[Myfile_size] = & gt; 18364
)
*/