Django file statici con Nginx e Unicorn
Domanda
Sto cercando di distribuire la mia applicazione Django con i miei VPS. Quindi ho seguito diversi tutorial e l'unico problema che sto avendo è che non riesco a far apparire i miei file statici. Quindi sotto puoi trovare la struttura dei miei file sul VPS.
- .
- env virtuale: / opt / myApps /
- Progetto Django: / opt / myApps / UnipRogress /
- File statici: / opt / myApps / UnipRogress / Statico /
nginx config: / etc / nginx / siti-disponibile / uniprogress
server {
server_name 188.xxx.xxx.93;
access_log off;
location /static/ {
alias /opt/myapps/uniprogress/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
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"';
}
}
.
E infine nella mia Django Impostazioni.py:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Template Dirs
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_ROOT = '/opt/myapps/uniprogress/static/'
STATIC_URL = '/static/'
.
Ho anche usato: python manage.py collectstatic
ma i miei file statici non verranno visualizzati.
Aggiornamento Il tutorial che ho usato può essere trovato a Oceano digitale .
I Doppio controllò che i file esistono sul server.
Inoltre posso accedere a file statici come: http://188.xxx. xxx.93 / statico / css / bootstrap.css .
ma nel mio codice sorgente http://188.xxx.xxx .93: 8001 / , collega i file statici utilizzando la porta.
significa: <link href="/static/css/bootstrap.css" rel="stylesheet">
Quindi cerca di trovare bootstrap.css in http://188.xxx.xxx.93 : 8001 /static/bootstrap.css e il file non esiste (è necessario rimuovere il post per farlo funzionare).
Soluzione
Per servire i file statici lo fanno in livello nginx come
server {
server_name 188.xxx.xxx.93;
access_log off;
location ~ ^/(static)/ {
# root:- you static files path
# alias /opt/myapps/uniprogress/static/;
root /opt/myapps/uniprogress/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
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"';
}
}
.
Ora tutti i file statici serviranno dal tuo nginx.
Altri suggerimenti
La tua statica_root sta raccogliendo file statici a:
/ opt / myApps / UnipRogress / opt / myApps / UnipRogress / Static /
che non corrisponde alla configurazione del server, che punta a:
opt / myApps / UnipRogress / Static /
Statico_root è solo una cartella che contiene tutti i file statici (immagini, CSS, tutto) in un posto per la distribuzione, quindi impostalo su qualcosa come '/ collection_static /' e fissa la configurazione del server.
Controlla questa cosa
1 Se il più vecchio statico è accessibile da nginx, intendo l'autorizzazione della cartella.
2 o fare questo
Sostituisci questo:
STATIC_ROOT = '/opt/myapps/uniprogress/static/'
.
con questo
STATIC_ROOT = ''
.
e aggiungi questo in Impostazioni
STATICFILES_DIRS = (
'/opt/myapps/uniprogress/static/',
)
.
Spero che funzioni.