Nginx http upload module config

nginxPHPupload

I recently put nginx in front of apache to act as a reverse proxy.

Up until now Apache handled directly the requests and file uploads

Now, I need to configure nginx so that it sends file upload requests to apache.

I have several endpoints for uploading files and I am trying to see if there is a quick way to define one configuration options in nginx for all my endpoints

Right now the below refers just to one file upload entry

location /banner_upload {
    proxy_pass  http://backend:8080/banner/save;
   }




 location /banner/save {
            # Pass altered request body to this location
            upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in 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;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "(.*)";
    }

But the above entry is just for 1 endpoint

I have like 7 different ones

Do I need to make a new entry foreach of my endpoints or is there a way I can group them

Thanks

Best Answer

Haven't tested this mind, but you should be able to set a named location and rewrite requests to that. You still need one line for each request though

#Add one of these for each location
rewrite ^/upload/location1$ @upload last;
rewrite ^/upload/location2$ @upload last;
rewrite ^/upload/location3$ @upload last;
rewrite ^/upload/location4$ @upload last;

location = @upload {
        # Pass altered request body to this location
        upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in 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;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
        
        upload_pass_form_field "(.*)";
}

That's the rough idea, I might have got some of the syntax wrong so consult the wiki if you get issues.

Edit

Depending on the rest of your config, you might have better luck rewriting to a directory

#Add one of these for each location
rewrite ^/upload/location1$ /upload/ last;
rewrite ^/upload/location2$ /upload/ last;
rewrite ^/upload/location3$ /upload/ last;
rewrite ^/upload/location4$ /upload/ last;

location /upload/ {
        # Pass altered request body to this location
        upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in 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;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
        
        upload_pass_form_field "(.*)";
}
Related Topic