如何解决nginx 文件名重复
我正在尝试设置 django 项目以使用 gunicorn 和 nginx 服务器。与DEBUG=FALSE
。
我在 nginx 日志中看到静态词重复两次从而改变了路径。
# settings.py
"""
Django settings for cognizance project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file,see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values,see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
import sys
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '<SECRET>'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','pages'
]
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 = 'cognizance.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 = 'cognizance.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/
STATIC_URL = '/static/'
if sys.argv[1] != 'runserver':
STATIC_ROOT = os.path.join(BASE_DIR,"static/")
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# NGINX CONF - under sites-enabled
server {
listen 80;
server_name 127.0.0.1;
location /static/ {
root /home/tejas/Desktop/cognizance;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
# The error when I see the nginx logs
2021/04/18 17:27:05 [error] 7902#7902: *96 open() "/home/tejas/Desktop/cognizance/static/static/js/bs-init.js" failed (2: No such file or directory),client: 127.0.0.1,server: 127.0.0.1,request: "GET /static/js/bs-init.js HTTP/1.1",host: "127.0.0.1",referrer: "http://127.0.0.1/"
2021/04/18 17:27:05 [error] 7902#7902: *95 open() "/home/tejas/Desktop/cognizance/static/static/js/smoothproducts.min.js" failed (2: No such file or directory),request: "GET /static/js/smoothproducts.min.js HTTP/1.1",referrer: "http://127.0.0.1/"
2021/04/18 17:27:05 [error] 7902#7902: *99 open() "/home/tejas/Desktop/cognizance/static/static/js/theme.js" failed (2: No such file or directory),request: "GET /static/js/theme.js HTTP/1.1",referrer: "http://127.0.0.1/"
2021/04/18 17:27:06 [error] 7902#7902: *98 open() "/home/tejas/Desktop/cognizance/static/static/js/Simple-Slider.js" failed (2: No such file or directory),request: "GET /static/js/Simple-Slider.js HTTP/1.1",referrer: "http://127.0.0.1/"
您可以看到它请求 /home/tejas/Desktop/cognizance/static/static/js/Simple-Slider.js
,而不是
/home/tejas/Desktop/cognizance/static/js/Simple-Slider.js
。
我该如何解决?
项目目录骨架
我按照本教程设置 nginx
请忽略这一点,stackoverflow 需要我多写几行东西.....
解决方法
尝试对静态文件配置使用 alias
而不是 root
location /static {
alias /home/tejas/Desktop/cognizance/static/;
}
无论哪种方式,您都需要将 /static/
添加到根/别名
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。