[英]Django 301 and 403 forbidden errors on my static files in production
我正在尝试使用 nginx 和 gunicorn 在 ubuntu 14.04 vps 上部署一个 django 网站,但是我的 css 文件和 js 文件没有加载。我在默认的 django 开发服务器上开发了它,但当我部署它时它运行良好网站和collectstatic
并尝试通过浏览器访问它,只有 HTML 文本被加载,并且在我的浏览器控制台上检查时,我发现我的 static 文件夹中有301
和403
forbidden
错误。 我也是 ubuntu 和 django 的新手,我在网上找到的所有主题和论坛都只是指向文件的权限和所有权,当我更正我的权限时没有任何运气。
当我尝试访问 django admin 时我的浏览器错误的屏幕截图
这是我的设置.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;
我的网站配置文件
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 和 collectstatic 然后检查
检查您的项目所有权: ls -l <YOUR_PROJECT_FOLDER>
并确保您的用户和组与您在nginx.conf
拥有的用户相同。
在您的情况下,您有user www-data www-data;
Nginx 需要能够读取静态文件夹。 您可以通过将 www-data 用户添加到项目用户所在的组来执行此操作。
sudo usermod -a -G your_user www-data
然后重启nginx。
检查用户和组权限,可能是某些 javascripts 没有执行权限。 我的意思是 static 文件夹和子文件夹的 rwx 权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.