【发布时间】:2019-02-18 19:27:12
【问题描述】:
我在一个使用 NGINX 和 Gunicorn 的域的 ubuntu 服务器上设置了我的 django wagtail。我的目录中 static 文件夹中的 CSS 和 JS 文件得到了正确的服务,但我不知道为什么 Wagtail Admin CSS/JS 文件没有得到服务。我假设这与 Wagtail 管理文件不在我的 CSS/JS 文件的静态文件夹中的事实有关。 我运行了 CollectStatic 并设置了 Debug=False。
Google chrome 报告在管理员 CSS/JS 上找不到 404 文件
NGINX 文件的一部分
server {
listen 443 default_server;
listen [::]:443 default_server;
root /home/projects/stemletics/stemletics/mysite/mysite;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name domain.com www.domain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/projects/stemletics/stemletics/mysite/mysite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/projects/stemletics/stemletics/mysite/mysite.sock;
}
Base.py 的相关部分
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Wagtail settings
WAGTAIL_SITE_NAME = "mysite"
# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'http://example.com'
Production.py
from .base import *
DEBUG = False
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY_STEMLETICS"]
try:
from .local import *
except ImportError:
pass
静态文件夹路径
root@django-manaland:/# cd home/projects/stemletics/stemletics/mysite/mysite/static/
root@django-manaland:/home/projects/stemletics/stemletics/mysite/mysite/static# lsbrand css fonts img js scss
【问题讨论】:
-
你配置不正确,我不认为你的静态目录是
/home/projects/stemletics/stemletics/mysite/mysite。请从您的settings.py发布相关部分。 -
添加了要发布的请求信息
-
您的
location /static/应该指向静态目录,即/my/project/dir/static。如何提供您自己的 CSS/JS 文件是一个谜,但我认为这是因为您在 HTML 中手动编码了静态 URL(它们也指向错误的位置)。 -
哈哈,我的 Nginx 文件太乱了,不过我现在完全理解了。我的 Nginx 文件中的根目录和静态位置都是错误的。谢谢!