1

I built a file upload form using AJAX code that runs perfectly on my localhost (xampp/apache). I can upload files as large as 256MB. Using HTTP.

However, when I upload this code to my nginx server on aws ec2 I get a 404 when trying to upload any file larger the 10KB (yes that is a K). I'm using HTTPS.

I thought that maybe the php.ini settings were incorrect so I added this code to the file upload form.

<script>
    console.log("max_execution_time = <?php echo(ini_get('max_execution_time')); ?>");
    console.log("max_input_time = <?php echo ini_get('max_input_time'); ?>");
    console.log("memory_limit = <?php echo ini_get('memory_limit'); ?>");
    console.log("post_max_size = <?php echo ini_get('post_max_size'); ?>");
    console.log("file_uploads = <?php echo ini_get('file_uploads'); ?>");
    console.log("upload_max_filesize = <?php echo ini_get('upload_max_filesize'); ?>");
    console.log("max_file_uploads = <?php echo ini_get('max_file_uploads'); ?>");
    console.log("default_socket_timeout = <?php echo ini_get('default_socket_timeout'); ?>");
</script>

I get this console output.

Note the 0 followed by "File upload success". Here 0 is the file error code i.e. $_FILES['ninja_file']['error']

The two files I uploaded in this case were 9KB.

When you see the Post ... 404 () at the bottom that was due to me trying to upload a 22KB file.

enter image description here

In nginx.conf I set

client_max_body_size 0;

Setting size to 0 disables checking of client request body size per the docs http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

Technology:

  • Wordpress 4.7
  • PHP 7.0
  • NGINX 1.10

1 Answer 1

0

Check the nginx error.log (based on nginx.conf error_log location).

$ sudo nano /path/to/nginx/error.log

There is a "permission denied" error on /var/lib/nginx. This happened because I changed the user setting in nginx.conf from

user nginx;

to something else. In this case,

user iam;

Solution:

Check the current user and group ownership on /var/lib/nginx.

$ ls -ld /var/lib/nginx
drwx------ 3 nginx nginx 4096 Aug  5 00:05 /var/lib/nginx

This tells you that a possibly non-existent user and group named nginx owns this folder. This prevents file uploading.

Change the folder ownership to the user defined in nginx.conf in this case iam (sudo may not be required).

$ sudo chown -Rf iam:iam /var/lib/nginx

Verify that it actually changed.

$ ls -ld /var/lib/nginx
drwx------ 3 iam iam 4096 Aug  5 00:05 /var/lib/nginx

The permission denied error should now go away. Check the error.log (based on nginx.conf error_log location).

$ sudo nano /path/to/nginx/error.log

If that doesn't work you might need to reload nginx and php-fpm.

$ sudo service nginx reload
$ sudo service php-fpm reload
1
  • why didn't it affect files less than 10kb? Commented May 10, 2019 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.