我试图使用nginx和gunicorn在ubuntu 14.04 vps上部署一个django网站,但我的css文件和js文件没有得到loaded.i在默认的django开发服务器上开发它,它工作得很好,但是当我部署我的网站和collectstatic
并尝试通过浏览器访问它只有HTML文本被加载,并在我的浏览器控制台检查,我发现我的静态文件夹301
和403
forbidden
错误。 ia新手到Ubuntu和Django,以及我在网上find的所有线程和论坛只是指向文件的权限和所有权,并没有任何运气,当我纠正我的权限。
当我尝试访问Djangopipe理员时,我的浏览器错误的截图
这里是我的settings.py
import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOWED_HOSTS = ['mysite.com','www.mysite.com'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'powerlineapp', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'powerlineproject.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'powerlineproject.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/ STATIC_URL = '/static/' if not DEBUG: STATIC_ROOT = '/var/www/powerline/powerlineproject/static/' MEDIA_ROOT = os.path.join(BASE_DIR,'media') MEDIA_URL = '/media/'
这里是我的主要urls.py
from django.conf.urls import include,url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^',include('powerlineapp.urls')), ]
nginx.conf
user www-data www-data; pid /var/run/nginx.pid; worker_processes 1; worker_rlimit_nofile 100000; events { worker_connections 4096; include /etc/nginx.custom.events.d/*.conf; } http { default_type application/octet-stream; access_log off; error_log /var/log/nginx/error.log crit; sendfile on; tcp_nopush on; keepalive_timeout 20; client_header_timeout 20; client_body_timeout 20; reset_timedout_connection on; send_timeout 20; types_hash_max_size 2048; gzip on; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 256; gzip_comp_level 4; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js; server_names_hash_bucket_size 128; include mime.conf; charset UTF-8; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; server_tokens off; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; include proxy.conf; include fcgi.conf; include conf.d/*.conf; include /etc/nginx.custom.d/*.conf; } include /etc/nginx.custom.global.d/*.conf;
我的网站conf文件
server { listen *:80; server_name powerlineagencies.co.ke; access_log /var/log/nginx/powerline-agencies.access.log; error_log /var/log/nginx/powerline-agencies.error.log; root /var/www/powerline/powerlineproject; index index.html index.htm index.php; location / { proxy_pass http://unix:/var/run/ajenti-v-gunicorn-powerline-agencies-python-wsgi-0.sock; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ /static/.* { alias /var/www/powerline/powerlineproject; }
}
这是我的目录结构
我的目录结构的截图
有人可以帮我解决这个问题吗?
from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在ngix中
location /static/ { alias /var/www/powerline/powerlineproject/static/; }
将这些行添加到urls.py并收集静态,然后检查