Django Nginx - Fail to serve static files - No css etc -


i trying deploy django project djangos own runserver onto nginx. project works fine on manage.py runserver.. using python2.7, django 1.6 , rhel5.

the project lives in /home/nm/openrem/ following structure

|-- admin |   |-- css |   |-- img |   `-- js |       `-- admin |-- css |-- fonts |-- img |-- js |-- media |   |-- dicom_in |   `-- exports |-- openremproject |-- remapp |   |-- exports |   |-- extractors |   |-- interface |   |-- migrations |   |-- netdicom |   |-- static |   |   |-- css |   |   |-- fonts |   |   |-- img |   |   `-- js |   |-- templates |   |   |-- registration |   |   `-- remapp |   |-- templatetags |-- scripts `-- static     |-- admin     |   |-- css     |   |-- img     |   |   `-- gis     |   `-- js     |       `-- admin     |-- css     |-- fonts     |-- img     `-- js 

(running ./manage.py collectstatic seems create duplicate copies of js, css,font, admin , img in /home/nm/openrem , /home/nm/openrem/static. i’m not sure should happen).

here pertinent section of settings.py lives in openremproject

root_project = os.path.join(os.path.split(__file__)[0],"..") media_root = '/home/nm/openrem/media/'  # absolute path directory static files should collected to. # don't put in directory yourself; store static files # in apps' "static/" subdirectories , in staticfiles_dirs.  static_root = '' media_url = 'http://127.0.0.1/media/' static_url = 'http://127.0.0.1/static/'  # additional locations of static files staticfiles_dirs = (             os.path.join(root_project,'static'),                 )      # list of finder classes know how find static files in     # various locations.     staticfiles_finders = (                 'django.contrib.staticfiles.finders.filesystemfinder',                 'django.contrib.staticfiles.finders.appdirectoriesfinder',                     ) # python dotted path wsgi application used django's runserver. wsgi_application = 'openremproject.wsgi.application' installed_apps = (     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.sites',     'django.contrib.messages',     'django.contrib.staticfiles',     # uncomment next line enable admin:     'django.contrib.admin',     # uncomment next line enable admin documentation:     # 'django.contrib.admindocs',     'remapp',     'django_filters',     'pagination',     'django.contrib.humanize',     'south', ) 

i have nginx.conf in /home/nm/openrem/openremproject/. symbolic link created in /etc/nginx/sites-enabled/ points file.

nginx.conf

nginx.conf

upstream django {     # connect socket     # server unix:///tmp/uwsgi.sock;    # file socket     server 127.0.0.1:8001;      # web port socket     }  server {     # port site served on     listen      8000;     # domain name serve     server_name 192.168.197.111;   # substitute machine's ip address or fqdn     charset     utf-8;      #max upload size     client_max_body_size 75m;   # adjust taste      # django media     location /media/  {     alias /home/nm/openrem/media;      # django project's media files     }          location /static/ {         alias/home/nm/openrem/static/;              }      # finally, send non-media requests django server.     location / {         uwsgi_pass  django;         include     /etc/nginx/uwsgi_params;          }     } 

for uwsgi: uwsgi --http :8000 --chdir /home/nm/openrem/ --module openremproject.wsgi

however, css styling etc not work when go website (it worked using djangos lightweight server). when add image /home/nm/openrem/media/image.png cannot see in http:/localhost:8000/media/image.png. how serve static files correctly?


unsure if related have noticed css works manage.py runserver when debug = true in settings.py


edit when access page on localhost following on linux terminal executed uwsgi command

[pid: 1838|app: 0|req: 36/36] 127.0.0.1 () {38 vars in 650 bytes} [fri jul 17 09:11:44 2015] /static/js/bootstrap.min.js => generated 2306 bytes in 10 msecs (http/1.1 404) 1 headers in 51 bytes (1 switches on core 0) 

this seems trying access css, bootstrap files

you haven't specified static_root directory in settings.py:

static_root = os.path.join(root_project, 'static')

because of that, static files copied wrong directory (which not have defined in nginx.conf)

when call collectstatic django collect assets in static_root directory. assets being displayed on devserver (manage.py runserver) because devserver debug flag set true serves files (found in static directories under project's modules , additional directories defined in staticfiles_dirs (more information can found in official django documentation: https://docs.djangoproject.com/en/1.8/howto/static-files/#serving-static-files-during-development)

finally do not hard code paths in settings, if migrate project environment experience problems:

media_root - os.path.join(root_project, 'media')


Comments