How to serve media files in Django app on Railway ? Nginx ? Please help
Hello everyone,
I’m a software development student and me and my peers we have written our first web app with Django. We are absolutely new to Django, Railway and Nginx but we was learning on the road and after almost a week of deployement efforts we have managed to deploy our app on Railway.
We used Dockefile, gunicorn and whitenoise for static files. All this is working great but the app doesn't serve media files. After research we discovered that apparently we need to use Nginx web server for serving media files (those uploaded via users and admins).
So we'are trying to configure a Nginx webserver to do that but as we never worked with nginx our conf file is not working. All the static assets are served ok (js, css, etc) but files in media directory are not found.
Please, could anyone help us to configure nginx or other to serve media files ?
Project id: d616bd58-0440-4197-b741-24b36e54d293
settings.py:
47 Replies
Project ID:
d616bd58-0440-4197-b741-24b36e54d293
nginx conf file:
dockerfile:
entrypoint.sh:
(use triple backticks for code blocks)
done, I hope is more readable now. thanks
but have a look at this template, nginx isnt strictly needed https://railway.app/template/AWUIv6
specifically
https://github.com/vfehring/django-volumes/blob/master/mysite/settings.py#L133-L134
and
https://github.com/vfehring/django-volumes/blob/master/mysite/urls.py#L27
mount the volume to
/app/media
and there is no need to set that RAILWAY_VOLUME_MOUNT_PATH
variable as it is set automatically on deploythank you, I'm reading it
I did this but I'm not sure if it's right:
-I've mounted a volume for my app with mount path:
/root/DepistClic/DepistClic/media
-I didn't change anything in settings.py
-And this is my urls.py:
But it's still doesn't work ? Have I missed something ? Sorry it's the first time I'm doing thisI've mounted a volume for my app with mount path: /root/DepistClic/DepistClic/mediaplease mount the volume to the path i specified
I didn't change anything in settings.pyi have shown you what to change in your settings.py
And this is my urls.pyi have shown you what to change in your urls.py
I did this but I'm not sure if it's rightit seems like you didnt follow any of the instructions i gave?
fixing it. thanks
Ok, this is what I did, I followed instructions as precisely as I can understand it:
-The volume's mount path is:
/app/media
-The settings.py:
-urls.py:
`
But I think I still miss something. I should put media files manually on the volume right ?your app would need to provide a way to upload the images, please referance the template's code i linked since it does just that
This code from the template https://railway.app/template/AWUIv6
I don't get it.
This is what I understand:
-There is an image field in my database entries: for an item it is currently at
images/file.png
for example.
-Currently this file is stored at ~/DepistClic/DepistClic/media/images/file.png
and the app is unable to serve it to the browser from https://depistclic-production.up.railway.app/media/images/file.png
--> "The requested resource was not found on this server."
-We have mounted a volume with mount path app/media
so the the MEDIA_ROOT = app/media because MEDIA_ROOT = os.environ["RAILWAY_VOLUME_MOUNT_PATH"]
But currently I cannot find this path app/media
. I've read the doc about volumes https://docs.railway.app/reference/volumes and I should be able to find it.
Now I don't understand how the media files will be served from the volume and how I put those file there, or how I tell Django to serve them from there. As an admin or user I upload the image to the app. It goes to media directory of the app right ?
Could you help me to understand ? I think I'm missing something fundamental here
Thank you for your time and patience, I'm still learningI'll just tag in the creator of that template when I see him online
@Vin - we need assistance 🙂
lemme catch up
Can you share your repo?
the branch from which I'm trying to deplmoy is this:
https://github.com/v-dav/DepistClic/tree/deployment_vlad
GitHub
GitHub - v-dav/DepistClic at deployment_vlad
Decision aid tool to help healthcare professionals screen for complications in patients affected by Diabetes Mellitus Type 2 - GitHub - v-dav/DepistClic at deployment_vlad
Few things I can see right off the top:
Edit settings.py lines 135-145 to be as follows
This will set the app properly for serving the images.. You have the
urls.py
file setup correctly.
which of your mainapp/urls.py
routes will be displaying images from the mounted volume?
Or are you trying to serve the images that you have in DepistClic/DepistClic/media
?url:
path('download_ordo/<int:screening_test_id>/', views.download_ordo, name='download_ordo')
This allows to open the pdf file if it's loaded in the database, via this view:
And this is the part that rendering the button with the link to the pdf on the page:
And for the images that are rendered on the page if available in database:
It's our first app so I understand if the design may appear quiet horrible 😐And to confirm... What is the mount path of your volume?
/app/media
ok I put this in settings:
Okay, it looks like it may conflict with the
media
folder that you have in your app already... you can change the mount point to /app/uploads
and then the MEDIA_URL = "uploads/"
in your settings.py file... I am trying to rule out the various issuesOk, it's done. This is the deployement log:
System check identified some issues: WARNINGS: ?: (staticfiles.W004) The directory '/app/DepistClic/static' in the STATICFILES_DIRS setting does not exist. Operations to perform: Apply all migrations: admin, auth, contenttypes, mainapp, sessions Running migrations: No migrations to apply. System check identified some issues: WARNINGS: ?: (staticfiles.W004) The directory '/app/DepistClic/static' in the STATICFILES_DIRS setting does not exist. 130 static files copied to '/app/DepistClic/staticfiles'. [2023-11-08 20:15:52 +0000] [36] [INFO] Starting gunicorn 21.2.0 [2023-11-08 20:15:52 +0000] [36] [INFO] Listening at: http://0.0.0.0:5959 (36) [2023-11-08 20:15:52 +0000] [36] [INFO] Using worker: sync [2023-11-08 20:15:52 +0000] [38] [INFO] Booting worker with pid: 38
Okay, that's positive now...
You are using a dockerfile when deploying, yes?
yes, here:
`
With the entrypoint.sh:
`
Okay, all your entrypoint commands look right.
can you try adding a
mkdir static
before your migrate and collectstatic? Just for shits and giggles?okey, doing
It's nothing to do with the database ? Is it ? Just to make sure that it's not a problem with the imageField link pointing to the file
ok the deploy log is happier:
Operations to perform: Apply all migrations: admin, auth, contenttypes, mainapp, sessions Running migrations: No migrations to apply. 130 static files copied to '/app/DepistClic/staticfiles'. [2023-11-08 20:24:16 +0000] [37] [INFO] Starting gunicorn 21.2.0 [2023-11-08 20:24:16 +0000] [37] [INFO] Listening at: http://0.0.0.0:6789 (37) [2023-11-08 20:24:16 +0000] [37] [INFO] Using worker: sync [2023-11-08 20:24:16 +0000] [39] [INFO] Booting worker with pid: 39
Okay, so it looks like it actually succeeded now
ok now the missing image link is following:
https://depistclic-production.up.railway.app/uploads/images/Grade_podologique.png
it seem have changed from media to uploads
now I just have to put the files there right ?
Yes, you would want to upload the images through your admin dashboard, and they should go to the volume now, and the links should work
I'll hang around while you give that a try
yes thanks a lot anyway, for the hour you've spent helping me on that. I really appreciate that
No problem.
just a question, is it normal that I cannot find the uploads folder while I'm in the railway shell ?
railway shell
is a local only shell
from railway --help
:
ok, and how I could know if it's effectively stored on the monted volume ?
the occupied size would increase on the volume metrics
yes it has slightly increased. So I guess the files are there. I've added all files manually through the admin panel, and now redeploying and crossing fingers
Awesome!
Is it possible to visualise somewhere server logs on railway, like whith the development server ?
these are logs? wdym visualize?
i mean all the requests
youd need to enable access logging with gunicorn
https://docs.gunicorn.org/en/stable/settings.html#logging
add
--access-logfile -
to your start command
the dash directs logging to stdout so that it can be picked up by railwaywell, it's not working...
I really don't understand
The files were uploaded on the admin panel and the volume metrics moved. The link to the file points to the uploads as here:
https://depistclic-production.up.railway.app/uploads/ordonnances/Bilan_annuel_4EjwBX7.pdf
The requested resource was not found on this server.
So frustrating...
But I don't want to give app
Thank you guys both of you
this is from the access logging:
192.168.0.3 - - [08/Nov/2023:21:12:49 +0000] "GET /uploads/ordonnances/Bilan_annuel_4EjwBX7.pdf HTTP/1.1" 404 179 "https://depistclic-production.up.railway.app/synthese/";
Did you try changing to
It's the path or permission issue always
I tried with and without first slashes both
static
and media
, and for each case I tried the mount path /app/uploads
and /app/DepistClic/uploads
. Nothing works, not any media file is accessible through urls of type https://depistclic-production.up.railway.app/uploads/ordonnances/file.pdf
. The files are on the mounted volume, I checked it through ls -la
in entrypoint.sh.
Mystery. Tomorrow I'll try again with nginx I guess, maybe it could serve these files
Maybe add os.path.join(BASE_DIR, "uploads") to the STATICFILES_DIRS...
I tried also with nginx with this config:
`
Stilll not working. Could anyone say how people manage media files in production in Django app ? ThanksIn production I always resort to boto3 and cdn network
Setting up, caching, serving, network cost is not worth it to self manage / host
This might help if you stil want to use django
wsgi.py