Reputation: 1835
I am trying to build a website on a CentOS server. My stack consists of Django, PostgreSQL, and Nginx/Gunicorn. My version of Nginx is 1.7.7. I am having trouble configuring the server so that my static files (css/js) is accessible to my Django app.
Here's what the configuration looks like:
In /var/www/domain.com/
/var/www/domain.com
├── Repository/
│ ├── wsgi.py
│ ├── manage.py
│ ├── static
│ │ ├── css/
│ │ └── js/
│ ├── apps/
│ └── more apps...
├── error/
│ └── 404.html
├── public/
│ ├── 400.html
│ ├── 404.html
│ └── 500.html
In /etc/nginx/conf.d/django.conf
upstream django_project {
server 127.0.0.1:8003 fail_timeout=0;
}
And finally, in /etc/nginx/sites/01-domain.com.conf
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
#root /var/www/domain.com;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name domain.com;
root /var/www/domain.com/public;
#location / {
# # First attempt to serve request as file, then
# # as directory, then fall back to displaying a 404.
# #try_files $uri $uri/ /index.html;
# # Uncomment to enable naxsi on this location
# # include /etc/nginx/naxsi.rules
#}
location /static/ { # STATIC_URL
#root /var/www/firewall/static/;
root /var/www/domain.com/Repository/static/; # STATIC_ROOT
expires 30d;
}
location ^~ /media/ { # MEDIA_URL
root /var/www/domain.com/Repository; # MEDIA_ROOT
}
location ^~ /admin/media/js/admin/ {
root /opt/python-3.4.2/lib/python3.4/site-packages/Django-1.7.1-py3.4.egg/django/contrib/admin/static;
}
#location ^~ /admin/media/image {
# root /opt/python-2.7.8/lib/python2.7/site-packages/django/contrib/admin/static/admin/image/;
#}
#location ^~ /admin/media/css {
# root /opt/python-2.7.8/lib/python2.7/site-packages/django/contrib/admin/static/admin/css/;
#}
location /static/admin { # MEDIA_URL
root /opt/python-3.4.2/lib/python3.4/site-packages/Django-1.7.1-py3.4.egg/django/contrib/admin;
}
#location /admin/img { # MEDIA_URL
# root /opt/python-2.7.8/lib/python2.7/site-packages/django/contrib/admin/static; # MEDIA_ROOT
#}
#location /admin/js { # MEDIA_URL
# root /opt/python-2.7.8/lib/python2.7/site-packages/django/contrib/admin/static; # MEDIA_ROOT
#}
location / {
try_files $uri @app_proxy;
}
location @app_proxy {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_redirect off;
proxy_pass http://django_project;
}
error_page 400 401 402 403 404 /400.html;
#location = /400.html {
# root /var/www/domain.com/error;
#}
error_page 500 502 503 504 /500.html;
#location = /500.html {
# root /var/www/domain.com/error;
#}
#location / {
# include fastcgi_params;
# #fastcgi_pass 127.0.0.1:8080;
# proxy_pass http://127.0.0.1:8003;
#}
}
The file above I somewhat copied from an example I saw. I'm pretty sure the two parts I need help with are the lines starting with location /static
and location ^~ /media/
. I think these are what serve my actual code, so if there is a problem with the configuration of my server or my files are in the wrong place, can someone please tell me how it is supposed to be?
Also, here is my Repository/homeapp/settings.py
"""
Django settings for homeapp project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret_key'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'homeapp',
'crispy_forms',
'app1',
'app2',
'app3',
'app4',
'app5',
'app6',
'app7',
'app8',
'app9',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'homeapp.urls'
WSGI_APPLICATION = 'homeapp.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'homeapp/templates'),
os.path.join(BASE_DIR, 'app1/templates'),
os.path.join(BASE_DIR, 'app2/templates'),
os.path.join(BASE_DIR, 'app3/templates'),
os.path.join(BASE_DIR, 'app4/templates'),
os.path.join(BASE_DIR, 'app5/templates'),
os.path.join(BASE_DIR, 'app6/templates'),
os.path.join(BASE_DIR, 'app7/templates'),
os.path.join(BASE_DIR, 'app8/templates'),
os.path.join(BASE_DIR, 'app9/templates'),
)
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name',
'USER': 'user',
'PASSWORD': 'passwd',
'HOST': 'localhost',
'POST': '',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
CRISPY_TEMPLATE_PACK = 'bootstrap3'
Upvotes: 1
Views: 1916
Reputation: 71
Try to remove trailing directory name in nginx config:
root /var/www/domain.com/Repository/; # STATIC_ROOT
Because in your case nginx will serve files like this:
/var/www/domain.com/Repository/static/static/...
Check your nginx error log for lines like:
open() "/var/www/domain.com/Repository/static/static/.../somefile" failed (2: No such file or directory),
Upvotes: 3