nginx 1.5+ file upload -- best practices

File UploadNginx

File Upload Problem Overview


I'm looking to upload files through my nginx server. I'm currently running nginx-1.4.6 and am willing up move to the latest stable nginx-1.5* as necessary.

The community favorite is/was Valery Kholodkov's nginx-upload-module, found here. Regrettably Valery is no longer maintaining this module, details here. As of nginx-1.3.9 the module works partially or not at all.

I've compiled the nginx-upload-module into nginx-1.4.6 and am getting a bug (Client cxn closed) that seems to be fixed in the nginx-1.5.3 changeset. However I've applied the patch and had no luck.

Anatoly's slightly dated post, here, offers multiple solutions, includes four solutions

As I dive into the second and fourth solutions (I prefer not to make lua a dependency, but I might) I figured that it's appropriate to ask this community:

What are the current best practices for file uploading with nginx-1.5+?

Let me add that I'm uploading files from a python POST command and trying to test with curl. PHP is not on my tech stack.

File Upload Solutions


Solution 1 - File Upload

Maybe you can use perl if you don't like php or lua.

http://nginx.org/en/docs/http/ngx_http_perl_module.html#methods

> $r->has_request_body(handler)

But out of the box nginx isn't the tool to save a received post request and store it.

Maybe uWSGI ( https://uwsgi-docs.readthedocs.io/en/latest/ ) is a better way to go with http-socket https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html and a python app.

Solution 2 - File Upload

There's a good article about your doubt. https://coderwall.com/p/swgfvw

I tried nginx-upload-module. It's a good solution, but, it seem this doesn't work for new versions. There's also some modules in Lua that can help you. In my case, i had businness logic then i implemented in my app.

Have in mind, that big problem for upload files is the FILER. This is botleneck: a lot of load test i had made show me this conclusion.

Solution 3 - File Upload

Nginx supports the 'POST' method, where you can use big parameters. The client can use php or jsp to use post method to build the request to the nginx server.

Solution 4 - File Upload

I realize this is a very old question, but it's on the first page of google search for "nginx upload module" and it's the first Stack Overflow result, so in case this helps anyone else that comes across this question:

I am using the nginx-upload-module on nginx 1.10.3. As the original question mentions, Valery Kholodkov is no longer maintaining the module. However, there are several different forks and other people have modified it to work with newer nginx versions.

This pull request is what I used in order to compile nginx with the upload module.

https://github.com/vkholodkov/nginx-upload-module/pull/88

The upload module, IMO, is still the best solution out there if you're using nginx and php-fpm. This module allows uploads to be handled completely by nginx until they are complete, and then the job of processing the upload is passed onto PHP. So php-fpm processes won't create a bottleneck when your users are uploading lots of files.

Solution 5 - File Upload

Just a quick draft but try this...

Upload.php
if (isset($_FILES['dlc_file']) && !empty($_FILES['dlc_file'])) {
	if (empty($_FILES['dlc_file']['name']) === true) {
		echo = "Please choose a file to upload";
	} else {
		$allowed = array('zip', 'rar', 'gzip', 'tar', '7z', 'png', 'jpg');
		
		$file_name = $_FILES['dlc_file']['name'];
		$file_extn = strtolower(end(explode('.', $file_name)));
		$file_temp = $_FILES['dlc_file']['tmp_name'];
		
		if (in_array($file_extn, $allowed) === true) {
			$file_path = 'dlc/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
			move_uploaded_file($file_temp, $file_path);
			echo = "Successfully uploaded $file_name";
		} else {
			$file_types = implode(', ', $allowed);
			echo "File type is not allowed, Allowed file types $file_types";
		}
	}
}

index.php
<form action="upload.php" method="post" enctype="multipart/form-data>
	<input type="file" name="dlc_file">
	<input type="submit" name="submit" value="Upload">
</form>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayInNycView Question on Stackoverflow
Solution 1 - File UploadAleksandarView Answer on Stackoverflow
Solution 2 - File UploadAndre FonsecaView Answer on Stackoverflow
Solution 3 - File UploadchenqunView Answer on Stackoverflow
Solution 4 - File UploadagileafroView Answer on Stackoverflow
Solution 5 - File UploadWeb DeveloperView Answer on Stackoverflow