nginx upload module upload

【转】https://blog.csdn.net/daa20/article/details/75084282

 

1. Principle of nginx upload module

Official documentation:  http://www.grid.net.ru/nginx/upload.en.html

The Nginx upload module accepts files uploaded by users through the nginx service, and automatically parses all files stored in the request body and uploads them to the directory specified by upload_store. These file information are separated from the original request body and reassembled the upload parameters according to the configuration in nginx.conf, and handed over to the segment specified by upload_pass to process, thereby allowing the processing of arbitrary uploaded files. The file field value in each uploaded file is replaced by a series of upload_set_form_field directive values. The content of each uploaded file can be read from the $upload_tmp_path variable, or the file can be moved to the destination directory. Uploaded file removal can be controlled with the upload_cleanup directive. If the requested method is not POST, the module will return a 405 error (405 Not Allowed), which can be handled by the error_page directive.

The specific process is as follows:

  1. User visits a page where they can choose to upload a file

  2. User submits form

  3. The browser sends the file and information about the file to the server as part of the request

  4. The server saves the file to the temporary storage directory upload_store

  5. The php page that handles the form submission specified by upload_pass copies the file from upload_store to the persistent storage location

2. nginx upload module configuration parameters

upload_pass indicates the php address for subsequent processing. The fields in the file will be separated and replaced, containing the necessary information to process the uploaded file.

upload_resumable Whether to start resumable uploads.

upload_store Specifies the upload file storage address (directory). Directories can be hashed, in which case all subdirectories must exist before nginx starts.

upload_state_store Specifies the file state information directory for saving uploaded files to restore uploads. Directories can be hashed, in which case all subdirectories must exist before nginx starts.

upload_store_access access permission to upload files, user:r means user readable

upload_pass_form_field The parameter that goes from the form to the backend as it is, which can be represented by a regular expression. :

$upload_field_name – the name of the field in the original file

upload_pass_form_field “^submit|description|description”;

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

Both the upload_set_form_field name and value may contain the following special variables:

$upload_field_name The name value of the form

$upload_content_type Type of uploaded file

$upload_file_name The original file name uploaded by the client

$upload_tmp_path The location where the file is saved on the server after uploading

upload_aggregate_form_field Several variables that can be used more, generated after the file is received and passed to the backend

$upload_file_md5 MD5 checksum of the file

$upload_file_md5_uc MD5 checksum in uppercase letters

$upload_file_sha1 SHA1 checksum of the file

$upload_file_sha1_uc uppercase SHA1 checksum

$upload_file_crc32 Hexadecimal representation of the file CRC32 value

$upload_file_size file size

$upload_file_number The file number in the request body

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

upload_cleanup delete uploaded files if there is an error like 400 404 499 500-505

upload_buffer_size upload buffer size

upload_max_part_header_len specifies the maximum length in bytes of the header part.

upload_max_file_size specifies the maximum size of the uploaded file, the soft limit. client_max_body_size hard limit.

upload_limit_rate Upload speed limit, if set to 0, it means no limit.

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

upload_tame_arrays Specifies whether the square brackets of the file field name are removed

upload_pass_args Whether to forward parameters.

3. nginx configuration

# wget http://www.nginx.org/download/nginx-1.2.2.tar.gz

# wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz

# tar zxvf nginx_upload_module-2.2.0.tar.gz -c ../software/

# tar zxvf nginx_upload_module-2.2.0.tar.gz -C ../software/

# ./configure --prefix=/usr/local/nginx --add-module=../nginx_upload_module-2.2.0 --with-http_secure_link_module

# make

# make  install

# vi nginx.conf

user www-data;

worker_processes 20;

error_log logs/error.log notice;

working_directory /usr/local/nginx;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

root /www/web/upload;

server {

listen 80;

server_name 192.168.41.129;



error_page 405 = 200 @ 405 ; // handle 405 error 

location / {

index index.html index.htm index.php;

}

location @405

{

root /www/web/upload;

}

location ~ \.php$ {

try_files $uri /404.html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include /etc/nginx/fastcgi_params;

}

client_max_body_size 100m;

# Upload the page and submit it to this location

location /upload {

# After the file is uploaded, it is transferred to the back-end php code for processing

upload_pass @test;

# Temporary storage location for uploaded files, the directory is hashed, there should be subdirectories 0 1  2  3  4  5  6  7  8  9

upload_store /www/web/upload/tmp 1;

upload_store_access user:r;

# Set the fields of the request body

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;

# Instruct the backend about the md5 value and file size of the uploaded file

upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;

upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

# Indicates the parameter that goes to the backend as it is, which can be represented by a regular expression

upload_pass_form_field "^submit$|^description$";

upload_pass_args on;

}

# Send the request to the backend's address for processing

location @test {

rewrite ^(.*)$ /test.php last;

}

}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028454&siteId=291194637