[英]Django, Nginx, Gunicorn static files not working
I am trying to build a website on a CentOS server. 我想在CentOS服务器上建立一个网站。 My stack consists of Django, PostgreSQL, and Nginx/Gunicorn. 我的堆栈包括Django,PostgreSQL和Nginx / Gunicorn。 My version of Nginx is 1.7.7 . 我的Nginx版本是1.7.7 。 I am having trouble configuring the server so that my static files (css/js) is accessible to my Django app. 我在配置服务器时遇到问题,因此我的Django应用程序可以访问我的静态文件(css / js)。
Here's what the configuration looks like: 这是配置的样子:
In /var/www/domain.com/
在/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
在/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
最后,在/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/
. 我很确定我需要帮助的两个部分是以location /static
和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
另外,这是我的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'
Try to remove trailing directory name in nginx config: 尝试删除nginx配置中的尾随目录名称:
root /var/www/domain.com/Repository/; # STATIC_ROOT
Because in your case nginx will serve files like this: 因为在你的情况下,nginx将提供这样的文件:
/var/www/domain.com/Repository/static/static/...
Check your nginx error log for lines like: 检查你的nginx错误日志中的行:
open() "/var/www/domain.com/Repository/static/static/.../somefile" failed (2: No such file or directory),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.