1👍
Your nginx aliases have to match your STATIC_ROOT
and MEDIA_ROOT
settings, or nginx won’t know where to find these files.
location /media {
alias /home/django/django/proj/proj/site_media/media;
}
location /static {
alias /home/django/django/proj/proj/site_media/static;
}
If that doesn’t work, check where nginx tries to find the file in your error logs.
Serving media files is quite simple: you only need to tell nginx that requests made to MEDIA_URL + '<filename>'
should serve a file named MEDIA_ROOT + '<filename>'
. Those are your location
and alias
settings in nginx.
Static files are a bit more complicated. The way Django works with static files, is that manage.py collectstatic
uses your STATICFILES_FINDERS
to find all static files. This includes all files in your STATICFILES_DIRS
directories. A symbolic link is made in your STATIC_ROOT
directory with the same relative name.
As you can’t create a symbolic link with the exact same full path+name as the file is links to, your STATIC_ROOT
needs to be different from your STATICFILES_DIRS
. If all your static files reside in your app-specific static folders, you can omit the STATICFILES_DIRS
. The STATIC_ROOT
setting is required, though, as Django needs a folder for all the symbolic links in order to collect the files.
These symbolic links are a way to allow third-party apps (that are not in your project root) to still provide static files. Even though your static files can be all over the place, e.g. in your folder with python packages, you only ever need to configure nginx for one specific location of static files. This greatly simplifies your server configuration.
Those symbolic links are also why you always need to run collectstatic
after you’ve made a change in your static files. Otherwise, nginx won’t find a link that tells where the actual file is located, and can’t serve that file.
0👍
Change the paths in the nginx configuration (note the site_media
part)?
location /media {
alias /home/django/django/proj/proj/site_media/media;
}
location /static {
alias /home/django/django/proj/proj/site_media/static;
}
And change the MEDIA_URL
settings to point to valid url:
MEDIA_URL = "/media/"
STATIC_URL = "/static/"
- [Answer]-Does django need to set TEST_RUNNER first to run doctest?
- [Answer]-Django-Allauth and django-facebook to retrieve Facebook profile images