Django + Gunicorn media files

Running Django + Gunicorn, I’ve switched to DEBUG=False and realized I’m going to need a way to host my static and media files. The static files were fine, I use:

  guest_path = "/app/static"
  url_prefix = "/static/"

But media is dynamic, therefore I can’t as they should be present in the image. I’ve create a volume which is mounted via:

  destination = "/data"
  source = "data"

In a traditional setup I would use Nginx to serve these files. Because is acting as my Nginx in this case, is there a way to do this via the UI? Is there a way to easily serve media files without needing another server? There seems to be a lack of documentation for what should be a core part of using Django on! How has everyone else got around this? Ideally I keep all my infrastructure on site, without needing an S3 bucket or seperate hosting provider.

Use WhiteNoise. There should be more docs on Django + Fly shortly.

WhiteNoise doesn’t solve the media files aspect to this, does it? I would have thought the answer is that you have to use something like S3 as described here: Storing Django Static and Media Files on Amazon S3 |

I am very interested in learning about how to handle media files on w/out the use of AWS S3. Thanks!:slight_smile:

Correct @czue, WhiteNoise is only for static files. You’d normally use django-storages for media files and can host anywhere though S3 is commonly used.

1 Like

Off the top of my head I “think” you could just use a Volume though I need to play around with this to confirm and make sure no security issues.

@wsvincent that works for the upload, but the volume isn’t accessible to the outside world.

Just for reference, I ended up running a nginx process in the background which exposes the volume. This isn’t ideal!

Oh, interesting. Hmm… makes sense around exposure. Honestly django-storages plus S3/Netlify/whatever works so well that I’m not sure how deep down this rabbit hole I’m motivated to go personally. Though I am curious technically what’s possible! Maybe someone from Fly will weigh in around exposing volumes in a safe way :man_shrugging:

1 Like