I know this question was already asked frequently. But the supposed solutions seem not to help me.
Here is my Nginx definition for the static files
location /static/ {
alias /data/atsi_webapp/ATSi_WebApp/static;
}
Here my Django settings
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I did run already
python manage.py collectstatic
However, I get
Not Found: /static/js/xxxx.js
CodePudding user response:
I think your nginx declaration causes the issue.
Could you please try this:
location /static/ {
# static files
autoindex on;
autoindex_exact_size off;
# /data/atsi_webapp/ATSi_WebApp <-- may be in your case
root /exact/path/to/project/folder;
}
Instead of this:
location /static/ {
alias /data/atsi_webapp/ATSi_WebApp/static;
}
CodePudding user response:
This error related to incorrect usage of alias
nginx directive is discussed over and over again on SO. Lets assume you have a request http://example.com/static/js/script.js
. Normalized URI of this request which is processed by nginx location /static/ { ... }
directive is /static/js/script.js
When you have an alias /data/atsi_webapp/ATSi_WebApp/static;
directive inside that location, nginx removes /static/
prefix used in location
directive from the URI and appends the rest to the path given in asias
directive. This result is concatenation of /data/atsi_webapp/ATSi_WebApp/static
and js/script.js
strings which gives you /data/atsi_webapp/ATSi_WebApp/staticjs/script.js
file path/name. So general rule of writing the path when you use an alias
directive is to use the trailing slash in the path when you had that slash in your location
directive and don't use it otherwise. This means that when you use an alias
directive you should use either
location /static {
alias /data/atsi_webapp/ATSi_WebApp/static;
}
or
location /static/ {
alias /data/atsi_webapp/ATSi_WebApp/static/;
}
location blocks for your static files (I think the second one is preferred).
On the other side a root
directive appends its path to the full request URI. As nginx documentation states,
When location matches the last part of the directive’s value:
location /images/ { alias /data/w3/images/; }
it is better to use the
root
directive instead:location /images/ { root /data/w3; }
So @Sabil answer is right (although it is missing the explanation) and you'd better use
location /static/ {
root /data/atsi_webapp/ATSi_WebApp;
}
This way for the http://example.com/static/js/script.js
request the path /data/atsi_webapp/ATSi_WebApp
will be concatenated with the normalized URI /static/js/script.js
given you correct /data/atsi_webapp/ATSi_WebApp/static/js/script.js
file name/path. Since every normalized URI starts with the slash, you are not required to add that slash to the end of root
directive argument although adding it won't cause so many harm as missing required slash at the end of alias
directive argument.