Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

403 error with large media files on django server running nginx

I'm running a Django server hosted on DigitalOcean using Nginx and Gunicorn. I tried adding a 2MB picture via the admin interface when I get hit with a 403 error. Looking into error.log indicated permission was denied, as follows:

2017/06/27 01:03:29 [error] 1643#1643: *30 open() "/home/brian/nydkc11/nydkc11/nydkcd11/media/image_main/dtc1.jpg" failed (13: Permission denied), client: 108.29.217.25, server: nydkc11.org, request: "GET /media/image_main/dtc1.jpg HTTP/1.1", host: "nydkc11.org", referrer: "http://nydkc11.org/admin/blog/image/7/change/"

The weirdest thing, however, is that smaller image files work just fine (around 18 kb or so). Anyone know why larger media files may be triggering the problem, and how I should fix it?

I had to set client_max_body_size to 100M in my nginx.conf, if that's a useful thing to know.

like image 682
Brian Lee Avatar asked Jun 27 '17 01:06

Brian Lee


2 Answers

I just came across the exact same problem. Thanks for your SO thread, otherwise I'd still be searching in the wrong place.

To fix this, you actually do not need to set DATA_UPLOAD_MAX_MEMORY_SIZE as long as you're only experiencing this error when uploading files. The Django documentation links to the other value, FILE_UPLOAD_MAX_MEMORY_SIZE, here.

Setting FILE_UPLOAD_MAX_MEMORY_SIZE to a higher limit in my settings.py resolves the problem, alongside with the aforementioned client_max_body_size in nginx.conf.

This sets the upload maximum to roughly 200 MB:

FILE_UPLOAD_MAX_MEMORY_SIZE = 200000000

like image 198
Michael Gecht Avatar answered Nov 15 '22 08:11

Michael Gecht


larger files(>2.5M) are stored using temporary files, which will get 0o600 permissions by default. The problem can easily be fixed by setting a value to FILE_UPLOAD_PERMISSIONS

Set this in settings.py

FILE_UPLOAD_PERMISSIONS = 0o644

Reference

See For Python2

See Documentation

like image 30
ცოტნე შარვაძე Avatar answered Nov 15 '22 07:11

ცოტნე შარვაძე