Skip to content

Instantly share code, notes, and snippets.

@Tobi-De
Last active June 25, 2024 17:32
Show Gist options
  • Save Tobi-De/7751c394570cbf0d7beb852304394046 to your computer and use it in GitHub Desktop.
Save Tobi-De/7751c394570cbf0d7beb852304394046 to your computer and use it in GitHub Desktop.

Media Setup for CapRover NGINX and Django

Use NGINX to serve the media files on CapRover for a Django project.

To give a brief overview, everything in CapRover is in a container, including NGINX, so no container can access other container data. But if you want to serve your media files (this will work the same for static files), NGINX needs to be able to access these files. Fortunately, the NGINX CapRover container provides a folder on the host that it can read and can be shared with other containers. So, basically, what you need to do is tell your Django app to write the media files in this shared folder so that NGINX can read them.

# Django media settings
STORAGES = {
    "BACKEND": "django.core.files.storage.FileSystemStorage",
}
MEDIA_ROOT = APPS_DIR / "media"
MEDIA_URL = "/media/"

Some assumptions:

  • The project is named myjourney (this is also the APPS_DIR).
  • In the project's running container, the project is in a folder /app, so the media files would be located in /app/myjourney/media.

The NGINX container in CapRover provides a shared folder for apps to store data that it can access. Check here for more details.

  • On the host: /captain/data/nginx-shared
  • In the container: /nginx-shared
  1. Create a media folder for myjourney on the host in the shared folder, so something like /captain/data/nginx-shared/myjourney/media.
  2. Add a persistent directory in the app config in CapRover. This will map the media app in your container to the media directory in the CapRover shared directory on the host.
    • Path in App: /app/myjourney/media
    • Path on Host (don't let CapRover manage the path): /captain/data/nginx-shared/myjourney/media
  3. Configure your app's NGINX to serve the files from the NGINX shared folder when requests are received to the media path.
    location /media/ {
        alias /nginx-shared/myjourney/media/;
    }
    
    There are two server configs; update both.
@aimedey19
Copy link

Path in App: /usr/src/app/myjourney/media

@Tobi-De
Copy link
Author

Tobi-De commented Jun 21, 2024

Path in App: /usr/src/app/myjourney/media

Depends on the specifics of the Image / container, that's why I said (as part of the assumptions)

In the project's running container, the project is in a folder /app, so the media files would be located in /app/myjourney/media.

@aimedey19
Copy link

Okay fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment