settings.py:
DEBUG = False
STATIC_ROOT = '/opt/app/statics/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/statics/'
# Additional locations of static files
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'statics'),)
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG == True:
urlpatterns += staticfiles_urlpatterns()
/etc/nginx/sites-available/app
server {
server_name xxx.xxx.xxx.xxx;
access_log on;
location /statics/ {
alias /opt/app/statics/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
/etc/nginx/sites-enabled/app is exactly the same as in sites-available Folder with static files has been granted with highest possible privileges ( chmod 777 ).
Whenever I try to run the server with command:
gunicorn -b 0.0.0.0:8000 example.wsgi:application
It actually runs the server, but do not redirect correctly to static files
use this please:
root /opt/app/statics/;
instead:
alias /opt/app/statics/;
if your config with root is like this,
location /static/ {
root /opt/app/statics/;
}
In this case the final path that Nginx will derive will be
/opt/app/statics/statics
This is going to return 404 since there is no statics/ within statics/
This is because the location part is appended to the path specified in the root. Hence, with root, the correct way is
location /static/ {
root /opt/app/;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.