Question

Nginx: https_upload module save files with their original name instead of a number?

I’m using the ngx_http_upload_module in Nginx to handle file uploads. Currently, it saves the uploaded files with an incrementing number instead of their original names. I want to preserve the original filenames when saving these files to my specified directory. I understand that the module allows access to the original file name through the $upload_file_name variable, but I’m not sure how to effectively use this to save the file under its original name.

Is there a direct way to configure the ngx_http_upload_module to save files with their original names?

Here is my current nginx code:

location /upload {
    upload_pass   @backend;

    upload_store /var/upload;

    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_aggregate_form_field "$upload_field_name.md5" $upload_file_md5;
    upload_aggregate_form_field "$upload_field_name.size" $upload_file_size;

    upload_cleanup 400 404 499 500-505;
}

location @backend {
    proxy_pass   http://localhost:8080;
}


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
June 2, 2023
Accepted Answer

Heya,

As far as I know ngx_http_upload_module does not provide an out-of-the-box method for saving files by their original names instead of an incrementing number. The way files are stored is largely dependent on the configuration of the upload module and how it’s set up.

You can, however, access the original file name in the $upload_file_name variable after the file is uploaded. This variable can be used in your application logic to rename the file after it has been uploaded.

What you’ve already provided as an Nginx block will do the work as well. $upload_file_name will contain the original file name as it was on the client side. After uploading the file, you can send a request to a backend application (here it’s http://localhost:8080) with fields such as original filename, content_type, file path, etc. You can then use these details in your application to rename the file from the incrementing number to its original name.

Now, you’ll need to write some code in order for it fully work but I don’t see another way using the module.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel