403 error with large media files on django server

2019-05-09 14:30发布

问题:

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.

回答1:

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