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
2
Answers
I think your nginx declaration causes the issue.
Could you please try this:
Instead of this:
This error related to incorrect usage of
alias
nginx directive is discussed over and over again on SO. Lets assume you have a requesthttp://example.com/static/js/script.js
. Normalized URI of this request which is processed by nginxlocation /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 inlocation
directive from the URI and appends the rest to the path given inasias
directive. This result is concatenation of/data/atsi_webapp/ATSi_WebApp/static
andjs/script.js
strings which gives you wrong/data/atsi_webapp/ATSi_WebApp/staticjs/script.js
file path/name. So general rule of writing the path when you use analias
directive is to use the trailing slash in the path when you had that slash in yourlocation
directive and don’t use it otherwise. This means that when you use analias
directive you should use eitheror
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,So @Sabil answer is right (although it is missing the explanation) and you’d better use
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 ofroot
directive argument although adding it won’t cause so many harm as missing required slash at the end ofalias
directive argument.